Python ยท Python Fundamentals

Loops: Practice Questions

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

Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.

Q1forEasyMust Do

Print the numbers 1 to 10, each on its own line, using a for loop and range().

Q2range()Easy

Print the numbers 5 to 15 using range() with a start and a stop.

Q3range()Easy

Print the numbers 0 to 4 using range() with only one argument.

Q4range()EasyMust Do

Print the numbers 10 down to 1 using a negative step.

Q5range()Easy

Print all the even numbers from 2 to 20 using a step of 2.

Q6forEasyMust Do

Print each character of "Python" on its own line.

Q7forEasy

Print the 5 times table from 5 x 1 to 5 x 10, one line per row.

Q8whileEasyMust Do

Print the numbers 1 to 5 using a while loop.

Q9whileEasy

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

Q10whileEasy

Starting from total = 100, keep halving it with a while loop while it stays above 1, printing each value to 2 decimal places.

Q11AccumulatorEasyMust Do

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

Q12AccumulatorEasy

Multiply the numbers 1 to 5 together using an accumulator, then print the result. Note why this accumulator must start at 1.

Q13AccumulatorEasy

Count how many characters are in "programming" using a loop and a counter, without using len().

Q14Nested LoopsEasyMust Do

Using nested for loops, print every pair (i, j) where both run from 1 to 3. There should be 9 lines.

Q15Nested LoopsEasy

Using nested loops, count how many times the inner body runs when the outer loop goes 1 to 4 and the inner goes 1 to 5. Print the final count.

Q16breakEasyMust Do

Loop through the numbers 1 to 10, printing each, but stop completely as soon as you reach 5.

Q17continueEasyMust Do

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

Q18passEasy

Loop through the numbers 1 to 5. For the number 3 do nothing at all using pass, and print every other number.

Q19break vs continueEasy

Print the numbers 1 to 6 twice: once using continue to skip 3, and once using break to stop at 3, so the difference is obvious.

Q20Loop elseEasy

Loop through the numbers 1 to 5 printing each, and add an else block that prints "Finished normally". Note that it runs because no break happened.

Q21Loop elseEasy

Repeat the previous loop but break at 3. Show that the else block is now skipped.

Q22Loop elseEasy

Given word = "rhythm", search its characters for a vowel. Print "Has a vowel" and break if you find one, and use for/else to print "No vowels found" otherwise.

Q23while elseEasy

Count up from 1 while the value stays below 5, printing each, and add a while/else block that prints "Loop completed normally".

Q24if inside loopEasy

Loop through the numbers 1 to 10 and print "<n> is even" or "<n> is odd" for each.

Q25range(len())Easy

Given word = "Python", print each character together with its index, by looping over range(len(word)).

Q26AccumulatorMedium

Add up only the odd numbers from 1 to 50 and print the total.

Q27CountingMedium

Ask the user for a sentence and count how many vowels it contains.

Q28CountingMedium

Ask for a sentence and count how many characters are digits, using a loop and .isdigit().

Q29String BuildingMedium

Build a single string containing the squares of 1 to 5 separated by spaces, using +=, then print it tidied up.

Q30Nested LoopsMediumMust Do

Print the full multiplication table from 1x1 to 5x5 as a neat grid, one row per outer pass.

Q31Manual CounterMedium

Print each character of "Python" with a position number starting at 1, by keeping your own counter variable.

Q32range(len())Medium

Given first = "abc" and second = "xyz", print their characters side by side as a - x, b - y, c - z.

Q33Min/Max TrackingMediumMust Do

Ask the user for 5 numbers one at a time and print the largest, without using max().

Q34Min/Max TrackingMedium

Ask for 5 numbers and print the smallest, without using min().

Q35whileMedium

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

Q36Sentinel LoopMediumMust Do

Keep asking the user for numbers, adding each to a running total, until they type "done". Print the final total.

Q37while TrueMedium

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

Q38Nested LoopsMedium

Using nested loops over i and j from 1 to 9, find and print the first pair whose product is 24, then stop the inner loop. Observe that the outer loop keeps going.

Q39Nested LoopsMedium

Fix the previous question so that both loops stop at the very first match, using a flag variable.

