Python · Python Fundamentals

Loops — Practice Questions

50 questions. Try each one yourself before checking the answer.

Q1forEasy

Print numbers 1 to 10 using a for loop and range().

Q2range()Easy

Print numbers 10 down to 1 using range() with a negative step.

Q3whileEasy

Print numbers 1 to 5 using a while loop.

Q4forEasy

Print each character of "Python" on its own line using a for loop.

Q5forEasy

Given fruits = ["apple", "banana", "cherry"], print each item using a for loop.

Q6range()Easy

Print all even numbers from 2 to 20 using range() with a step of 2.

Q7whileEasy

Count down from 5 to 1 using a while loop, then print "Liftoff!" after the loop ends.

Q8forEasy

Sum the numbers from 1 to 100 using a for loop and an accumulator variable, then print the total.

Q9Nested LoopsEasy

Print a 3x3 grid of * characters using nested for loops (3 rows, each with 3 stars printed on one line).

Q10breakEasy

Loop through numbers 1 to 10, printing each one, but stop completely once you reach 5.

Q11continueMedium

Loop through numbers 1 to 10 and print every number except multiples of 3.

Q12passMedium

Loop through numbers 1 to 5. For the number 3, do nothing (pass) as a placeholder for future logic; print every other number normally.

Q13whileMedium

Repeatedly ask the user to enter a number, adding it to a running total, until they type "done". Print the final total.

Q14Loop elseMedium

Given names = ["Alice", "Bob", "Charlie"], search for "Dave" using a for/else loop, printing "Found" only if a break occurred, otherwise "Not found" from the else block.

Q15Loop elseMedium

Using a while/else loop, count up from 1 while a number stays below 5, printing each value; add an else block that prints "Loop completed normally" (it will, since there's no break here).

Q16Nested LoopsMedium

Print the full multiplication table from 1x1 to 5x5 using nested for loops, one row per outer iteration.

Q17enumerate()Medium

Given colors = ["red", "green", "blue"], print each color with its index (starting from 1) using enumerate().

Q18zip()Medium

Given names = ["Alice", "Bob"] and scores = [85, 92], print each name with their score using zip().

Q19range()Medium

Sum only the odd numbers from 1 to 50 using range() with a step of 2, and print the total.

Q20Nested LoopsMedium

Given grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], search for the value 5 using nested for loops with break, printing "Found at row X, col Y".

Q21Real-WorldMedium

Ask the user for a sentence and count the number of vowels in it using a for loop over the characters.

Q22AlgorithmicMedium

Ask for a number and reverse its digits using a while loop (via % and //), then print the reversed number.

Q23AlgorithmicMedium

Ask for a number and compute the sum of its digits using a while loop.

Q24AlgorithmicMedium

Ask for a number and compute its factorial using a for loop.

Q25AlgorithmicMedium

Ask for a number and check whether it's prime using a for/else loop.

Q26AlgorithmicMedium

Ask for two numbers and find their GCD using a while loop (Euclidean algorithm).

Q27Real-WorldMedium

Given numbers = [4, 7, 2, 9, 10, 3, 6], count how many are even using a for loop.

Q28AlgorithmicMedium

Given numbers = [23, 67, 12, 89, 45], find the maximum value using a for loop, without using the built-in max().

Q29forMedium

Given numbers = [1, 2, 3, 4, 5], build a new list containing the square of each number, using a for loop and .append().

Q30whileMedium

Ask the user to enter 5 numbers one at a time, printing the running average after each entry.

Q31AlgorithmicHard

Ask for n and print the first n numbers of the Fibonacci sequence using a while loop.

Q32AlgorithmicHard

Ask for a 3-digit number and check whether it's an Armstrong number (sum of the cube of each digit equals the number itself), using a while loop.

Q33AlgorithmicHard

Ask for a number and check whether it's a perfect number (the sum of its proper divisors equals the number, e.g. 6 = 1+2+3), using a for loop.

Q34Nested LoopsHard

Ask for n and print all prime numbers from 2 up to n, using a nested loop where the outer loop picks candidates and the inner loop (with break/else) tests divisibility.

Q35FormattingHard

Ask for a number and print its full multiplication table from 1 to 10 using a for loop, with both operands and the result aligned in neat columns.

Q36Nested LoopsHard

Given matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], compute and print the sum of all elements using nested for loops.

Q37AlgorithmicHard

Given numbers = [12, 45, 2, 41, 31, 10, 8], find the second-largest value using a single for loop pass (no sorting).

Q38Text ProcessingHard

Given text = "the cat sat on the mat with the hat" and target = "the", count how many times target appears as a whole word by looping over text.split().

Q39break / continue / passHard

Loop through numbers 1 to 10. Use continue to skip even numbers, pass as a placeholder for the number 7, and break to stop entirely once you reach 9. Print every number that's actually printed.

Q40whileHard

Use an infinite while True loop to keep asking the user for a number until they enter a positive one, then break and print it.

Q41Mini-ProjectMini-Project

Build a "Number Guessing Game". The secret number is 42. Loop asking the user to guess until they get it right, telling them "Too high" or "Too low" each time, and finally print how many attempts it took.

Q42Mini-ProjectMini-Project

Build a "Menu-Driven Calculator Loop". Repeatedly show a menu (add/subtract/multiply/divide/exit), perform the chosen operation on two numbers, and keep looping until the user types "exit".

Q43Mini-ProjectMini-Project

Build a "Multiplication Table Generator". Ask the user for a number and a range limit, then print that number's table from 1 up to the limit.

Q44Mini-ProjectMini-Project

Build a "Simple ATM Loop". Starting with balance = 1000, repeatedly show a menu (check/deposit/withdraw/exit), updating and reflecting the balance across iterations until the user exits.

Q45Mini-ProjectMini-Project

Build an "Attendance Tracker". Ask for the number of students, then loop that many times collecting "present"/"absent" for each, and print a final tally of both counts.

Q46InterviewInterview

Demonstrate an infinite-loop bug: write a while loop that never updates its condition variable, explain why it hangs, then show the corrected version.

Q47InterviewInterview

Given values = [5, -3, 8, 0, -1, 9], loop through them: use continue to skip negative numbers, pass as a placeholder for 0 (a "reserved" case with no logic yet), and break the moment you encounter a number greater than 8. Print every value that gets printed.

Q48InterviewInterview

Given usernames = ["alice", "bob", "carol"] and a banned_word = "admin", use a for/else loop to print "Clean list" only if no username matches the banned word — without using a separate flag variable.

Q49InterviewInterview

Given numbers = [3, 7, 12, 19, 25, 40], search for 19 two ways: once looping through every element regardless, and once stopping with break as soon as it's found. Explain in a comment why the break version does less work on average.

Q50CapstoneInterview

Build a "Loop Mastery Report". Using a sentinel-controlled while loop, collect numbers from the user until they type "done". Without using sum(), min(), or max(), manually compute and print the count, total, average, minimum, maximum, and how many were even vs. odd.

Still stuck on something?

Book a free 1-on-1 session and we'll work through it together.

Book a Free Session