Python, Concept by Concept › For & Range
Exercises: For & Range
Core: Exercises 1–13 are the main path. More Practice: Exercises 14–26 add variety. Challenges: Exercises 27–31 are optional stretch work; no core exercise depends on them.
For every exercise, use the given values, follow the specification, and produce the exact output shown. Compare every space and line with the worked sample. Build each art row as a string, then print it once. Do not modify a for variable inside its loop body.
Exercise 1: Triangle Number
A number lab wants the total from 1 through a much larger upper bound.
Specification: Given n = 250, start total at 0 and add each integer from 1 through n with a for loop. Print Total: <total>.
Worked sample — given values:
n = 250
Expected output:
Total: 31375
Real program
The real program reads one upper bound with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 2: Stop-Bound Inspector
A calibration visits every integer from 3 through 8. The report records only the final value visited.
Specification: Start last_seen at 0. Use for number in range(3, 9): and save each visited number in last_seen. After the loop, print exactly Last: <last_seen>. Remember that the stop value is not included.
Worked sample — required interval:
3 through 8
Expected output:
Last: 8
Real program
The real program reads the start and end of the interval with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 3: Divisible-by-Four Census
A scoreboard marks every game number divisible by 4 in the first 26 games.
Specification: Use for number in range(1, 27): to check 1 through 26. Add 1 to count only when the number is divisible by 4. After the loop, print exactly Count: <count>.
Worked sample — required interval:
1 through 26
Expected output:
Count: 6
Real program
The real program reads n, then n game numbers on separate lines with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 4: FizzBuzz Board
A number board replaces multiples of 3 with Fizz, multiples of 5 with Buzz, and multiples of both with FizzBuzz.
Specification: Use a for loop over 1 through 18. Use one if/elif/elif/else ladder, checking divisibility by 15 first, then 3, then 5. Save exactly one result in line and print it once per pass.
Worked sample — required interval:
1 through 18
Expected output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
Real program
The real program reads one upper bound with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 5: Prime-Flag Test
A number is prime when no integer from 2 through one less than the number divides it exactly.
Specification: Given an integer n >= 2, start is_prime as True. Loop over range(2, n). When a divisor is found, set the flag to False and break. After the loop, print exactly <n> is prime: <is_prime>.
Worked sample — given values:
n = 35
Expected output:
35 is prime: False
Notice
If n were 2, range(2, 2) would be empty and the True flag would stay correct.
Real program
The real program reads one integer n with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 6: Four-by-Four Times Table
A study card needs one complete string per multiplication-table row.
Specification: Given table_size = 4, use nested for loops with outer variable row and inner variable col, each visiting 1 through table_size. Start row_text as an empty string for each outer pass. Accumulate the products with one space between them, then print the completed row after the inner loop.
Worked sample — given values:
table_size = 4
Expected output:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Real program
The real program reads the table size with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 7: Nested Classification Count
A game master wants numbers that pass two tests: they must be even, and then they must also be divisible by 3.
Specification: Use for number in range(1, 13):. Inside the loop, first use if number % 2 == 0:. Inside that if, place another if that tests divisibility by 3 and adds 1 to count. After the loop, print exactly Count: <count>. This must be an if inside an if inside the for.
Worked sample — required interval:
1 through 12
Expected output:
Count: 2
Real program
The real program reads n, then n numbers on separate lines with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 8: Growing Signal Triangle
A signal display has one star in row 1, two in row 2, and so on through the chosen height.
Specification: Given height = 4, use nested for loops named row and col. For each row, start row_text as an empty string. Add one "*" per inner pass, then print the completed row once. Do not use string repetition.
Worked sample — given values:
height = 4
Expected output:
*
**
***
****
Real program
The real program reads the triangle height with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 9: Factorial over a Range
A combination counter multiplies the integers from 1 through a small number.
Specification: Given an integer n from 1 through 10, initialize product to 1. Use for number in range(1, n + 1): to update the product. After the loop, print exactly Product: <product>.
Worked sample — given values:
n = 7
Expected output:
Product: 5040
Real program
The real program reads one integer n with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 10: Countdown by Twos
A timer reports every even number as it counts down.
Specification: Given start = 20, use range(start, 0, -2) and build one space-separated output line.
Worked sample — given values:
start = 20
Expected output:
20 18 16 14 12 10 8 6 4 2
Real program
The real program reads the starting number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 11: Inverted Triangle
A display shrinks its star rows from four to one.
Specification: Given height = 4, use a negative-step range and string repetition. Build and print each row without trailing spaces.
Worked sample — given values:
height = 4
Expected output:
****
***
**
*
Real program
The real program reads the height with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 12: Right-Aligned Triangle
A display moves the growing triangle against its right edge.
Specification: Given height = 4, add height - row leading spaces and row stars to each row.
Worked sample — given values:
height = 4
Expected output:
*
**
***
****
Real program
The real program reads the height with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: four lines; rows keep their leading spaces exactly as shown
Exercise 13: Hollow Box
A frame needs stars on its edges and spaces inside.
Specification: Given width = 6 and height = 4, use nested loops. In the outer if, test whether the row is the top or bottom row; in its else, test whether the column is the left or right edge. Build each row as one string.
Worked sample — given values:
width = 6
height = 4
Expected output:
######
# #
# #
######
Real program
The real program reads the width and height on separate lines with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: four lines; the inside spaces are kept exactly as shown; rows keep their leading spaces exactly as shown
Exercise 14: More Practice: Checkerboard
A board alternates two marks in both directions.
Specification: Given size = 4, use nested loops and (row + col) % 2 to choose # or .. Build each row as a string.
Worked sample — given values:
size = 4
Expected output:
#.#.
.#.#
#.#.
.#.#
Real program
The real program reads the board size with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 15: More Practice: Pyramid
A stage display grows by two stars per row.
Specification: Given height = 4, build each row with height - row spaces and 2 * row - 1 stars.
Worked sample — given values:
height = 4
Expected output:
*
***
*****
*******
Real program
The real program reads the height with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: four lines; rows keep their leading spaces exactly as shown
Exercise 16: More Practice: Diamond
A diamond grows to its widest row and then shrinks.
Specification: Given size = 3, build the upper half including the middle, then the lower half. Each row uses " " * (size - row) + "*" * (2 * row - 1).
Worked sample — given values:
size = 3
Expected output:
*
***
*****
***
*
Real program
The real program reads the size with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: five lines; rows keep their leading spaces exactly as shown
Exercise 17: More Practice: Tree with Trunk
A tree adds a trunk below a centered crown.
Specification: Given height = 4, build the four pyramid rows, then print a trunk row with height - 1 leading spaces and |.
Worked sample — given values:
height = 4
Expected output:
*
***
*****
*******
|
Real program
The real program reads the height with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: five lines; rows keep their leading spaces exactly as shown
Exercise 18: More Practice: Floyd's Triangle
A number triangle continues its count across rows.
Specification: Given rows = 4, start a number at 1. Use nested loops to place one more number on each successive row, with one space between numbers.
Worked sample — given values:
rows = 4
Expected output:
1
2 3
4 5 6
7 8 9 10
Real program
The real program reads the number of rows with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 19: More Practice: Skip the Sevens
A tally excludes multiples of seven.
Specification: Use a for loop over 1 through 20. Use continue when the number is divisible by 7; add every other number to total and print Sum: <total>.
Worked sample — given values:
limit = 20
Expected output:
Sum: 189
Real program
The real program reads the upper bound with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 20: More Practice: Multiples of 3 or 5
A number puzzle totals all positive multiples of 3 or 5 below a limit.
Specification: Given limit = 1000, use a loop and an if to add each qualifying number once. Print Sum: <total>.
Worked sample — given values:
limit = 1000
Expected output:
Sum: 233168
Real program
The real program reads the exclusive upper bound with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 21: More Practice: Perfect Number Check
A perfect number equals the sum of its positive divisors smaller than itself.
Specification: Given n = 28, add each proper divisor and print 28 is perfect when the sum equals n; otherwise print <n> is not perfect.
Worked sample — given values:
n = 28
Expected output:
28 is perfect
Real program
The real program reads one integer n with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 22: More Practice: Divisor Count
A number report counts every divisor, including 1 and the number itself.
Specification: Given n = 36, test each candidate from 1 through n, count divisors, and print <n> has <count> divisors.
Worked sample — given values:
n = 36
Expected output:
36 has 9 divisors
Real program
The real program reads one integer n with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 23: More Practice: Leap Years in a Range
A calendar report counts leap years across an inclusive interval.
Specification: Given start = 1990 and end = 2030, loop over every year. Use nested decisions: check % 4 in the outer if, check % 100 inside it, then % 400 inside the century case. Count leap years and print Leap years: <count>.
Worked sample — given values:
start = 1990
end = 2030
Expected output:
Leap years: 10
Real program
The real program reads the start and end years on separate lines with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 24: More Practice: Pythagorean Triples
A puzzle searches for whole-number right triangles.
Specification: Find all a < b < c <= 20 with a * a + b * b == c * c. Loop over c first, then b, then a; print each matching triple on one line in that search order.
Worked sample — given values:
limit = 20
Expected output:
3 4 5
6 8 10
5 12 13
9 12 15
8 15 17
12 16 20
Real program
The real program reads the largest allowed side with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 25: More Practice: Chickens and Rabbits
A farm puzzle gives the number of heads and legs.
Specification: Given heads = 20 and legs = 56, try every possible number of rabbits. Each chicken has 2 legs; each rabbit has 4. Print the matching counts exactly as shown.
Worked sample — given values:
heads = 20
legs = 56
Expected output:
Chickens: 12, Rabbits: 8
Real program
The real program reads the heads and legs on separate lines with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 26: More Practice: Coin Combinations
A coin puzzle counts ways to make 50 cents.
Specification: Given target = 50, use three nested loops for counts of 5-cent, 10-cent, and 25-cent coins. Count each combination whose value is exactly the target. Print Ways: <count>.
Worked sample — given values:
target = 50
Expected output:
Ways: 10
Real program
The real program reads the target amount in cents with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 27: Step-Range Total
A shortcut range can advance by 2, visiting only the even values.
Specification: Use the three-argument form range(2, 21, 2) to visit the even integers from 2 through 20. Add them to a running total, then print exactly Sum: <total>.
Worked sample — required values:
2, 4, 6, ..., 20
Expected output:
Sum: 110
Real program
The real program reads one upper bound with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 28: Count the Primes
A number lab needs the count of primes from 2 through 20. Each candidate needs its own flag and divisor search.
Specification: Use an outer for candidate in range(2, 21):. Set is_prime = True for each candidate. In an inner loop over possible divisors, set the flag to False and break when one divides evenly. After the inner loop, add 1 to prime_count only when the flag is still true. Print exactly Count: <prime_count> after the outer loop.
Worked sample — required interval:
2 through 20
Expected output:
Count: 8
Real program
The real program reads one upper bound with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 29: Aligned Times Table
A compact table gives each product three character positions.
Specification: Given size = 6, use nested loops and f"{product:3}" for every product. Build each row without extra separators or trailing spaces.
Worked sample — given values:
size = 6
Expected output:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
Real program
The real program reads the table size with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: six lines; every product takes three character positions, so keep the leading and inside spaces exactly as shown; rows keep their leading spaces exactly as shown
Exercise 30: Primes up to 50
A number lab prints the primes through an inclusive limit.
Specification: Given limit = 50, test each candidate from 2 through the limit with a nested divisor loop and break. Print all primes on one space-separated line and then Count: <count>.
Worked sample — given values:
limit = 50
Expected output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Count: 15
Real program
The real program reads the inclusive upper bound with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 31: Pascal's Triangle
A sequence display builds each row from the previous value in that row.
Specification: Given rows = 5, use row from 0 through 4 and k from 0 through row in nested loops. Start c = 1 for each row. Add each c to the row string, then update it with c = c * (row - k) // (k + 1) for the next position. Print one space-separated row at a time.
Worked sample — given values:
rows = 5
Expected output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Real program
The real program reads the number of rows with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines