Python Module 2 (Operators and Expressions)

In this lesson, we will learn about Operators and Expressions in Python.

โœจ Types of Operators in Python

1. Arithmetic Operators

These are used for basic math:

OperatorDescriptionExample (a = 10, b = 3)Result
+Additiona + b13
-Subtractiona - b7
*Multiplicationa * b30
/Divisiona / b3.33
//Floor Divisiona // b3
%Modulus (remainder)a % b1
**Exponentiationa ** b1000

๐Ÿ“Œ Try this in Python:

print(5 + 3)
print(7 % 2)
print(2 ** 3)

2. Comparison (Relational) Operators

Compare values and return True or False.

OperatorDescriptionExample (a = 10, b = 3)Result
==Equal toa == bFalse
!=Not equal toa != bTrue
>Greater thana > bTrue
<Less thana < bFalse
>=Greater than or equala >= bTrue
<=Less than or equala <= bFalse

3. Logical Operators

Used to combine conditional statements.

OperatorDescriptionExampleResult
andTrue if both are trueTrue and FalseFalse
orTrue if at least one is trueTrue or FalseTrue
notReverses resultnot TrueFalse

4. Assignment Operators

Assign values to variables.

OperatorExampleSame As
=x = 5Assign 5 to x
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 4x = x * 4
/=x /= 2x = x / 2
//=x //= 2x = x // 2
%=x %= 2x = x % 2
**=x **= 3x = x ** 3

5.Bitwise Operators

Bitwise operators work on bits (0s and 1s) of integers at the binary level.

They are used to perform operations like AND, OR, XOR, NOT, etc., bit-by-bit.

OperatorDescriptionExample
&AND5 & 3 = 1
``OR
^XOR5 ^ 3 = 6
~NOT~5 = -6
<<Left Shift5 << 1 = 10
>>Right Shift5 >> 1 = 2

๐Ÿงช 6. Identity and Membership Operators

Identity :

x = [1, 2]
y = x
print(x is y)  # True

Membership

fruits = ["apple", "banana"]
print("apple" in fruits)  # True

Expressions in Python

x = 10
y = 5
result = (x + y) * 2
print(result) # Output: 30

Operator Precedence

Determines the order in which operations are performed.

From highest to lowest:

  1. () โ€“ Parentheses
  2. ** โ€“ Exponentiation
  3. +, - (Unary)
  4. *, /, //, %
  5. +, - (Binary)
  6. <, <=, >, >=, ==, !=
  7. and
  8. or
  9. =, +=, -=, etc.

Example :

x = 10
y = 4
z = 3

result = x + y * z - y / 2
print(result)
# Output: 10 + 12 - 2.0 = 20.0

Examples


๐Ÿ”ข 1. Arithmetic Operators

a = 10
b = 3

print(a + b)   # Output: 13
print(a - b)   # Output: 7
print(a * b)   # Output: 30
print(a / b)   # Output: 3.333...
print(a // b)  # Output: 3
print(a % b)   # Output: 1
print(a ** b)  # Output: 1000

๐Ÿค 2. Comparison Operators

x = 5
y = 10

print(x == y)  # Output: False
print(x != y)  # Output: True
print(x > y)   # Output: False
print(x < y)   # Output: True
print(x >= 5)  # Output: True
print(y <= 10) # Output: True

๐Ÿง  3. Logical Operators

a = True
b = False

print(a and b)  # Output: False
print(a or b)   # Output: True
print(not a)    # Output: False


๐Ÿงฑ 4. Bitwise Operators

a = 5      # 0101
b = 3      # 0011

print(a & b)  # Output: 1  (0001)
print(a | b)  # Output: 7  (0111)
print(a ^ b)  # Output: 6  (0110)
print(~a)     # Output: -6 (inverts bits)
print(a << 1) # Output: 10 (shifts left: 1010)
print(a >> 1) # Output: 2  (shifts right: 0010)


โœ๏ธ 5. Assignment Operators

x = 5
x += 3     # Same as x = x + 3
print(x)   # Output: 8

x *= 2     # x = x * 2
print(x)   # Output: 16


๐Ÿ” 6. Identity Operators

a = [1, 2]
b = a
c = [1, 2]

print(a is b)  # Output: True
print(a is c)  # Output: False
print(a == c)  # Output: True (values are same)

๐ŸŽ 7. Membership Operators

fruits = ["apple", "banana", "cherry"]

print("banana" in fruits)   # Output: True
print("grape" not in fruits)  # Output: True

๐ŸŽฏ 8. Expressions Example

result = (5 + 3) * 2 - 4 / 2
print(result)  # Output: 14.0

Welcome to the Python Programming Quiz!

Test your knowledge by selecting the correct answers. You will immediately see if you’re right or wrong.

Score: 0

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *