CSE 0613/2106C — Python Programming Lab
Exam Practice: Common Problems (Loops, Conditionals, Nested Loops & Nested Conditionals)
1. Variables & Input
1.1. Swap two numbers using a third variable
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a = b
b = temp
print("After swap: a =", a, ", b =", b)
Output:
Enter first number: 5
Enter second number: 9
After swap: a = 9 , b = 5
1.2. Swap two numbers without a third variable
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("After swap: a =", a, ", b =", b)
Output:
Enter first number: 5
Enter second number: 9
After swap: a = 9 , b = 5
1.3. Simple Interest calculation
p = float(input("Enter Principal: "))
r = float(input("Enter Rate: "))
t = float(input("Enter Time: "))
si = (p * r * t) / 100
print(f"Simple Interest = {si}")
Output:
Enter Principal: 1000
Enter Rate: 5
Enter Time: 2
Simple Interest = 100.0
2. Formatted Print (f-strings)
2.1. Print name and age using f-string
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"My name is {name} and I am {age} years old")
Output:
Enter your name: Rafi
Enter your age: 20
My name is Rafi and I am 20 years old
2.2. Percentage of marks using f-string
m1 = int(input("Marks 1: "))
m2 = int(input("Marks 2: "))
m3 = int(input("Marks 3: "))
total = m1 + m2 + m3
percent = total / 3
print(f"Total = {total}, Percentage = {percent:.2f}%")
Output:
Marks 1: 80
Marks 2: 90
Marks 3: 70
Total = 240, Percentage = 80.00%
3. Loops (for and while)
3.1. Sum of first n natural numbers
n = int(input("Enter n: "))
total = 0
for i in range(1, n+1):
total += i
print("Sum =", total)
Output:
Enter n: 5
Sum = 15
3.2. Factorial of a number
n = int(input("Enter n: "))
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial =", fact)
Output:
Enter n: 5
Factorial = 120
3.3. Print even numbers between 1 and n (while loop)
n = int(input("Enter n: "))
i = 1
while i <= n:
if i % 2 == 0:
print(i)
i += 1
Output:
Enter n: 10
2
4
6
8
10
3.4. Check if a number is prime
n = int(input("Enter n: "))
is_prime = True
if n < 2:
is_prime = False
for i in range(2, n):
if n % i == 0:
is_prime = False
break
if is_prime:
print(n, "is Prime")
else:
print(n, "is not Prime")
Output:
Enter n: 7
7 is Prime
3.5. Skip multiples of 3 using continue
n = int(input("Enter an integer number: "))
for i in range(1, n):
if i % 3 == 0:
continue
print(i)
Output:
Enter an integer number: 10
1
2
4
5
7
8
4. Conditional Statements (if, elif, else)
4.1. Check even or odd
n = int(input("Enter an integer number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
Output:
Enter an integer number: 7
Odd
4.2. Grade calculator using elif
marks = int(input("Enter marks: "))
if marks >= 80:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")
Output:
Enter marks: 75
Grade B
4.3. Check leap year
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
Output:
Enter year: 2024
Leap Year
5. Nested Loops
5.1. Print a star pyramid pattern
rows = int(input("Enter number of rows: "))
for i in range(1, rows+1):
for j in range(1, i+1):
print("*", end=" ")
print()
Output:
Enter number of rows: 4
*
* *
* * *
* * * *
5.2. Print number pattern
rows = int(input("Enter number of rows: "))
for i in range(1, rows+1):
for j in range(1, i+1):
print(j, end=" ")
print()
Output:
Enter number of rows: 4
1
1 2
1 2 3
1 2 3 4
5.3. Print coordinate pairs (rows x columns)
rows = int(input("Rows: "))
cols = int(input("Columns: "))
for i in range(1, rows+1):
for j in range(1, cols+1):
print(f"({i},{j})", end=" ")
print()
Output:
Rows: 3
Columns: 3
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
5.4. Multiplication table for numbers 1 to 5 (grid)
for i in range(1, 6):
for j in range(1, 6):
print(i*j, end="\t")
print()
Output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
6. Nested Conditional Statements
6.1. Check even/odd and positive/negative together
n = int(input("Enter a number: "))
if n >= 0:
if n % 2 == 0:
print("Positive Even")
else:
print("Positive Odd")
else:
if n % 2 == 0:
print("Negative Even")
else:
print("Negative Odd")
Output:
Enter a number: 7
Positive Odd
6.2. Simple login check (username then password)
username = input("Enter username: ")
if username == "admin":
password = input("Enter password: ")
if password == "1234":
print("Login Successful")
else:
print("Wrong Password")
else:
print("Invalid Username")
Output:
Enter username: admin
Enter password: 1234
Login Successful
6.3. Eligibility check (age and ID)
age = int(input("Enter your age: "))
if age >= 18:
has_id = input("Do you have an ID card? (yes/no): ")
if has_id == "yes":
print("Eligible to vote")
else:
print("Get an ID card first")
else:
print("Not eligible - underage")
Output:
Enter your age: 20
Do you have an ID card? (yes/no): yes
Eligible to vote
Tip: A nested if checks conditions in sequence (one inside another), which differs from combining conditions with
'and' in a single if — both forms are common exam questions.
Good luck with your exam!