Loops — Practice Questions
50 questions. Try each one yourself before checking the answer.
Print numbers 1 to 10 using a for loop and range().
Print numbers 10 down to 1 using range() with a negative step.
Print numbers 1 to 5 using a while loop.
Print each character of "Python" on its own line using a for loop.
Given fruits = ["apple", "banana", "cherry"], print each item using a for loop.
Print all even numbers from 2 to 20 using range() with a step of 2.
Count down from 5 to 1 using a while loop, then print "Liftoff!" after the loop ends.
Sum the numbers from 1 to 100 using a for loop and an accumulator variable, then print the total.
Print a 3x3 grid of * characters using nested for loops (3 rows, each with 3 stars printed on one line).
Loop through numbers 1 to 10, printing each one, but stop completely once you reach 5.
Loop through numbers 1 to 10 and print every number except multiples of 3.
Loop through numbers 1 to 5. For the number 3, do nothing (pass) as a placeholder for future logic; print every other number normally.
Repeatedly ask the user to enter a number, adding it to a running total, until they type "done". Print the final total.
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.
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).
Print the full multiplication table from 1x1 to 5x5 using nested for loops, one row per outer iteration.
Given colors = ["red", "green", "blue"], print each color with its index (starting from 1) using enumerate().
Given names = ["Alice", "Bob"] and scores = [85, 92], print each name with their score using zip().
Sum only the odd numbers from 1 to 50 using range() with a step of 2, and print the total.
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".
Ask the user for a sentence and count the number of vowels in it using a for loop over the characters.
Ask for a number and reverse its digits using a while loop (via % and //), then print the reversed number.
Ask for a number and compute the sum of its digits using a while loop.
Ask for a number and compute its factorial using a for loop.
Ask for a number and check whether it's prime using a for/else loop.
Ask for two numbers and find their GCD using a while loop (Euclidean algorithm).
Given numbers = [4, 7, 2, 9, 10, 3, 6], count how many are even using a for loop.
Given numbers = [23, 67, 12, 89, 45], find the maximum value using a for loop, without using the built-in max().
Given numbers = [1, 2, 3, 4, 5], build a new list containing the square of each number, using a for loop and .append().
Ask the user to enter 5 numbers one at a time, printing the running average after each entry.
Ask for n and print the first n numbers of the Fibonacci sequence using a while loop.
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.
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.
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.
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.
Given matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], compute and print the sum of all elements using nested for loops.
Given numbers = [12, 45, 2, 41, 31, 10, 8], find the second-largest value using a single for loop pass (no sorting).
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().
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.
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.
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.
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".
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.
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.
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.
Demonstrate an infinite-loop bug: write a while loop that never updates its condition variable, explain why it hangs, then show the corrected version.
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.
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.
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.
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