What Is A Python Operators

What are operators in python?

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
For example:
  1. >>> 2+3
  2. 5
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.

Arithmetic operators

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, etc.
Arithmetic operators in Python
OperatorMeaningExample
+Add two operands or unary plusx + y
+2
-Subtract right operand from the left or unary minusx - y
-2
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus - remainder of the division of left operand by the rightx % y (remainder of x/y)
//Floor division - division that results into the whole number adjusted to the left in the number linex // y
**Exponent - left operand raised to the power of rightx**y (x to the power y)

Example #1: Arithmetic operators in Python

  1. x = 15
  2. y = 4
  3. # Output: x + y = 19
  4. print('x + y =',x+y)
  5. # Output: x - y = 11
  6. print('x - y =',x-y)
  7. # Output: x * y = 60
  8. print('x * y =',x*y)
  9. # Output: x / y = 3.75
  10. print('x / y =',x/y)
  11. # Output: x // y = 3
  12. print('x // y =',x//y)
  13. # Output: x ** y = 50625
  14. print('x ** y =',x**y)
When you run the program, the output will be:
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

Comparison operators

Comparison operators in Python
OperatorMeaningExample
>Greater than - True if left operand is greater than the rightx > y
<Less than - True if left operand is less than the rightx < y
==Equal to - True if both operands are equalx == y
!=Not equal to - True if operands are not equalx != y
>=Greater than or equal to - True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to - True if left operand is less than or equal to the rightx <= y

Example #2: Comparison operators in Python

  1. x = 10
  2. y = 12
  3. # Output: x > y is False
  4. print('x > y is',x>y)
  5. # Output: x < y is True
  6. print('x < y is',x<y)
  7. # Output: x == y is False
  8. print('x == y is',x==y)
  9. # Output: x != y is True
  10. print('x != y is',x!=y)
  11. # Output: x >= y is False
  12. print('x >= y is',x>=y)
  13. # Output: x <= y is True
  14. print('x <= y is',x<=y)

Logical operators

Logical operators are the andornot operators.
Logical operators in Python
OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false (complements the operand)not x

Example #3: Logical Operators in Python

  1. x = True
  2. y = False
  3. # Output: x and y is False
  4. print('x and y is',x and y)
  5. # Output: x or y is True
  6. print('x or y is',x or y)
  7. # Output: not x is False
  8. print('not x is',not x)
Here is the truth table for these operators.

Bitwise operators

Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.
For example, 2 is 10 in binary and 7 is 111.
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Bitwise operators in Python
OperatorMeaningExample
&Bitwise ANDx& y = 0 (0000 0000)
|Bitwise ORx | y = 14 (0000 1110)
~Bitwise NOT~x = -11 (1111 0101)
^Bitwise XORx ^ y = 14 (0000 1110)
>>Bitwise right shiftx>> 2 = 2 (0000 0010)
<<Bitwise left shiftx<< 2 = 40 (0010 1000)

Assignment operators

Assignment operators are used in Python to assign values to variables.
a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.
There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.
Assignment operators in Python
OperatorExampleEquivatent to
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
//=x //= 5x = x // 5
**=x **= 5x = x ** 5
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = x << 5

Special operators

Python language offers some special type of operators like the identity operator or the membership operator. They are described below with examples.

Identity operators

is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.
Identity operators in Python
OperatorMeaningExample
isTrue if the operands are identical (refer to the same object)x is True
is notTrue if the operands are not identical (do not refer to the same object)x is not True

Example #4: Identity operators in Python

  1. x1 = 5
  2. y1 = 5
  3. x2 = 'Hello'
  4. y2 = 'Hello'
  5. x3 = [1,2,3]
  6. y3 = [1,2,3]
  7. # Output: False
  8. print(x1 is not y1)
  9. # Output: True
  10. print(x2 is y2)
  11. # Output: False
  12. print(x3 is y3)
Here, we see that x1 and y1 are integers of same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings).
But x3 and y3 are list. They are equal but not identical. It is because interpreter locates them separately in memory although they are equal.

Membership operators

in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
In a dictionary, we can only test for presence of a key, not the value.
OperatorMeaningExample
inTrue if value/variable is found in the sequence5 in x
not inTrue if value/variable is not found in the sequence5 not in x

Example #5: Membership operators in Python

  1. x = 'Hello world'
  2. y = {1:'a',2:'b'}
  3. # Output: True
  4. print('H' in x)
  5. # Output: True
  6. print('hello' not in x)
  7. # Output: True
  8. print(1 in y)
  9. # Output: False
  10. print('a' in y)

Post a Comment

0 Comments