Operators are particular symbols which operate on some values and produce an output.
The values are known as Operands.
Example :
4 + 5 = 9
Here 4 and 5 are Operands and (+) , (=) signs are the operators. They produce the output 9.
Python supports the following operators:
1. Arithmetic Operators
e.g.
>>> 10+20
30
>>> 20-10
10
>>> 10*2
20
>>> 10/2
5
>>> 10%3
1
>>> 2**3
8
>>> 10//3
3
>>>
2. Relational Operators
e.g.
>>> 10<20
True
>>> 10>20
False
>>> 10<=10
True
>>> 20>=15
True
>>> 5==6
False
>>> 5!=6
True
>>> 10<>2
True
>>>
3. Assignment Operators
e.g
>>> c=10
>>> c
10
>>> c+=5
>>> c
15
>>> c-=5
>>> c
10
>>> c*=2
>>> c
20
>>> c/=2
>>> c
10
>>> c%=3
>>> c
1
>>> c=5
>>> c**=2
>>> c
25
>>> c//=2
>>> c
12
>>>
4. Logical Operators
Example :
a=5>4 and 3>2
print a
b=5>4 or 3<2
print b
c=not(5>4)
print c
Output:
>>>
True
True
False
>>>
5. Membership Operators
Example :
a=10
b=20
list=[10,20,30,40,50];
if (a in list):
print "a is in given list"
else:
print "a is not in given list"
if(b not in list):
print "b is not given in list"
else:
print "b is given in list"
Output:
>>>
a is in given list
b is given in list
>>>
6. Identity Operators
Example :
a=20
b=20
if( a is b):
print ?a,b have same identity?
else:
print ?a, b are different?
b=10
if( a is not b):
print ?a,b have different identity?
else:
print ?a,b have same identity?
Output:
>>>
a,b have same identity
a,b have different identity
>>>
The values are known as Operands.
Example :
4 + 5 = 9
Here 4 and 5 are Operands and (+) , (=) signs are the operators. They produce the output 9.
Python supports the following operators:
1. Arithmetic Operators
e.g.
>>> 10+20
30
>>> 20-10
10
>>> 10*2
20
>>> 10/2
5
>>> 10%3
1
>>> 2**3
8
>>> 10//3
3
>>>
2. Relational Operators
e.g.
>>> 10<20
True
>>> 10>20
False
>>> 10<=10
True
>>> 20>=15
True
>>> 5==6
False
>>> 5!=6
True
>>> 10<>2
True
>>>
3. Assignment Operators
e.g
>>> c=10
>>> c
10
>>> c+=5
>>> c
15
>>> c-=5
>>> c
10
>>> c*=2
>>> c
20
>>> c/=2
>>> c
10
>>> c%=3
>>> c
1
>>> c=5
>>> c**=2
>>> c
25
>>> c//=2
>>> c
12
>>>
4. Logical Operators
Example :
a=5>4 and 3>2
print a
b=5>4 or 3<2
print b
c=not(5>4)
print c
Output:
>>>
True
True
False
>>>
5. Membership Operators
Example :
a=10
b=20
list=[10,20,30,40,50];
if (a in list):
print "a is in given list"
else:
print "a is not in given list"
if(b not in list):
print "b is not given in list"
else:
print "b is given in list"
Output:
>>>
a is in given list
b is given in list
>>>
6. Identity Operators
Example :
a=20
b=20
if( a is b):
print ?a,b have same identity?
else:
print ?a, b are different?
b=10
if( a is not b):
print ?a,b have different identity?
else:
print ?a,b have same identity?
Output:
>>>
a,b have same identity
a,b have different identity
>>>