Operators 1
Operators 1
1) Arithmetic Operators
3) Logical operators
4) Bitwise oeprators
5) Assignment operators
6) Special operators
1) Arithmetic Operators:
1) + Addition
2) – Subtraction
3) * Multiplication
4) / Division Operator
5) % Modulo Operator
Eg: test.py
1) a=10
2) b=2
3) print('a+b=',a+b)
4) print('a-b=',a-b)
5) print('a*b=',a*b)
6) print('a/b=',a/b)
7) print('a//b=',a//b)
8) print('a%b=',a%b)
9) print('a**b=',a**b)
Output: Python test.py OR py test.py
a+b = 12
a-b= 8
a*b= 20
a/b= 5.0
a//b= 5
a%b= 0
a**b= 100.
Eg:
1) a = 10.5
2) b=2
3)
4) a+b= 12.5
5) a-b= 8.5
6) a*b= 21.0
7) a/b= 5.25
8) a//b= 5.0
9) a%b= 0.5
Eg:
10//2 -> 5
Note:
֍ / operator always performs floating point arithmetic. Hence it will always returns float value.
֍ But Floor division (//) can perform both floating point and integral arithmetic. If arguments are int
type then result is int type. If atleast one argument is float type then result is float type.
Note:
֍ If we want to use + operator for str type then compulsory both arguments should be str type only
otherwise we will get error.
1) >>> "ram"+10
3) >>> "ram"+"10"
4) 'ram10'
֍ If we use * operator for str type then compulsory one argument should be int and other argument
should be str type.
֍ 2*"krishna"
"krishna"*2
Note:
10/0
10.0/0
2) b=20
7)
8) a > b is False
9) a >= b is False
Eg 2:
1) a="durga"
2) b="durga"
7)
8) a > b is False
9) a >= b is True
Eg:
1) print(True>True) False
2) print(True>=True) True
5)
6) print(10>'durga')
Eg:
1) a=10
2) b=20
3) if(a>b):
5) else:
Note:
Chaining of relational operators is possible. In the chaining, if all comparisons returns True then only
result is True. If atleast one comparison returns False then the result is False
1) >>> 10==20
2) False
3) >>> 10!= 20
4) True
5) >>> 10==True
6) False
7) >>> False==False
8) True
9) >>> "raju"=="raju"
10) True
12) False
Note: Chaining concept is applicable for equality operators. If atleast one comparison returns False
then the result is False. Otherwise the result is True.
1) >>> 10==20==30==40
2) False
3) >>> 10==10==10==10
4) True
and -> If both arguments are True then only result is True
not ->Complement
x and y:
Eg: 10 and 20
x or y:
10 or 20 ->10
0 or 20 ->20.
not x:
not 0 ->True
Eg:
5) "durga" or ""==>"durga"
6) not ""==>True
5) Bitwise Operators:
֍ We can apply these operators bitwise.
֍ These operators are applicable only for int and boolean types.
֍ By mistake if we are trying to apply for any other type then we will get Error.
֍ &, |, ^, ~, <>
֍ print(4&5) ->Valid
֍ & -> If both bits are 1 then only result is 1 otherwise result is 0
֍ print(4&5) -> 4
֍ print(4|5) ->5
֍ print(4^5) ->1