Letter O pattern

Python Practice › Loop Programs › Program 10

Letter O pattern

MediumLoop Programs · 10 of 90

Write a Python program to print the letter ‘O’ with stars.

Output
 *** 
*   *
*   *
*   *
*   *
*   *
 *** 

Approach

  1. The top and bottom rows print stars in the middle columns.
  2. The other rows print a star only in the first and last columns.

Solution

Python
rows, cols = 7, 5for r in range(rows):    line = ""    for c in range(cols):        top_or_bottom = (r == 0 or r == rows - 1) and 0 < c < cols - 1        side = 0 < r < rows - 1 and (c == 0 or c == cols - 1)        line += "*" if top_or_bottom or side else " "    print(line)
Output
 *** 
*   *
*   *
*   *
*   *
*   *
 *** 

Line by line

Line 5

The first and last rows get stars only between the corners, which rounds the shape.

Line 6

The middle rows get stars only on the left and right edges.

Line 7

Each position adds a star or a space.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Count letters and digits

Python Practice › Loop Programs › Program 9

Count letters and digits

EasyLoop Programs · 9 of 90

Write a Python program that counts the letters and digits in a string.

Input
"python1234"
Output
Letters 6
Digits 4

Approach

  1. Use isalpha() and isdigit() on each character.

Solution

Python
text = "python1234"letters = digits = 0for ch in text:    if ch.isalpha():        letters += 1    elif ch.isdigit():        digits += 1print("Letters", letters)print("Digits", digits)
Output
Letters 6
Digits 4

Line by line

Lines 4-7

Spaces and symbols match neither test, so they are not counted.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Convert uppercase letters to lowercase

Python Practice › Loop Programs › Program 8

Convert uppercase letters to lowercase

EasyLoop Programs · 8 of 90

Write a Python program that converts all uppercase letters in a word to lowercase, using a loop.

Input
"SqaTOOlS"
Output
sqatools

Approach

  1. Loop over the characters, lower-case the uppercase ones and build a new string.

Solution

Python
word = "SqaTOOlS"result = ""for ch in word:    if ch.isupper():        result += ch.lower()    else:        result += chprint(result)
Output
sqatools

Line by line

Lines 4-5

Only uppercase characters are changed.

Tip

word.lower() does the whole job in one call; the loop shows how it works.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

FizzBuzz from 1 to 30

Python Practice › Loop Programs › Program 7

FizzBuzz from 1 to 30

EasyLoop Programs · 7 of 90

Write a Python program that prints 1 to 30, but “Fizz” for multiples of 3, “Buzz” for multiples of 5 and “FizzBuzz” for multiples of both.

Output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 

Approach

  1. Check the multiple-of-15 case first.

Solution

Python
for n in range(1, 31):    if n % 15 == 0:        print("FizzBuzz", end=" ")    elif n % 3 == 0:        print("Fizz", end=" ")    elif n % 5 == 0:        print("Buzz", end=" ")    else:        print(n, end=" ")print()
Output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 

Line by line

Line 2

A multiple of both 3 and 5 is a multiple of 15; testing it first stops 15 from printing only “Fizz”.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Fibonacci series up to 20

Python Practice › Loop Programs › Program 6

Fibonacci series up to 20

EasyLoop Programs · 6 of 90

Write a Python program to print the Fibonacci series from 0 to 20.

Output
0 1 1 2 3 5 8 13 

Approach

  1. Keep two variables and advance them until the value passes 20.

Solution

Python
a, b = 0, 1while a <= 20:    print(a, end=" ")    a, b = b, a + bprint()
Output
0 1 1 2 3 5 8 13 

Line by line

Line 4

Both values update at once, so no temporary variable is needed.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Print 0 to 6 except 3 and 6

Python Practice › Loop Programs › Program 5

Print 0 to 6 except 3 and 6

EasyLoop Programs · 5 of 90

Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.

Output
0 1 2 4 5 

Approach

  1. Use continue to skip the unwanted numbers.

Solution

Python
for n in range(7):    if n in (3, 6):        continue    print(n, end=" ")print()
Output
0 1 2 4 5 

Line by line

Lines 2-3

continue jumps to the next number, so 3 and 6 are never printed.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Count even and odd numbers

Python Practice › Loop Programs › Program 4

Count even and odd numbers

EasyLoop Programs · 4 of 90

Write a Python program to count the even and odd numbers in a series.

Input
(1, 2, 3, 4, 5, 6, 7, 8, 9)
Output
Number of even numbers: 4
Number of odd numbers: 5

Approach

  1. Keep two counters and increase one of them for each number.

Solution

Python
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)even = odd = 0for n in numbers:    if n % 2 == 0:        even += 1    else:        odd += 1print("Number of even numbers:", even)print("Number of odd numbers:", odd)
Output
Number of even numbers: 4
Number of odd numbers: 5

Line by line

Line 2

Both counters start at 0.

Lines 4-7

Each number increases exactly one counter.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Star pattern that grows then shrinks

Python Practice › Loop Programs › Program 2

Star pattern that grows then shrinks

EasyLoop Programs · 2 of 90

Write a Python program to print a star pattern that grows from 1 to 5 stars and shrinks back to 1, using nested loops.

Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Approach

  1. First loop: rows 1 to 5. Second loop: rows 4 down to 1.
  2. An inner loop prints the stars of each row.

Solution

Python
n = 5for i in range(1, n + 1):    for _ in range(i):        print("*", end=" ")    print()for i in range(n - 1, 0, -1):    for _ in range(i):        print("*", end=" ")    print()
Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Line by line

Lines 2-5

Row i prints i stars; end=" " keeps them on one line and print() ends the row.

Line 6

range(n - 1, 0, -1) counts 4, 3, 2, 1 for the shrinking half.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Numbers divisible by 7 and multiples of 5

Python Practice › Loop Programs › Program 1

Numbers divisible by 7 and multiples of 5

EasyLoop Programs · 1 of 90

Write a Python program to find the numbers between 1500 and 2700 (both included) that are divisible by 7 and are multiples of 5.

Output
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995, 2030, 2065, 2100, 2135, 2170, 2205, 2240, 2275, 2310, 2345, 2380, 2415, 2450, 2485, 2520, 2555, 2590, 2625, 2660, 2695]
35 numbers

Approach

  1. Loop over the range and keep numbers where n % 7 == 0 and n % 5 == 0.

Solution

Python
result = []for n in range(1500, 2701):    if n % 7 == 0 and n % 5 == 0:        result.append(n)print(result)print(len(result), "numbers")
Output
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995, 2030, 2065, 2100, 2135, 2170, 2205, 2240, 2275, 2310, 2345, 2380, 2415, 2450, 2485, 2520, 2555, 2590, 2625, 2660, 2695]
35 numbers

Line by line

Line 2

range(1500, 2701) includes 2700 because the end value is excluded.

Line 3

Both conditions must hold, so they are joined with and.

Tip

Divisible by both 7 and 5 means divisible by 35, so range(1505, 2701, 35) gives the same list without a test.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Python Loop Programs with Solutions

Python Practice › Loop Programs

Python Loop Programs with Solutions

90 programs75 easy14 medium1 hard

90 loop programs from counting and digit tricks to star and letter patterns. They practise for, while, range(), break, continue and nested loops; every solution is explained and shows its real output.

Before you start

Read the Loops: for & while lesson first if the topic is new to you.

All programs