CPT211 - Lecture Note 3
CPT211 - Lecture Note 3
Introduc4on:
In Python, operators are symbols that perform various opera8ons on variables and values.
They allow us to perform mathema8cal computa8ons, manipulate strings, compare values,
and more. This lecture note provides an overview of the different types of operators in Python,
along with detailed illustra8ve examples for each case.
Table of Contents:
1. Arithme8c Operators
1.1 Addi8on Operator (+)
1.2 Subtrac8on Operator (-)
1.3 Mul8plica8on Operator (*)
1.4 Division Operator (/)
1.5 Modulus Operator (%)
1.6 Exponen8a8on Operator (**)
1.7 Floor Division Operator (//)
2. Assignment Operators
2.1 Simple Assignment Operator (=)
2.2 Compound Assignment Operators (+=, -=, *=, /=, %=, **=, //=)
3. Comparison Operators
3.1 Equal to (==)
3.2 Not equal to (!=)
3.3 Greater than (>)
3.4 Less than (<)
3.5 Greater than or equal to (>=)
3.6 Less than or equal to (<=)
4. Logical Operators
4.1 Logical AND (and)
4.2 Logical OR (or)
4.3 Logical NOT (not)
5. Bitwise Operators
5.1 Bitwise AND (&)
5.2 Bitwise OR (|)
5.3 Bitwise XOR (^)
5.4 Bitwise NOT (~)
5.5 Led Shid (<<)
5.6 Right Shid (>>)
6. Membership Operators
6.1 in Operator
6.2 not in Operator
7. Iden8ty Operators
7.1 is Operator
-1-
7.2 is not Operator
8. Conclusion
1. Arithme4c Operators:
1.1 Addi4on Operator (+): The addi8on operator is used to add two values together.
Example:
a=5
b=3
result = a + b
print(result) # Output: 8
1.2 Subtrac4on Operator (-): The subtrac8on operator is used to subtract one value from
another.
Example: a=5
b=3
result = a - b
print(result) # Output: 2
1.3 Mul4plica4on Operator (*): The mul8plica8on operator is used to mul8ply two
values.
Example: a=5
b=3
result = a * b
print(result) # Output: 15
1.4 Division Operator (/): The division operator is used to divide one value by another.
Example:
a = 10
b=2
result = a / b
print(result) # Output: 5.0
1.5 Modulus Operator (%): The modulus operator returns the remainder of the division
opera8on.
Example:
a = 10
b=3
c=2
d=7
result1 = a % b
result2 = a % c
result3 = a % d
print(result1) # Output: 1
print(result2) # Output: 0
print(result3) # Output: 3
1.6 Exponen4a4on Operator (**): The exponen8a8on operator raises the first operand
to the power of the second operand.
-2-
Example: a=2
b=3
result = a ** b
print(result) # Output: 8
1.7 Floor Division Operator (//): The floor division operator returns the quo8ent of the
division opera8on, rounded down to the nearest whole number.
Example: a = 10
b=3
result = a // b
print(result) # Output: 3
2. Assignment Operators:
2.1 Simple Assignment Operator (=): The simple assignment operator is used to assign a
value to a variable.
Example:
a = 10 # a stores value 10
2.2 Compound Assignment Operators (+=, -=, *=, /=, %=, **=, //=): Compound
assignment operators perform the opera8on on the led operand with the right operand
and assign the result to the led operand.
Example:
a=5
a += 2 # Equivalent to a = a + 2 print(a) # Output: 7
3. Comparison Operators:
3.1 Equal to (==): The equal to operator compares two values and returns True if they
are equal; otherwise, it returns False.
Example:
a=5
b=5
result = a == b
print(result) # Output: True
3.2 Not equal to (!=): The not equal to operator compares two values and returns True if
they are not equal; otherwise, it returns False.
Example: a=5
b=3
result = a != b
print(result) # Output: True
3.3 Greater than (>): The greater than operator compares two values and returns True if
the led operand is greater than the right operand; otherwise, it returns False.
-3-
Example:
a=5
b=3
result = a > b
print(result) # Output: True
3.4 Less than (<): The less than operator compares two values and returns True if the led
operand is less than the right operand; otherwise, it returns False.
Example:
a=5
b=3
result = a < b
print(result) # Output: False
3.5 Greater than or equal to (>=): The greater than or equal to operator compares two
values and returns True if the led operand is greater than or equal to the right operand;
otherwise, it returns False.
Example:
a=5
b=5
result = a >= b
print(result) # Output: True
3.6 Less than or equal to (<=): The less than or equal to operator compares two values and
returns True if the led operand is less than or equal to the right operand; otherwise, it
returns False.
Example: a = 5
b=3
result = a <= b
print(result) # Output: False
4. Logical Operators:
4.1 Logical AND (and): The logical AND operator returns True if both operands are True;
otherwise, it returns False.
Example:
a=5
b=3
result = (a > 0) and (b > 0)
print(result) # Output: True
4.2 Logical OR (or): The logical OR operator returns True if at least one of the operands is
True; otherwise, it returns False.
Example: a=5
b=3
result = (a > 0) or (b > 0)
print(result) # Output: True
-4-
4.3 Logical NOT (not): The logical NOT operator returns True if the operand is False and
False if the operand is True.
Example:
a=5
result = not(a > 0)
print(result) # Output: False
5. Bitwise Operators:
5.1 Bitwise AND (&): The bitwise AND operator performs the AND opera8on on each
corresponding bit of the operands.
Example: a=5 # 0101 in binary
b=3 # 0011 in binary
result = a & b
print(result) # Output: 1 (0001 in binary)
5.2 Bitwise OR (|): The bitwise OR operator performs the OR opera8on on each
corresponding bit of the operands.
Example:
a=5 # 0101 in binary
b=3 # 0011 in binary
result = a | b
print(result) # Output: 7 (0111 in binary)
5.3 Bitwise XOR (^): The bitwise XOR operator performs the XOR (exclusive OR) opera8on
on each corresponding bit of the operands.
Example: a = 5 # 0101 in binary
b = 3 # 0011 in binary
result = a ^ b
print(result) # Output: 6 (0110 in binary)
5.4 Bitwise NOT (~): The bitwise NOT operator flips the bits of the operand.
Example:
a = 5 # 0101 in binary
result = ~a
print(result) # Output: -6 (1010 in binary)
5.5 Lee Shie (<<): The led shid operator shids the bits of the led operand to the led by
the number of posi8ons specified by the right operand.
Example: a = 5 # 0101 in binary
result = a << 2
print(result) # Output: 20 (10100 in binary)
5.6 Right Shie (>>): The right shid operator shids the bits of the led operand to the right
by the number of posi8ons specified by the right operand.
Example:
a = 20 # 10100 in binary
result = a >> 2
print(result) # Output: 5 (0101 in binary)
-5-
6. Membership Operators:
6.1 in Operator: The in operator checks if a value exists in a sequence (such as a string,
list, or tuple) and returns True if it is present; otherwise, it returns False.
Example:
string = "Hello"
result = "o" in string
print(result) # Output: True
6.2 not in Operator: The not in operator checks if a value does not exist in a sequence and
returns True if it is not present; otherwise, it returns False.
Example:
string = "Hello"
result = "x" not in string
print(result) # Output: True
7. Iden4ty Operators:
7.1 is Operator: The is operator checks if two variables refer to the same object and
returns True if they do; otherwise, it returns False.
Example:
a = [1, 2, 3]
b=a
result = a is b
print(result) # Output: True
7.2 is not Operator: The is not operator checks if two variables do not refer to the same
object and returns True if they don't; otherwise, it returns False.
Example: a = [1, 2, 3]
b = [1, 2, 3]
result = a is not b
print(result) # Output: True
-6-