Natural numbers 1 to n with while

Python Practice › Loop Programs › Program 11

Natural numbers 1 to n with while

EasyLoop Programs · 11 of 90

Write a Python program to print the natural numbers from 1 to n using a while loop.

Input
n = 10
Output
1 2 3 4 5 6 7 8 9 10 

Approach

  1. Start a counter at 1 and increase it until it passes n.

Solution

Python
n = 10i = 1while i <= n:    print(i, end=" ")    i += 1print()
Output
1 2 3 4 5 6 7 8 9 10 

Line by line

Line 5

Forgetting this line would make the loop run forever.

Learn the concept

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

More loop programs

← All 90 Loop Programs

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

Build a string character by character

Python Practice › Loop Programs › Program 3

Build a string character by character

EasyLoop Programs · 3 of 90

Write a Python program that adds each character of a word from the user to an empty string.

Input
python
Output
Enter a word: python
python

Approach

  1. Start with an empty string and append each character with +=.

Solution

Python
word = input("Enter a word: ")result = ""for ch in word:    result += chprint(result)
Outputtyped input: python
Enter a word: python
python

Line by line

Line 2

The empty string is the starting value.

Lines 3-4

Each loop pass appends one character.

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