1. Operator
s
Operator is a symbol that performs certain operations.
Python provides the following set of operators
1. Arithmetic Operators
2. Relational Operators or Comparison
Operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
5. Relational
Operators:
1. >
2. >=
3. <
4. <=
5. ==
6. !=
1) a=10
2) b=20
3) print("a > b is ",a>b)
4) print("a >= b is
",a>=b)
5) print("a < b is
",a<=b)
OUTPUT
8) a > b is False
9) a >= b is False
10) a < b is True
a="durga"
2) b="durga"
3) print("a > b is ",a>b)
4) print("a >= b is
",a>=b)
5) print("a < b is ",a<b)
6) print("a <= b is
",a<=b)
OUTPUT
8) a > b is False
9) a >= b is True
10) a < b is False
6. 1) print(True>True) ->False
2) print(True>=True)-> True
3) print(10 >True) ->True
4) print(False > True) ->False
5) 6) print(10>'durga’)
7) TypeError: '>' not supported
between instances of 'int' and
'str'
Relational
Operators:
1) a=10
2) b=20
3) if(a>b):
4) print("a is greater than b")
5) else:
6) print("a is not greater than
b")
OUTPUT
a is not greater than b
7. Logical
Operators:
1. and
2. or
3. not
For boolean types behaviour:
and ==>If both arguments are True then only result
is True or ====>If atleast one arugemnt is True then
result is True not ==>complement
True and False ==>False
True or False ===>True
not False ==>True
8. Logical
Operators:
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
x or y: If x evaluates to True then result is x otherwise
result is y
Eg:
10 and 20 ==> 20
0 and 20 ==> 0
10 or 20 ==> 10
0 or 20 ==> 20
not 10 ==>False
not 0 ==>True
not x: If x is evalutates to False then result is True
9. Eg:
1) "durga" and "durgasoft"
==>durgasoft
2) "" and "durga" ==>""
3) "durga" and "" ==>""
4) "" or "durga" ==>"durga"
5) "durga" or ""==>"durga"
6) not ""==>True
Logical
Operators:
10. 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.
1. & If both bits are 1 then only result is 1 otherwise result is 0
2. | If atleast one bit is 1 then result is 1 otherwise result is 0
3. ^ If bits are different then only result is 1 otherwise result
is 0
4. ~ bitwise complement operator i.e 1 means 0 and 0 means
1
5. >> Bitwise Right shift Operator
12. Assignment
Operators:
We can use assignment operator to assign value to the
variable.
Eg: x=10
We can combine asignment operator with some other
operator to form compound assignment operator.
Eg: x+=10 ====> x = x+10
The following is the list of all possible compound
assignment operators in Python
1. +=
2. -=
3. *=
4. /=
5. %=
1. //=
2. **=
3. &=
4. |=
5. ^=
13. Ternary
Operator
Syntax:
x = firstValue if condition else secondValue
If condition is True then firstValue will be considered else
secondValue will be considered.
Eg 1:
1) a,b=10,20
2) x=30 if a<b else 40
3) print(x) #30
Eg 2: Read two numbers from the
keyboard and print minimum value
1) a=int(input("Enter First Number:"))
2) b=int(input("Enter Second
Number:"))
3) min=a if a<b else b
14. Nesting of ternary
operator
Program for minimum of 3 numbers
1) a=int(input("Enter First Number:"))
2) b=int(input("Enter Second
Number:"))
3) c=int(input("Enter Third Number:"))
4) min=a if a<b and a<c else b if b<c else
c
15. Special
operators
Python defines the following 2 special operators
1. Identity Operators
2. Membership operators
Identity Operators
We can use identity operators for address comparison.
2 identity operators are available
1. is
2. 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
16. Eg-1:
1) a=10
2) b=10
3) print(a is b)
#True
4) x=True
5) y=True
6) print( x is y)
#True
Eg-2:
1) a="durga"
2)
b="durga"
3)
print(id(a))
4)
print(id(b))
5) print(a is
Eg-3:
1) list1=["one","two","three
"]
2)
list2=["one","two","three"]
3) print(id(list1))
4) print(id(list2))
5) print(list1 is list2) #False
6) print(list1 is not list2)
#True
7) print(list1 == list2) #True
Identity
Operators
We can use is operator for address comparison where as ==
operator for content comparison.
17. Membership
operators
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
18. Eg:
1)list1=["sunny","bunny","chinny","pi
nny"] 2) print("sunny" in list1) #True
3) print("tunny" in list1) #False
4) print("tunny" not in list1) #True
Eg:
1) x="hello learning Python is very
easy!!!"
2) print('h' in x) True
3) print('d' in x) False
4) print('d' not in x) True
Membership
operators
19. Operator
Precedence
If multiple operators present then which operator will be
evaluated first is decided by operator precedence.
Eg-1:
print(3+10*2)
#23
print((3+10)*2) #
26
Eg-2:
1) a=30
2) b=20
3) c=10
4) d=5
5) print((a+b)*c/d)
#100.0
6) print((a+b)*(c/d))
#100.0
7) print(a+(b*c)/d)
20. The following list describes operator precedence in Python
Operator
Precedence
1. () #Parenthesis
2. ** #exponential operator
3. ~,- #Bitwise complement operator,unary minus
operator
4. *,/,%,// #multiplication,division,modulo,floor division
5. +,- #addition,subtraction
6. <<,>> # Left and Right Shift
7. & # bitwise And
21. 9. | # Bitwise OR
10. >,>=,<,<=, ==, != ==> # Relational or Comparison
operators 11. =,+=,-=,*=... ==> # Assignment
operators
12. is , is not # Identity Operators
13. in , not in # Membership operators
14. not # Logical not
15. and # Logical and
Operator
Precedence