B.
Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
Conditional & Control Statements
1. if Statement
The 'if' statement is used to test a condition. If the condition is true, the block of code inside the 'if'
statement is executed.
Example :
1.
x = 10
if x > 5:
print("x is greater than 5")
2.
age = 18
if age >= 18:
print("You are eligible to vote.")
3.
num = 7
if num % 2 != 0:
print("Odd number")
4.
temp = 100
if temp > 99:
print("High temperature")
2. if-else Statement
The 'if-else' statement adds an alternative block if the condition is false.
Example :
1.
x = -3
if x >= 0:
print("Positive")
else:
print("Negative")
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
2.
x=3
if x > 10:
print("Greater than 10")
else:
print("10 or less")
3.
marks = 45
if marks >= 50:
print("Pass")
else:
print("Fail")
4.
speed = 60
if speed > 80:
print("Over Speeding")
else:
print("Normal Speed")
3. if-elif-else Statement
Used when you have multiple conditions to check.
Example :
1.
x=0
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
2.
day = 2
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
else:
print("Other day")
3.
score = 85
if score >= 90:
print("Grade A")
elif score >= 75:
print("Grade B")
else:
print("Grade C")
4. Nested if-else
An if or else block containing another if-else structure.
Example :
1.
x = 10
if x >= 0:
if x == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
2.
x = 15
if x > 10:
if x < 20:
print("Between 10 and 20")
else:
print("20 or more")
else:
print("10 or less")
3.
a=5
if a > 0:
if a % 2 == 0:
print("Positive Even")
else:
print("Positive Odd")
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
else:
print("Negative")
5. for Loop
Used to iterate over a sequence (like list, tuple, range).
Example :
1.
for i in range(5):
print(i)
2.
for char in 'abc':
print(char)
3.
for i in range(2, 7):
print(i)
4.
for fruit in ['apple', 'banana', 'cherry']:
print(fruit)
6. while Loop
Executes as long as a condition is true.
Example :
1.
i=0
while i < 5:
print(i)
i += 1
2.
i=0
while i < 10:
if i % 2 == 0:
print(i)
i += 1
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
7. Nested Loops
Using a loop inside another loop.
Example :
1.
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
2.
for i in range(2):
for j in range(3):
print(i, j)
3.
for x in range(1, 4):
for y in range(1, x+1):
print('*', end=' ')
print()
4.
names = ['A', 'B']
for name in names:
for i in range(2):
print(name, i)
8. break Statement
Used to exit the loop prematurely.
Example :
1.
for i in range(10):
if i == 5:
break
print(i)
2.
i=1
while i < 10:
if i == 5:
break
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
print(i)
i += 1
3.
for ch in 'python':
if ch == 'h':
break
print(ch)
9. continue Statement
Skips the current iteration and moves to the next.
Example :
1.
for i in range(5):
if i == 2:
continue
print(i)
2.
i=0
while i < 5:
i += 1
if i == 3:
continue
print(i)
3.
for letter in 'python':
if letter == 't':
continue
print(letter)
10. pass Statement
A null statement used as a placeholder. It does nothing.
Example :
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
1.
for i in range(3):
if i == 1:
pass
print(i)
2.
def func():
pass
print("Function defined but not implemented.")
3.
class Demo:
pass
print("Empty class defined using pass")
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University