Operator is a symbol that performs certain operations
Python provides the following set of operators
Arthimetic operators
Relational operator or comparison operators
Logical operators
Bitwise operators
Assignment operators
Special operators
Arthimetic operators
+ —> addition - ---> subtraction * —> multiplication / —> division % —> modulo operator // —> floor division operator
Eg: test.py
a=10
b=2
print(‘a+b=‘,a+b)
print(‘a-b=',a-b)
print(‘a*b=‘,a*b)
print(‘a/b=‘,a/b)
print(‘a//b=‘,a//b)
print(‘a%b=‘,a%b)
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
a = 10.5
b=2
a+b = 12.5
a-b = 8.5
a*b = 21.5
a/b = 5.25
a//b = 5.0
a%b = 0.5
a**b = 110.25
Eg:
10/2 —> 5.0
10//2 —> 5
10.0/2 —> 5.0
10.0//2 —> 5.0
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 at least one argument is float type then result is float type.
Note :
We can use +,* operator for str type also.
If we want to use + operator for str type then compulsory both arguments should be str type only otherwise we will get error.
>>> “demo”+10
TypeError: must be str , not int
>>> “demo”+”10”
‘demo10’
If we use * operator for str type then compulsory one argument should be int and other argument should be str type.
2*”demo”
“demo”*2
2.5*”demo” —> TypeError: cannot multiply sequence by non-int of type ‘float’
“demo”*”demo” —> TypeError: can multiply sequence by non-int of type ’str’
+ —> string concatenation operator * —> string multiplication operator
Note : for any number x,
x/0 and x%0 always raises “ZeroDivisionError”
10/0
10.0/0
Relational operators : >, >=, <, <=
a= 10
b= 2-
print(“a > b is “,a>b)
print(“a >= b is “,a>=b)
print(“a < b is “,a<b)
print(“a <= b is “,a<=b)
a > b Is False
a >= b Is False
a < b is True
a <= b is True
We can apply relational operators for str types also
Eg 2:
a=“demo”
b=“demo”
print(“a > b is “,a>b)
print(“a >= b is “,a>=b)
print(“a < b is “,a<b)
print(“a <= b is “,a<=b)
a > b Is False
a >= b Is True
a < b is False
a <= b is True
Eg:
print(True>True) False
print(True>=True) True
print(10>True) True
print(False > True) False
print(10>’demo’)
TyoeError: ‘>’ not supported between instances of ‘int’ and ’str’
a=10
b=20
If(a>b):
print(“a is greater than b”)
else:
print(“a is not greater than b”)
Output : a is not greater than b
Note: chaining of relational operators is possible. In the chaining, if all compassions returns True then only result is True. If aleast one comparison returns false then the result is false.
10<20 —> True
10<20<30 —> True
10<20<30<40 —> True
10<20<30<40>50 —> False
Equality operator
We can apply these operators for any type even for incompatible types also.
>>> 10==20
False
>>> 10!=20
True
>>> 10==True
False
>>> False==False
True
>>> “demo”==“demo”
True
>>> 10==“demo”
False
Note: chaining concept is applicable for equality operators. If at least one comparison returns false then the result is False. Otherwise the result is True.
>>> 10==20==30==40
False
>>> 10=10=10=10
True
Logical operators: and , or, not
We can apply for all types
For boolean types behavior
And —> if both arguments are True then only result is True
or —> if atleast one argument is True then result is True
not —> complement
True and False —> False
True or False —> True
Not Fasle —> True
For non-boolean types behavior
0 means False
non-zero means True
empty string is always treated as False
x and y:
If x is evaluates to false return x otherwise return y
Eg:
10 and 20
0 and 20
If first argument is zero then result is zero otherwise result is y
x or y
If x evaluates to True then result is x otherwise result is y
10 or 20 —> 10
0 or 20 —> 20
Not x
If x is evaluates to False then result is true otherwise false
not 10 —> false
not 0 —> true
Eg:
“demo” and “demob” ==> demob
“” and “demo” ==> “”
“demo” and “” ==> “”
“” or “demo” ==> “demo”
“demo” or “” ==> “demo”
not “” ==> True
not “demo” ==> False
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
print(10.5 & 5.6)
—> TypeError: unsupported operand type(s) for &: ‘float’ and ‘float’
print(True & True) —> valid
& —> if both bits are 1 then only result is 1 otherwise result is 0
| —> if at least one bit is 1 then result is 1 otherwise result is 0
^ —> if bits are different then only result is 1 otherwise result is 0 - —> bitwise complement operator
1 —> 0 & 0 —> 1
<< —> bitwise left shift
>> —> bitwise right shift
print(4&5) —> 4
print(4 | 5) —> 5
print(4^5) —> 1
Operator
|
Description
|
&
|
If both bits are 1 then only results is 1 otherwise result is 0
|
|
|
If atleast one bit is 1 then result is 1 otherwise result is 0
|
^
|
If bits are different then only result Is 1 otherwise result is 0
|
"
|
Bitwise complement operator i.e means 0 and 0 means 1
|
>>
|
Bitwise left shift operator
|
<<
|
Bitwise right shift operator
|
Bitwise complement operator ( ~ )
We have to apply complement for total bits
Eg : print(~5) = -6
Note :
The most significant bit acts as sign bit. 0 value represents +ve number where as 1 represents -ve value.
Positive numbers will be represented directly in the memory where as -ve numbers will be represented indirectly in 2’s complement form.
Shift operators
<< left shift operator
After shifting the empty cells we have to fill with zero
print(10<<2) —> 40
0 ( cross mark )
|
0 (cross mark )
|
0
|
0
|
1
|
0
|
1
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
0
|
0
|
Right shift operator
After shifting the empty cells we have to fill with sign bit ( 0 for +ve and 1 for -ve )
print(10>>2) —> 2
0
|
0
|
0
|
0
|
1
|
0
|
1 ( cross mark )
|
0 (cross mark )
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
We can apply bitwise operators for boolean types also :
Kaushik Gattu: Python Operators >>>>> Download Now
ReplyDelete>>>>> Download Full
Kaushik Gattu: Python Operators >>>>> Download LINK
>>>>> Download Now
Kaushik Gattu: Python Operators >>>>> Download Full
>>>>> Download LINK B7