Operators in Python
Arithmetic Operator
- + - * / % //
#program for Airthmetic operator
>>> a=10
>>> b=3
>>> a+b #addition
13
>>> a-b #subtraction
7
>>> a*b #multiplication
30
>>> a/b #division, result will be float value
3.3333333333333335
>>> a//b #floor, result will be integer
3
>>> a%b #modulus
1
>>>
Relational Operator
- > >= < <= == !=
#program for Relational operator
>>> a = 10
>>> b = 3
>>> a > b
True
>>> a >= b
True
>>> a < b
False
>>> a <= b
False
>>> a == b
False
>>> a != b
True
Logical Operator
- and or not
#program for Logical operator
>>> a = 10
>>> b = 3
>>> a and b
3
>>> a or b
10
>>> not a
False
>>> not b
False
>>> not True
False
>>> not False
True
Assignment Operator
- =
#program for Assignment operator
>>> a = 10
>>> b = 3
Short-Hand Assignment operator
- += -= *= /= %= //= >>= <<= **= &= |= ^=
#program for Assignment operator
>>> a = 10
>>> a += 1
>>> a
11
>>> a -= 2
>>> a
9
>>> a *= 5
>>> a
45
>>> a //= 9
>>> a
5
>>> a >>= 1
>>> a
2
>>> a <<= 1
>>> a
4
>>> a **= 2
>>> a
16
>>> a &= 2
>>> a
0
>>> a |= 16
>>> a
16
>>> a ^= 2
>>> a
18
>>> a /= 3
>>> a
6.0
Bit-wise Operator
- & | ^ << >>
#program for Bit-wise operator
>>> a =10
>>> b = 3
>>> a & b
2
>>> a | b
11
>>> a ^ b
9
>>> a << 2
40
>>> b >> 2
0
Membership Operator
- in not in
#program for Menbership operator
>> > a = "Mandeep Singh"
>> > b = "Singh"
>> > a in b
False
>> > b in a
True
>> > a not in b
True
>> > b not in a
False
Identify Operator
- is is not
#program for Identify operator
>>> a = 10
>>> b = 5
>>> a is b
False
>>> a is not b
True
Slice Operator
- :
Comments
Post a Comment