HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Programming
  2. /Python Basics
  3. /Loops

Loops

Python Programming · Middle Stage (6–8) · 3 courses, 34 worked problems, 69 practice questions

Computers repeat steps quickly and without mistakes. A loop tells Python to run a block of code again and again. A for loop repeats once for each item in a list, or for each number from range(), so for i in range(5): runs five times. A while loop repeats as long as a condition is True, which suits cases where you do not know the count in advance. The word break stops a loop early, and continue skips to the next turn. Loops make tables, add up totals and run step-by-step simulations.

With practice you will be able to

  • Write a for loop over a list or a range()
  • Write a while loop that stops when a condition fails
  • Use a loop to build a running total or a table

On the syllabus of

  • CBSE / State Class 12
  • Python Certification
  • AP
  • IB
  • A-Level
  • IGCSE

Lessons

  • Lists: Many Values in One VariableAfter b = a, why does changing b also change a?Middle Stage (6–8)
  • Repeat with for: LoopsWhy does range(1, 6) stop at 5? Can a loop add 1 to 100 for you?Middle Stage (6–8)
  • Repeat Until: while LoopsWhy does a loop sometimes never stop? How do you leave a loop early?Middle Stage (6–8)

Practice questions

Choose an answer, then check it. Each one shows a full explanation.

A for loop walks through a string and prints each character twice. for ch in "abc": print(ch * 2, end="-") print() What does the program print?

A while loop walks through a word with an index. After it finds the letter a, it jumps two places ahead. s = "aardvark" i = 0 found = 0 while i < len(s): if s[i] == "a": found += 1 i += 2 else: i += 1 print(found, i) What is printed?

Two nested counting loops add up numbers. The inner range starts at the outer variable, so the inner loop gets shorter each time round. total = 0 for i in range(1, 4): for j in range(i, 4): total += j print(total) What does the program print?

A loop counts vowels in a short phrase. It skips spaces, it can stop early, and it has an else block. count = 0 for ch in "sky is blue": if ch == " ": continue if ch in "aeiou": count += 1 if ch == "u": break else: count = 100 print(count) What does the program print?

Starting from ten, the program halves the number when it is even, and triples it and adds one when it is odd, counting the steps until it reaches one. The while statement has an else part. n = 10 steps = 0 while n != 1: if n % 2 == 0: n //= 2 else: n = 3 * n + 1 steps += 1 else: print("done", end=" ") print(steps) What does the program print?

Two loops build a list of two-digit codes. The inner loop uses both continue and break. codes = [] for i in range(3): for j in range(3): if j == i: continue if j > i + 1: break codes.append(str(i) + str(j)) print(codes) What does the program print?

The program should spell a word backwards, one letter at a time. The slice in the loop header is missing, shown as ____. word = "loops" result = "" for ch in ____: result += ch print(result) Which slices complete the program so that its output is spool? There may be more than one.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

The first line typed says how many words follow. The user types 3, then pear, fig and plum, one on each line, and the program keeps the longest word. n = int(input()) longest = "" for i in range(n): word = input() if len(word) >= len(longest): longest = word print(longest, n) What is printed?

A program searches a list of tuples for a number whose double is 5. If the search fails, it reports the last tuple it saw. pairs = [(1, "a"), (2, "b"), (3, "c")] for n, ch in pairs: if n * 2 == 5: print("found", ch) break else: print("none", n, ch) What does the program print?

A shop keeps its stock in a dictionary. A loop lists the items that are running low. stock = {"pen": 0, "ink": 4, "pad": 1} need = [] for item, qty in stock.items(): if qty > 1: continue need.append(item) print(need, len(stock)) What does the program print?

All 69 practice questions on Loops →

Worked problems

Each one has a hint, the key idea and a full worked solution.

  • The list is marks = [45, 78, 90, 62, 38, 85, 71]…Easy
  • How many numbers does each row have, and which is the last number? …Easy
  • This program should show the sum of the numbers 1 to 4, which is 10…Medium
  • Print a rectangle of stars with 4 rows and 7 columns…Easy
  • How many times does the line print("x") run? …Medium
  • Asha saves 45 rupees every week, and has 0 at the start…Easy
  • Find the first number from 100 upwards that can be divided by both 6 and 8…Easy
  • Write a program that asks for a password, again and again, until the user types python123…Medium
  • This loop should show the even numbers from 2 up to and including 10…Medium
  • Find the total and the average of the marks 72, 85, 64, 90 and 58.Medium
  • Find the highest mark, the lowest mark and the difference between them for [72, 85, 64, 90, 58].Easy
  • A mark of 60 or more is a pass. How many of [72, 85, 64, 90, 58] passed?Medium

All 34 worked problems on Loops →

Python labs

  • Pass or fail lists
  • Say it five times
  • Which numbers?
  • The sum of the first 100
  • Count and multiply
  • Draw the triangle

Related topics

  • Python Basics
  • Getting Started with Python
  • Variables and Data Types
  • Operators and Expressions
  • Input and Output
  • Strings
  • Conditionals: if, elif, else
  • Lists
  • Functions
  • Mini Projects