Operators are used to manipulate the values of operands. This tutorial lets you understand the python operators.
Python Operators :
Python provideing following set of operators.
- Arithmetic Operators
- Comparison (Relation) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Special Operators
Arithmetic Operators :
These operators are used to do the mathematical operators like addition, subtraction, multiplication and Etc.,
- + Add two operands or unary plus.
- – Subtract right operand or unary minus.
- * Multiply two operands.
- / Divide left operand by right.
- % Modulus- Reminder of the division of left operand.
- // Floor division. Division that results into whole number adjusted in the left in number line.
- ** Exponent – Left operand raised to the power of right.
Example :
demo.py
x=20
y=4
print('x+y : ', x+y)
print('x-y : ', x-y)
print('x*y : ', x*y)
print('x/y : ', x/y)
print('x%y : ', x%y)
print('x//y : ', x//y)
print('x**y : ', x**y)
Output :
$ python3 demo.py
x+y : 24
x-y : 16
x*y : 80
x/y : 5.0
x%y : 0
x//y : 5
x**y : 160000
Difference between / and // :
The “/” operator always performs floting point arithmetic, hence it will always returns float values. Where as “//” operator can perform both floating point and integral arithmentic. If both arguments are int type then it returns int type, if any one of argument is float type then it returns float type.
x=15.0
y=4
print('x/y : ', x/y)
print('x//y : ', x//y)
Output :
$ python3 demo.py
x/y : 3.75
x//y : 3.0
Using ‘*’ in String :
We can use * operator in string with number, like 4 * Chandra. It will multiplies the string into number of times.
>>> a = 2 * 'Chandra'
>>> print(a)
ChandraChandra
>>>
Comparison / Relational Operator :
Comparison operators are used to compare the values, it returns either true or false.
- > Greater than- True if left operator is greater than right one.
- < Less than- True if left operator is less than right one.
- == Equals to – True if both operands are equal.
- != Not Equals to – True if both two operands are not equal.
- >= Greater than Or Equals to – True if left operator is greater than or equal to right one.
- <= Less than or Equals to – True if left operator is less than or equals to right one.
Example :
demo.py
x=10
y=12
print('x>y : ', x>y)
print('x=y : ', x>=y)
print('x<=y : ', x<=y)
Output :
$ python3 demo.py
x>y : False
x<y : True
x==y : False
x!=y : True
x>=y : False
x<=y : True
Comparing Strings :
We can use strings in comparison operators in python like below.
x='chandra'
y='chandra'
print('x>y : ', x>y)
print('x=y : ', x>=y)
print('x<=y : ', x<=y)
Output :
$ python3 demo.py
x>y : False
x<y : False
x==y : True
x!=y : False
x>=y : True
x<=y : True
Logical Operators :
Python logical operators are used to do the logical operations on two operands. These operators return either true or false.
- and : Returns True if both operands are True.
- or : Returns True if either of the operand is True.
- not : Returns True if operand is False.
Example :
demo.py
x=True
y=False
print('x and y : ', x and y)
print('x or y : ', x or y)
print('x not y : ', not y)
Output :
$ python3 demo.py
x and y : False
x or y : True
x not y : True
Bitwise Operators :
These operators are used to do the operations on values based on bits.
- & : Bit wise AND
- | : Bit wise OR
- ~ : Bit wise NOT
- ^ : Bit wise XOR
- >> : Bit wise right shift
- << : Bit wise left shift
Example :
demo.py
x=10
y=4
print('x&y : ', x&y)
print('x|y : ', x|y)
print('~x : ', ~x)
print('x^y : ', x^y)
print('x>>2 : ', x>>2)
print('x<<2 : ', x<<2)
Output :
$ python3 demo.py
x&y : 0
x|y : 14
~x : -11
x^y : 14
x>>2 : 2
x<<2 : 40
Bitwise operators internally converts the operand values in the form of binary and performs the operation and gives the values in the form of decimal.
Assignment Operators :
Assignment operators are used to assign the values to the variables. It has both bitwise and arithmetic operators. Below example shows all possible assignment operators in python.
Example :
x=10
print('x=10 : ', x)
x+=10
print('x+=10 : ', x)
x-=10
print('x-=10 : ', x)
x*=5
print('x*=5 : ', x)
x**=5
print('x**=5 : ', x)
x/=5
print('x/=5: ', x)
x%=5
print('x%=5 : ', x)
x//=5
print('x//=5 : ', x)
y=20;
y&=10;
print('y&=10 : ', y)
y|=20;
print('y|=10 : ', y)
y^=10;
print('y^=10 : ', y)
y>>=3;
print('y>>3 : ', y)
y<<=3;
print('y<<3 : ', y)
Output :
$ python3 demo.py
x=10 : 10
x+=10 : 20
x-=10 : 10
x*=5 : 50
x**=5 : 312500000
x/=5: 62500000.0
x%=5 : 0.0
x//=5 : 0.0
y&=10 : 0
y|=10 : 20
y^=10 : 30
y>>3 : 3
y<<3 : 24
So far we have discussed the basic python operators. Apart from these basic operators python provides some special operators which are used to some special cases.
Special Python Operators :
Python supports the two types of special operators.
- Identity Operators
- Membership Operators
Identity Operators :
Identity operators are used to compare the address of the memory locations which are pointed by the operands. Identity operators returns True or False.
- is : Returns True - if two operands are identical (refers to the same object).
- is not : Returns True - True if operands are not identical (do not refer to the same object).
Example :
demo.py
x1=5
y1=5
x2='Hello'
y2='Hello'
print("x1 is y1 : ", x1 is y1)
print("x1 is not y1 : ", x1 is not y1)
print("x2 is y2 : ", x2 is y2)
print("x2 is not y2 : ", x2 is not y2)
Output :
$ python3 demo.py
x1 is y1 : True
x1 is not y1 : False
x2 is y2 : True
x2 is not y2 : False
Membership operators :
Membership operators are used to search for a particular element in a string, list, tuple, set and Etc.,
- in : Returns True - if value of variable if found in sequence.
- not in : Returns True - if value of variable is not found in sequence.
x='Hello World'
print('H' in x)
print('H' not in x)
print('Hello' not in x)
print('hello' in x)
print(' ' in x)
Output :
$ python3 demo.py
True
False
False
False
True
Happy Learning 🙂