Fibonacci numbers up to 55

Python Practice › Loop Programs › Program 56

Fibonacci numbers up to 55

EasyLoop Programs · 56 of 90

Write a Python program to print the first 11 Fibonacci numbers (0 to 55).

Output
0 1 1 2 3 5 8 13 21 34 55

Approach

  1. Build a list starting [0, 1] and append the sum of the last two.

Solution

Python
fib = [0, 1]while len(fib) < 11:    fib.append(fib[-1] + fib[-2])print(*fib)
Output
0 1 1 2 3 5 8 13 21 34 55

Line by line

Line 3

fib[-1] and fib[-2] are the last two values.

Line 4

*fib passes the items separately, so they print with spaces.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Number triangle 1, 22, 333

Python Practice › Loop Programs › Program 55

Number triangle 1, 22, 333

EasyLoop Programs · 55 of 90

Write a Python program to print row i with the number i repeated i times, for i from 1 to 5, using nested loops.

Output
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 

Approach

  1. An inner loop prints the row number i times.

Solution

Python
for i in range(1, 6):    for _ in range(i):        print(i, end=" ")    print()
Output
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 

Learn the concept

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

More loop programs

← All 90 Loop Programs

Inverted right triangle of stars

Python Practice › Loop Programs › Program 54

Inverted right triangle of stars

EasyLoop Programs · 54 of 90

Write a Python program to print an inverted right triangle of stars with 5 rows.

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

Approach

  1. Count rows down from 5 to 1.

Solution

Python
for i in range(5, 0, -1):    print("* " * i)
Output
* * * * * 
* * * * 
* * * 
* * 
* 

Learn the concept

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

More loop programs

← All 90 Loop Programs

Right triangle of stars

Python Practice › Loop Programs › Program 53

Right triangle of stars

EasyLoop Programs · 53 of 90

Write a Python program to print a right triangle of stars with 5 rows.

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

Approach

  1. Row i prints i stars.

Solution

Python
for i in range(1, 6):    print("* " * i)
Output
* 
* * 
* * * 
* * * * 
* * * * * 

Line by line

Line 2

Repeating “* ” i times builds row i.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Numbers from 0 to 100 divisible by 5

Python Practice › Loop Programs › Program 52

Numbers from 0 to 100 divisible by 5

EasyLoop Programs · 52 of 90

Write a Python program to find the numbers from 0 to 100 divisible by 5.

Output
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

Approach

  1. Keep numbers with n % 5 == 0.

Solution

Python
print([n for n in range(0, 101) if n % 5 == 0])
Output
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

Learn the concept

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

More loop programs

← All 90 Loop Programs

Sum of odd numbers 1 to 100

Python Practice › Loop Programs › Program 51

Sum of odd numbers 1 to 100

EasyLoop Programs · 51 of 90

Write a Python program to calculate the sum of all odd numbers from 1 to 100.

Output
2500

Approach

  1. Loop over odd numbers and add them.

Solution

Python
total = 0for n in range(1, 101, 2):    total += nprint(total)
Output
2500

Learn the concept

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

More loop programs

← All 90 Loop Programs

Electricity bill with slabs

Python Practice › Loop Programs › Program 50

Electricity bill with slabs

MediumLoop Programs · 50 of 90

Write a Python program to calculate an electricity bill: first 50 units Rs 0.50, next 100 units Rs 0.75, next 100 units Rs 1.25, above 250 units Rs 1.50, plus a 17% surcharge.

Input
350
Output
438.75
90.67

Approach

  1. Walk through the slabs with a loop, charging the units that fall in each one.

Solution

Python
slabs = [(50, 0.50), (100, 0.75), (100, 1.25), (float("inf"), 1.50)] def bill(units):    amount = 0    for size, rate in slabs:        used = min(units, size)        amount += used * rate        units -= used        if units == 0:            break    return round(amount * 1.17, 2) print(bill(350))print(bill(120))
Output
438.75
90.67

Line by line

Line 1

Each slab is (number of units, price per unit); the last slab has no limit.

Lines 6-8

Take as many units as fit in the slab, charge them and subtract them.

Line 11

Add the 17% surcharge.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Taxi bill by distance

Python Practice › Loop Programs › Program 49

Taxi bill by distance

MediumLoop Programs · 49 of 90

Write a Python program to calculate a bill: first 5 km at Rs 15/km, next 20 km at Rs 12/km, after that Rs 10/km.

Input
3 km, 18 km, 40 km
Output
3 km -> 45
18 km -> 231
40 km -> 465

Approach

  1. Charge each kilometre at the rate of the slab it falls in.

Solution

Python
def fare(km):    total = 0    for k in range(1, km + 1):        if k <= 5:            total += 15        elif k <= 25:            total += 12        else:            total += 10    return total for km in [3, 18, 40]:    print(km, "km ->", fare(km))
Output
3 km -> 45
18 km -> 231
40 km -> 465

Line by line

Lines 3-9

The loop visits every kilometre and adds the rate for its position, which handles the slabs naturally.

Note

40 km = 5 × 15 + 20 × 12 + 15 × 10 = 465.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Characters of a string into a list

Python Practice › Loop Programs › Program 48

Characters of a string into a list

EasyLoop Programs · 48 of 90

Write a Python program that adds every character of an input string to an empty list.

Input
sqatools007
Output
Enter text: sqatools007
['s', 'q', 'a', 't', 'o', 'o', 'l', 's', '0', '0', '7']

Approach

  1. Append each character in a loop.

Solution

Python
text = input("Enter text: ")chars = []for ch in text:    chars.append(ch)print(chars)
Outputtyped input: sqatools007
Enter text: sqatools007
['s', 'q', 'a', 't', 'o', 'o', 'l', 's', '0', '0', '7']
Tip

list(text) gives the same list in one call.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Star pyramid

Python Practice › Loop Programs › Program 44

Star pyramid

EasyLoop Programs · 44 of 90

Write a Python program to print a pyramid of stars with 5 rows (1, 3, 5, 7, 9 stars).

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

Approach

  1. Row i has 2i - 1 stars and n - i leading spaces.

Solution

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

Line by line

Line 3

Fewer spaces and more stars on each row make the pyramid shape.

Learn the concept

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

More loop programs

← All 90 Loop Programs