Operators in Python

Operators are used to perform any action between variable and value,these are followed:

Different types of operators in python are:

Arithmetic Operators To perform arithmetic operations. – // / * ** %
Assignment Operators To assign value to a variable or to update the value of an existing variable. = += -= *= //= /= **= %=
Comparison (Relational) Operators To compare the left and right side value or variable.either is true or not == != > >= < <=
Logical Operators Allow us to combine two or more conditional operators. Return True/False based on the Truth table. and or not
Bitwise Operators To perform bit operations bit by bit on the operands. >& | ^ ~ >> <<
Membership Operators To find the value in corresponded value in Standard Data Type in not in
Identity Operators To compare the value in at key value is is not

Numeric Operations

  • Arithmetic Operations
  • Arithmetic operators in Python are used to perform arithmetic operations such as addition, subtraction, multiplication, division etc
Operator Usage Description
+ a+b Return addition of a and b. Return type of result will depend on type of operands.
a-b Return subtraction of a and b. Return type of result will depend on type of operands
* a*b Return multiplication of and b. Return type of result will depend on type of operands.
/ a/b Return quotient of division of a by b. Return type of the result is always a float type.
// a//b Return greatest integer lower than the quotient of division of a by b.
% a%b Return remainder of division a by b.
** a**b Return a raise to power of b.
>>> a=234
>>> b=25
>>> a+b
259
>>> a-b
209
>>> a*b
5850
>>> a/b
9.36
>>> a//b
9
>>> a%b
9
>>> a**b
169979456100812713259954082694997919264875609543036186394624
>>> 

Assignment Operations

  • In Python, the assignment operators are used to assign values to variables or update values of variables.
Operator Usage Description
= a=x Assigns value ‘x’ to the variable ‘a’.
+= a+=x Adds a number ‘x’ to variable ‘a’, changing the variable itself.
-= a-=x Subtract a number ‘x’ from variable ‘a’, changing the variable itself
*= a*=x Multiplies a number ‘x’ to variable ‘a’, changing the variable itself
/= a/=x Divides a number ‘x’ by variable ‘a’, changing the variable itself
%= a%=x Perform modulo of variable ‘a’ from a number ‘x’, changing the variable itself
//= a//=x Perform floor division of variable a’ from a number ‘x’, changing the variable itself
**= a**=x Perform exponential of variable ‘a’ from a number ‘x’, changing the variable itself
>>> n=33
>>> n+=23
>>> print(n)
56
>>> n-=15
>>> print(n)
41
>>> n*=34
>>> print(n)
1394
>>> n/=2
>>> print(n)
697.0
>>> n//=2
>>> print(n)
348.0
>>> n**=3
>>> print(n)
42144192.0
>>>

Comparison (Relational) Operations

  • Relational operators compare the values on either side of the operand and decide the relation among them. Return True/False based on whether the condition is met.
Operator Usage Description
== a==b Return True, if a and b are equal
!= a!=b Return True, if a and b are not equal
< a<b Return True, if a is less than b
<= a<=b Return True, if a less than or equal to b
>> a>b Return True, if a is greater than b
>= a>=b Return True, if a is greater than or equal to b
>>> a=23
>>> b=344
>>> print(a>b)
False
>>> print(a>> print(a>=b)
False
>>> print(a<=b)
True
>>> print(a==b)
False
>>> print(a!=b)
True
>>>

Logical Operations

  • The logical operators in Python allow us to combine two or more conditional operators.
Operator Usage Description
and a and b Logical AND return True, if both the conditions a and b are True.
or a or b Logical OR return True, if one of the conditions a or b is True.
not not a Logical NOT (Negation) reverses the logical state of its operand.
>>> num1=233
>>> num2=33
>>> print(num1>num2 and num1>> print(num1>num2 or num1>> print(not 34>33)
False
>>>

Membership Operators

Python Membership Operators used for checking the sequence of the string,tuple etc ,there are two types of membership operators.

in, not in Evaluating the and checking the sequence.
>>> a=(1,2,3,4,5,6)
>>> 2 in a
True
>>> 34 not in a
True
>>> 3 not in a
False
>>> a="Mystring"
>>> "My" in a
True
>>> "String" in a
False
>>> 

Identity Operation

is, is not Used to test the identity of two numeric objects.
>>> "mystring" is  "Mystring"
False
>>> 33 is 45
False
>>> 23 is 23
True
>>> 33.0 is not 33
True
>>> "ab" is not "AB"
True

Bitwise Operators

The following operators are supported by Python.

Operator Usage Description
Binary And & a & b Finding the and operation at bit level.
Binary Or|

a | b

Finding the or operation at bit level.
Binary XOR ^ a ^ b Finding the xor operators at bit level.
Binary ones complement (~a) Using for the binary ones at bit level.
<< Binary left shift (a<<b) Using for shifting the bit at the left side.
<< Binary Right shift (a>>b) Using for shifting the bit at the Right side.
> a|b #finding bitwise or
254
>>> a^b #finding xor
52
>>> ~a #ones complement
-235
>>> ~b #ones complement
-223
>>> a=10
>>> b=2
>>> a<<b
40
>>> a>>b
2 >>>
Subscribe Now