2.PYTHON Operators
2.PYTHON Operators
THINKING WITH
PYTHON
PYTHON OPERATORS
OPERATORS IN PYTHON
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
ASSIGNMENT OPERATORS
MEMBERSHIP OPERATOR
• in – Evaluates to True if it finds the item in the specified sequence and False
otherwise.
• not in – Evaluates to True if it does not find the item in the specified sequence
and False otherwise.
EXAMPLE PROGRAM- PREDICT THE OUTPUT
#membership operator
ourword='asadsdadsada'
print('d' in ourword)
print('o' in ourword)
print('a' not in ourword)
print('a' in 'Hello')
IDENTITY OPERATOR
• To check two values (or variables) are located in the same part of the memory.
• is- x is y evaluates to true if and only if x and y are the same object.
• is not - x is not y yields the inverse truth value
#identity operator
x=1
y=1.0
print(x is y)
print(x is not y)
print(id(x))
print(id(y))
x=y
print(x is y)
print(x is not y)
print(id(x))
print(id(y))
PRECEDENCE RULES IN PYTHON
• When an expression contains more than one operator, in what order will the
operations be performed?
#identity operator
x=1
y=1.0
print(x is y)
print(x is not y)
print(id(x))
print(id(y))
x=y
print(x is y)
print(x is not y)
print(id(x))
print(id(y))