MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
Python Operators
MLDAP (MMC201) | Module 1 | The National Institute of Engineering, Mysuru
Course Instructor: Dr. Ananth G S
1. Introduction to Operators
An operator in Python is a special symbol or keyword that performs a specific operation on one or more
operands (values or variables). Operators are the building blocks of expressions and form the core of
computational logic in any program.
Python provides a rich set of operators organized into the following categories:
• Arithmetic Operators
• Relational (Comparison) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
2. Arithmetic Operators
Arithmetic operators are used to perform standard mathematical operations on numeric values (int and
float).
Operator Name Example Result
+ Addition 10 + 3 13
- Subtraction 10 - 3 7
* Multiplication 10 * 3 30
/ Division 10 / 3 3.3333...
// Floor Division 10 // 3 3
% Modulus 10 % 3 1
** Exponentiation 2 ** 8 256
Key Points
• / always returns a float, even if both operands are integers.
• // returns the largest integer less than or equal to the true quotient (floor).
• % (modulus) returns the remainder after integer division.
• ** is right-associative: 2 ** 3 ** 2 = 2 ** (3 ** 2) = 512.
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
Illustrative Program
# Program to demonstrate Arithmetic Operators
a = 17
b = 5
print('a =', a, ', b =', b)
print('Addition a + b =', a + b)
print('Subtraction a - b =', a - b)
print('Multiplication a * b =', a * b)
print('Division a / b =', a / b)
print('Floor Division a // b =', a // b)
print('Modulus a % b =', a % b)
print('Exponentiation a ** b =', a ** b)
Output
a = 17 , b = 5
Addition a + b = 22
Subtraction a - b = 12
Multiplication a * b = 85
Division a / b = 3.4
Floor Division a // b = 3
Modulus a % b = 2
Exponentiation a ** b = 1419857
3. Relational (Comparison) Operators
Relational operators compare two values and return a Boolean result: True or False. They are widely
used in conditions and loops.
Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7>4 True
< Less than 3<1 False
>= Greater than or equal 5 >= 5 True
<= Less than or equal 4 <= 3 False
Illustrative Program
# Program to demonstrate Relational Operators
x = 10
y = 20
print('x =', x, ', y =', y)
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
print('x == y :', x == y)
print('x != y :', x != y)
print('x > y :', x > y)
print('x < y :', x < y)
print('x >= 10 :', x >= 10)
print('x <= y :', x <= y)
Output
x = 10 , y = 20
x == y : False
x != y : True
x > y : False
x < y : True
x >= 10 : True
x <= y : True
4. Assignment Operators
Assignment operators assign values to variables. Compound assignment operators combine an
arithmetic or bitwise operation with assignment, offering a shorthand notation.
Operator Equivalent To Example (x=10) Value of x
= — x = 10 10
+= x=x+n x += 5 15
-= x=x-n x -= 3 7
*= x=x*n x *= 2 20
/= x=x/n x /= 4 2.5
//= x = x // n x //= 3 3
%= x=x%n x %= 3 1
**= x = x ** n x **= 2 100
&= x=x&n x &= 6 2
|= x=x|n x |= 5 15
^= x=x^n x ^= 3 9
>>= x = x >> n x >>= 1 5
<<= x = x << n x <<= 2 40
Illustrative Program
# Program to demonstrate Assignment Operators
x = 10
print('Initial value of x:', x)
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
x += 5
print('After x += 5 :', x) # 15
x -= 3
print('After x -= 3 :', x) # 12
x *= 2
print('After x *= 2 :', x) # 24
x //= 4
print('After x //= 4 :', x) # 6
x **= 2
print('After x **= 2 :', x) # 36
x %= 7
print('After x %= 7 :', x) # 1
Output
Initial value of x: 10
After x += 5 : 15
After x -= 3 : 12
After x *= 2 : 24
After x //= 4 : 6
After x **= 2 : 36
After x %= 7 : 1
5. Logical Operators
Logical operators combine Boolean expressions and return a Boolean result. They are essential for
building complex conditions.
Operator Description Example Result
and True if BOTH operands are True True and False False
or True if AT LEAST ONE is True True or False True
not Reverses the Boolean value not True False
Truth Table
A B A and B A or B not A
True True True True False
True False False True False
False True False True True
False False False False True
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
Short-Circuit Evaluation
Python uses short-circuit evaluation for and / or:
• and: if the first operand is False, the second is not evaluated.
• or: if the first operand is True, the second is not evaluated.
Illustrative Program
# Program to demonstrate Logical Operators
a = True
b = False
print('a =', a, ', b =', b)
print('a and b :', a and b)
print('a or b :', a or b)
print('not a :', not a)
# Compound condition
x = 5
print('x > 0 and x < 10 :', x > 0 and x < 10)
print('x < 0 or x > 3 :', x < 0 or x > 3)
print('not (x == 5) :', not (x == 5))
Output
a = True , b = False
a and b : False
a or b : True
not a : False
x > 0 and x < 10 : True
x < 0 or x > 3 : True
not (x == 5) : False
6. Bitwise Operators
Bitwise operators work at the binary (bit) level on integer values. Each operand is converted to binary,
and the operation is performed bit by bit.
Operator Name Example (a=12, b=10) Result
& Bitwise AND a&b 8 (1000)
| Bitwise OR a|b 14 (1110)
^ Bitwise XOR a^b 6 (0110)
~ Bitwise NOT ~a -13
<< Left Shift a << 2 48
>> Right Shift a >> 1 6
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
Binary Representation (a=12, b=10)
Value Binary
a = 12 0000 1100
b = 10 0000 1010
a & b 0000 1000 = 8
a | b 0000 1110 = 14
a ^ b 0000 0110 = 6
a << 2 0011 0000 = 48
a >> 1 0000 0110 = 6
Illustrative Program
# Program to demonstrate Bitwise Operators
a = 12 # binary: 0000 1100
b = 10 # binary: 0000 1010
print('a =', a, ', b =', b)
print('a & b (AND) :', a & b)
print('a | b (OR) :', a | b)
print('a ^ b (XOR) :', a ^ b)
print('~a (NOT) :', ~a)
print('a << 2 (Left Shift) :', a << 2)
print('a >> 1 (Right Shift):', a >> 1)
Output
a = 12 , b = 10
a & b (AND) : 8
a | b (OR) : 14
a ^ b (XOR) : 6
~a (NOT) : -13
a << 2 (Left Shift) : 48
a >> 1 (Right Shift): 6
7. Membership Operators
Membership operators test whether a value is present in a sequence (list, tuple, string, set, dictionary).
They return True or False.
Operator Description Example Result
in Returns True if value is found in 'e' in 'hello' True
sequence
not in Returns True if value is NOT found 5 not in [1, 2, 3] True
in sequence
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
Illustrative Program
# Program to demonstrate Membership Operators
fruits = ['apple', 'mango', 'banana']
# Testing membership in a list
print('mango in fruits :', 'mango' in fruits)
print('grape in fruits :', 'grape' in fruits)
print('apple not in fruits :', 'apple' not in fruits)
print('cherry not in fruits :', 'cherry' not in fruits)
# Testing membership in a string
text = 'Python Programming'
print('Python in text :', 'Python' in text)
print('Java in text :', 'Java' in text)
# Testing membership in a tuple
nums = (1, 2, 3, 4, 5)
print('3 in nums :', 3 in nums)
print('10 not in nums :', 10 not in nums)
Output
mango in fruits : True
grape in fruits : False
apple not in fruits : False
cherry not in fruits : True
Python in text : True
Java in text : False
3 in nums : True
10 not in nums : True
8. Identity Operators
Identity operators check whether two variables refer to the same object in memory (not just equal
values). This is a check on object identity, not equality.
Operator Description Example Result
is True if both variables point to the same x is y True / False
object
is not True if they point to different objects x is not y True / False
is vs ==
== checks value equality; is checks object identity (same memory address).
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
Illustrative Program
# Program to demonstrate Identity Operators
# Case 1: Lists with same content but different objects
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print('a == b :', a == b) # True – same values
print('a is b :', a is b) # False – different objects
print('a is c :', a is c) # True – same object
print('a is not b :', a is not b)
# Case 2: Small integer caching (-5 to 256)
x = 100
y = 100
print('x is y (100) :', x is y) # True – cached
# Case 3: Large integers – not cached
p = 1000
q = 1000
print('p is q (1000) :', p is q) # False – not cached
Output
a == b : True
a is b : False
a is c : True
a is not b : True
x is y (100) : True
p is q (1000) : False
9. Operator Precedence
When an expression contains multiple operators, Python evaluates them in a specific order called
operator precedence (highest to lowest).
Precedence Operator(s) Description
1 (Highest) ** Exponentiation
2 +x, -x, ~x Unary positive, negative, bitwise NOT
3 *, /, //, % Multiplication, Division, Floor, Modulus
4 +, - Addition, Subtraction
5 <<, >> Bitwise Shifts
6 & Bitwise AND
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
Precedence Operator(s) Description
7 ^ Bitwise XOR
8 | Bitwise OR
9 ==, !=, <, >, <=, >= Comparison / Relational
10 is, is not, in, not in Identity & Membership
11 not Logical NOT
12 and Logical AND
13 (Lowest) or Logical OR
Illustrative Program
# Program to demonstrate Operator Precedence
# Arithmetic precedence
print(2 + 3 * 4) # 14 (* before +)
print((2 + 3) * 4) # 20 (parentheses first)
print(2 ** 3 ** 2) # 512 (right-assoc: 2**(3**2))
print(10 - 4 + 2) # 8 (left to right)
# Mixed operators
print(5 + 2 > 3 * 2) # False (arithmetic before relational)
print(10 > 5 and 3 < 8) # True (relational before and)
print(True or False and False) # True (and before or)
# Negation precedence
print(not 5 > 3) # False (5 > 3 evaluated first)
print(not (5 > 3)) # False (same result here)
Output
14
20
512
8
False
True
True
False
False
Python Operators – e-Notes MMC201 | NIE Mysuru
MLDAP – MMC201 | Module 1: Python Operators | National Institute of Engineering, Mysuru
10. Summary
Category Operators Return Type
Arithmetic + - * / // % ** Number (int / float)
Relational == != > < >= <= Boolean
Assignment = += -= *= /= //= %= **= &= |= ... None (modifies variable)
Logical and, or, not Boolean
Bitwise & | ^ ~ << >> Integer
Membership in, not in Boolean
Identity is, is not Boolean
Prepared by Dr. Ananth G S – Python Operators | MMC201 | NIE Mysuru
Python Operators – e-Notes MMC201 | NIE Mysuru