Operator is a symbol that performs certain operations.
Python provides the following set of operators
- Arithmetic Operators
- Relational Operators or Comparison Operators
- Logical operators
- Bitwise operators
- Assignment operators
- Special operators
- Membership Operators
- Identity Operators
+ ==>Addition - ==>Subtraction * ==>Multiplication / ==>Division operator % ==>Modular operator // ==>Floor Division operator ** ==>Exponent operator or power operator like as pow(a,b) function
Ex: test.py: a=10 b=2 print('a+b=',a+b) print('a-b=',a-b) print('ab=',ab) 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 ab= 20 a/b= 5.0 a//b= 5 a%b= 0 a*b= 100
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.
- We can use +,* operators 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.
If we use * operator for str type then compulsory one argument should be int and other argument should be str type.
2.5*”seed” ==>TypeError: can’t multiply sequence by non-int of type ‘float’
“seed”*”seed”==>TypeError: can’t multiply sequence by non-int of type ‘str’
+ ==>String concatenation operator
* ==>String multiplication operator
Relational Operators (Comparison Operator):
|==||If the values of two operands are equal, then the condition becomes true.||(a == b) is not true.|
|!=||If values of two operands are not equal, then condition becomes true.||(a != b) is true.|
|<>||If values of two operands are not equal, then condition becomes true.||(a <> b) is true. This is similar to != operator.|
|>||If the value of left operand is greater than the value of right operand, then condition becomes true.||(a > b) is not true.|
|<||If the value of left operand is less than the value of right operand, then condition becomes true.||(a < b) is true.|
|>=||If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.||(a >= b) is not true.|
|<=||If the value of left operand is less than or equal to the value of right operand, then condition becomes true.||(a <= b) is true.|
Ex 1: a=10 b=20 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
Ex 2: a="seed" b="seed" 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
print(10 >True) True
print(False > True) False
TypeError: '>' not supported between instances of 'int' and 'str'
== , !=
We can apply these operators for any type even for incompatible types also
>>> 10!= 20
Note: Chaining concept is applicable for equality operators. If atleast one comparison returns False then the result is False. Otherwise the result is true.
and, or ,not
We can apply for all types.
For boolean types behaviour:
and ==>If both arguments are True then only result is True
or ====>If at least one argument is True then result is True
True and False ==>False
True or False ===>True
not False ==>True
For non-boolean types behaviour:
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
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
If x is evalutates to False then result is True otherwise False
not 10 ==>False
not 0 ==>True
"seed" and "dharshi" ==>dharshi
"" and "seed" ==>""
"seed" and "" ==>""
"" or "seed" ==>"seed"
"seed" or ""==>"seed"
not "seed" ==>False
- 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.
|& Binary AND||Operator copies a bit to the result if it exists in both operands||(a & b) (means 0000 1100)|
|| Binary OR||It copies a bit if it exists in either operand.||(a | b) = 61 (means 0011 1101)|
|^ Binary XOR||It copies the bit if it is set in one operand but not both.||(a ^ b) = 49 (means 0011 0001)|
|~ Binary Ones Complement||It is unary and has the effect of ‘flipping’ bits.||(~a ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number.|
|<< Binary Left Shift||The left operands value is moved left by the number of bits specified by the right operand.||a << 2 = 240 (means 1111 0000)|
|>> Binary Right Shift||The left operands value is moved right by the number of bits specified by the right operand.||a >> 2 = 15 (means 0000 1111)|
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 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 1 ==>0 & 0==>1 << ==>Bitwise Left shift >> ==> Bitwise Right Shift
We can apply bitwise operators for boolean types also
print(True & False) ==>False
print(True | False) ===>True
print(True ^ False) ==>True
We can use assignment operator to assign value to the variable.
We can combine assignment operator with some other operator to form compound assignment operator.
Ex: x+=10 ====> x = x+10
The following is the list of all possible compound assignment operators in Python += -= *= /= %= //= **= &= |= ^= >>= <<=
x = first Value if condition else second Value
If condition is True then first Value will be considered else second Value will be considered.
x=30 if a<b else 40
Ex 2: Read two numbers from the keyboard and print minimum value
a=int(input("Enter First Number:"))
b=int(input("Enter Second Number:"))
min=a if a<b else b
Enter First Number:10
Enter Second Number:30
Minimum Value: 10
Note: Nesting of ternary operator is possible.
Q. Program for minimum of 3 numbers a=int(input("Enter First Number:")) b=int(input("Enter Second Number:")) c=int(input("Enter Third Number:")) min=a if a<b and a<c else b if b<c else c print("Minimum Value:",min)
a=int(input(“Enter First Number:”))
b=int(input(“Enter Second Number:”))
print(“Both numbers are equal” if a==b else “First Number is Less than Second Number” if a<b else “First Number Greater than Second Number”)
Enter First Number:10
Enter Second Number:10
Both numbers are equal
Enter First Number:10
Enter Second Number:20
First Number is Less than Second Number
Enter First Number:20
Enter Second Number:10
First Number Greater than Second Number
Python defines the following 2 special operators
- Identity Operators
- Membership operators
We can use identity operators for address comparison.
2 identity operators are available
is is not
r1 is r2 returns True if both r1 and r2 are pointing to the same object
r1 is not r2 returns True if both r1 and r2 are not pointing to the same object
print(a is b) True
print( x is y) True
print(a is b)
print(list1 is list2) False
print(list1 is not list2) True
print(list1 == list2) True
We can use is operator for address comparison whereas == operator for content comparison.
We can use Membership operators to check whether the given object present in the given collection.(It may be String,List,Set,Tuple or Dict)
in => Returns True if the given object present in the specified Collection
not in =>Retruns True if the given object not present in the specified Collection
x=”hello learning Python is very easy!!!”
print(‘h’ in x) True
print(‘d’ in x) False
print(‘d’ not in x) True
print(‘Python’ in x) True
print(“seed” in list1) True
print(“india” in list1) False
print(“delhi” not in list1) True
Python Operators Precedence
|Sr.No.||Operator & Description|
|1||**Exponentiation (raise to the power)|
|2||~ + –Complement, unary plus and minus (method names for the last two are +@ and -@)|
|3||* / % //Multiply, divide, modulo and floor division|
|4||+ –Addition and subtraction|
|5||>> <<Right and left bitwise shift|
|7||^ |Bitwise exclusive `OR’ and regular `OR’|
|8||<= < > >=Comparison operators|
|9||<> == !=Equality operators|
|10||= %= /= //= -= += *= **=Assignment operators|
|11||is is notIdentity operators|
|12||in not inMembership operators|
|13||not or andLogical operators|