Q40Digit ManipulationMediumMust Do

Ask for a number and reverse its digits using a while loop with % and //.

Q41Digit ManipulationMedium

Ask for a number and print the sum of its digits.

Q42Digit ManipulationMedium

Ask for a number and count how many digits it has, using a loop rather than len(str(n)).

Q43Arithmetic AlgorithmsMediumMust Do

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

Q44Arithmetic AlgorithmsMedium

Ask for a base and an exponent and compute the power using repeated multiplication, without **.

Q45Arithmetic AlgorithmsMedium

Ask for two numbers and multiply them using repeated addition, without *.

Q46Number TheoryMedium

Ask for two numbers and find their GCD using the Euclidean algorithm.

Q47Number TheoryMedium

Ask for two numbers and find their LCM, using the GCD you just computed.

Q48SequencesMedium

Ask for n and print the first n numbers of the Fibonacci sequence.

Q49Loop elseMediumMust Do

Ask for a number and check whether it is prime, using a for/else loop.

Q50String BuildingMedium

Ask for a word and print it reversed using a loop, without using slicing.

Q51PrimesMediumMust Do

Ask for n and print every prime number from 2 up to n, using nested loops with break/else.

Q52Number PropertiesMedium

Ask for a number and check whether it is a perfect number โ€” one whose proper divisors add up to itself, like 6 = 1+2+3.

Q53Number PropertiesMedium

Ask for a 3-digit number and check whether it is an Armstrong number โ€” the cubes of its digits adding up to the number itself.

Q54Number PropertiesMedium

Ask for a number and check whether it is a palindrome, using only arithmetic โ€” no strings or slicing.

Q55Text ProcessingMediumMust Do

Ask for a sentence and count how many words it contains without using .split(). Walk it character by character and count each move from a space into a non-space, so repeated spaces don't inflate the count.

Q56CountingMedium

Ask for a sentence and count how many characters are uppercase and how many are lowercase.

Q57Digit ManipulationMedium

Ask for a number and print the sum of only its even digits.

Q58Digit ManipulationMedium

Ask for a number and print its largest digit.

Q59SequencesMedium

Ask for a number and print its Collatz sequence: halve it when even, otherwise triple it and add 1, until it reaches 1. Print how many steps it took.

Q60Real-WorldMedium

Ask for a number and a limit, then print that number's times table up to the limit.

Q61FormattingMediumMust Do

Ask for a number and print its full multiplication table from 1 to 10, with the operands and results lined up in neat columns.

Q62Real-WorldMedium

Ask for a principal, a rate and a number of years, then print the simple interest earned at the end of each year.

Q63Real-WorldMedium

Ask for a principal, a rate and a number of years, then print the compound balance at the end of each year.

Q64Real-WorldMedium

Ask for a savings goal, a starting balance and a monthly deposit. Loop month by month until the goal is reached, then print how many months it took.

Q65Real-WorldMedium

Ask how many students there are, then loop that many times collecting present/absent, and print a final tally of both.

Q66Real-WorldMediumMust Do

Ask how many students sat an exam, collect each mark, and print how many passed (40 or more) and how many failed.

Q67Real-WorldMediumMust Do

Ask for a number of days, collect a temperature for each, and print the highest, the lowest and the average.

Q68continueMedium

Ask for 6 numbers, using continue to skip any negative value, and print the average of only the numbers you actually accepted.

Q69CountingMedium

Ask for a threshold and then 5 numbers, and print how many of them were above the threshold.

Q70Real-WorldMedium

Ask for 4 words one at a time and print the longest one, without storing them all.

Q71DebuggingHardMust Do

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

Q72Off by OneHardMust Do

Print range(1, 5) and range(1, 6) as loops and explain why printing 1 to 5 needs range(1, 6).

Q73Loop VariableHard

Show that reassigning the loop variable inside a for loop does not change how many times the loop runs.

Q74break / continue / passHard

Loop through the numbers 1 to 10 using all three: continue to skip evens, pass as a placeholder for 7, and break on reaching 9. Predict the output first.

Q75Loop elseHard

Given password = "SecurePass", use a for/else loop to report whether it contains a digit โ€” without a separate flag variable.

