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

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

Extract 10-digit mobile numbers

Python Practice › String Programs › Program 106

Extract 10-digit mobile numbers

EasyString Programs · 106 of 106

Write a Python program to get all 10-digit mobile numbers from a string.

Input
a paragraph containing short numbers and four 10-digit numbers
Output
['8988858683', '2312245566', '4532892234', '9999234355']

Approach

  1. Match exactly 10 digits with word boundaries.

Solution

Python
import re text = """We have 2233 some employee8988858683whos 3455 mobile numbers are randomly distributed2312245566we want 453452 to get4532892234all the mobile numbers9999234355from this given string."""print(re.findall(r"\b\d{10}\b", text))
Output
['8988858683', '2312245566', '4532892234', '9999234355']

Line by line

Line 11

\b stops a match inside a longer number, and {10} requires exactly ten digits.

Learn the concept

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

More string programs

← All 106 String Programs

Extract email addresses

Python Practice › String Programs › Program 105

Extract email addresses

EasyString Programs · 105 of 106

Write a Python program to extract all email addresses from a string.

Input
a paragraph with four email addresses
Output
['john@gmail.com', 'jay@lic.com', 'hari@facebook.com', 'mery@hotmail.com']

Approach

  1. A regex for “letters @ domain . extension”.

Solution

Python
import re text = """We have some employee whosjohn@gmail.comemail id's are randomly distributedjay@lic.comwe want to gethari@facebook.comall the emailmery@hotmail.comid's from this given string."""print(re.findall(r"[\w.+-]+@[\w-]+\.[\w.]+", text))
Output
['john@gmail.com', 'jay@lic.com', 'hari@facebook.com', 'mery@hotmail.com']

Line by line

Line 11

The pattern matches the name, “@”, a domain and at least one dot-separated extension.

Learn the concept

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

More string programs

← All 106 String Programs

Each character on a new line

Python Practice › String Programs › Program 104

Each character on a new line

EasyString Programs · 104 of 106

Write a Python program to print each character on its own line.

Input
"python"
Output
p
y
t
h
o
n

Approach

  1. Loop or print(*text, sep="\n").

Solution

Python
print(*"python", sep="\n")
Output
p
y
t
h
o
n

Line by line

Line 1

* unpacks the string into separate characters and sep="\n" puts each on its own line.

Learn the concept

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

More string programs

← All 106 String Programs