■ Python Operators & Control Statements –
Full Program
# -----------------------------
# Python Operators & Control Statements
# -----------------------------
# 1. Arithmetic Operators
x, y = 10, 3
print("Addition:", x + y) # 13
print("Subtraction:", x - y) # 7
print("Multiplication:", x * y) # 30
print("Division:", x / y) # 3.333...
print("Floor Division:", x // y) # 3
print("Modulus:", x % y) # 1
print("Exponentiation:", x ** y) # 1000
print("-" * 40)
# 2. Comparison Operators
print("x == y:", x == y) # False
print("x != y:", x != y) # True
print("x > y:", x > y) # True
print("x < y:", x < y) # False
print("x >= y:", x >= y) # True
print("x <= y:", x <= y) # False
print("-" * 40)
# 3. Logical Operators
a, b = True, False
print("a and b:", a and b) # False
print("a or b:", a or b) # True
print("not a:", not a) # False
print("-" * 40)
# 4. Membership Operators
nums = [1, 2, 3, 4, 5]
print("2 in nums:", 2 in nums) # True
print("10 not in nums:", 10 not in nums) # True
print("-" * 40)
# A. Decision Making Statements
# if
z = 10
if z > 5:
print("z is greater than 5") # z is greater than 5
# if-else
if z % 2 == 0:
print("Even") # Even
else:
print("Odd")
# if-elif-else
num = 0
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero") # Zero
# Nested if
if z > 0:
if z < 20:
print("z is between 0 and 20") # z is between 0 and 20
# Ternary
a, b = 5, 10
result = a if a > b else b
print("Result:", result) # 10
print("-" * 40)
# B. Loop Control Statements
# while loop
i = 1
while i <= 3:
print("While Loop:", i)
i += 1
# Output: 1, 2, 3
# for loop
for i in [1, 2, 3]:
print("For Loop:", i)
# Output: 1, 2, 3
# range()
for i in range(1, 4):
print("Range Loop:", i)
# Output: 1, 2, 3
# Nested Loops
for i in range(2):
for j in range(2):
print("Nested Loop:", i, j)
# Output: (0,0), (0,1), (1,0), (1,1)
print("-" * 40)
# C. Loop Control Jumps
# break
for i in range(5):
if i == 3:
break
print("Break Loop:", i)
# Output: 0, 1, 2
# continue
for i in range(5):
if i == 2:
continue
print("Continue Loop:", i)
# Output: 0, 1, 3, 4