Q76Nested LoopsHard

Count and print exactly how many times the innermost line runs in a triple-nested loop of 3, 4 and 5, and state the general rule.

Q77Float AccumulationHard

Add 0.1 to a total ten times in a loop and compare the result with 1.0. Explain the outcome and show the safe way to test it.

Q78Empty LoopsHard

Show three range() calls that produce no iterations at all, and explain each one.

Q79range()Hard

Print the numbers 10 down to 1 correctly, then show the two common broken attempts and explain what each gets wrong.

Q80Min/Max TrackingHard

Ask how many numbers the user wants to enter, then find the second largest in a single pass, with no sorting.

Q81PrimesHard

Ask for n and count how many primes there are up to n, checking divisors only up to the square root of each candidate. Explain why that is enough.

Q82Digit ManipulationHard

Ask for a number and a digit, and count how many times that digit appears in the number, using arithmetic only.

Q83Base ConversionHard

Ask for a positive number and convert it to binary using a loop, without using bin() or a format specifier.

Q84DebuggingHard

This should print the sum of 1 to 5 but prints something far too small. Find and fix the bug.

for i in range(1, 6):
    total = 0
    total += i
print(total)
Q85DebuggingHardMust Do

This nested loop should print a 3x3 grid of coordinates but prints the same row three times. Find and fix the bug.

for i in range(3):
    for j in range(3):
        print(f"({i}, {i})", end=" ")
    print()
Q86Mini-ProjectMini-ProjectMust Do

Build a Number Guessing Game. The secret is 42. Keep asking until the player gets it, saying "Too high" or "Too low" each time, and report how many attempts it took.

Q87Mini-ProjectMini-Project

Extend the guessing game with a limit of 5 attempts. Use for/else so the "you lost" message appears only when the player never guessed correctly.

Q88Mini-ProjectMini-ProjectMust Do

Build a Menu-Driven Calculator that keeps running until the user chooses to exit, handling division by zero safely.

Q89Mini-ProjectMini-Project

Build an ATM Loop. Start with a balance of 1000 and keep offering check, deposit, withdraw and exit until the user leaves, keeping the balance correct across iterations.

Q90Mini-ProjectMini-Project

Build a Password Retry System. Allow 3 attempts at the password "python123", then lock out. Use for/else for the lockout message.

Q91Mini-ProjectMini-Project

Build a Shopping Cart Loop. Keep asking for an item name and price until the user types done, then print how many items were bought, the total, and the average price.

Q92Mini-ProjectMini-Project

Build a Multi-Question Quiz. Ask 3 questions in a loop-friendly way, track the score, and print a percentage and grade at the end.

Q93Mini-ProjectMini-Project

Build a Times Table Grid. Ask for a size n and print a full n-by-n multiplication grid with a header row and column, all neatly aligned.

Q94Mini-ProjectMini-Project

Build a Star Rating Collector. Ask how many reviews there are, collect a 1โ€“5 rating for each (rejecting anything out of range without counting it), then print the average and a star bar.

Q95Mini-ProjectMini-Project

Build a Digit Analyser. Ask for a number and report, using loops only: how many digits it has, their sum, their product, the largest, the smallest, and how many are even.

Q96InterviewInterview

Explain when you should reach for a while loop and when a for loop is the right tool, demonstrating one clear case of each.

Q97InterviewInterview

Loop over the characters of "5a3!7z9". Skip letters with continue, use pass as a reserved placeholder for punctuation, and break at the first digit above 8. Predict the output first.

Q98InterviewInterview

Show the same search written two ways โ€” once with a flag variable, once with for/else โ€” and say which you would submit in a code review.

Q99InterviewInterview

Search "programming" for the letter "g" twice: once checking every character regardless, and once stopping at the first match. Count the comparisons each version makes and explain the difference.

Q100CapstoneInterviewMust Do

Build a Loop Mastery Report โ€” the fullest demonstration of this topic. Using a sentinel-controlled while loop, collect numbers until the user types done. Without sum(), min() or max(), compute and print the count, total, average, minimum, maximum, the even and odd counts, and how many were above the average.

Still stuck on something?

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

Book a Free Session