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

Letter A pattern

Python Practice › Loop Programs › Program 43

Letter A pattern

MediumLoop Programs · 43 of 90

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

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

Approach

  1. Row 0 is the rounded top, the middle row is the cross bar, and the other rows have stars on both sides.

Solution

Python
rows, cols = 7, 6for r in range(rows):    line = ""    for c in range(cols):        top = r == 0 and 0 < c < cols - 1        bar = r == rows // 2        sides = r > 0 and c in (0, cols - 1)        line += "* " if top or bar or sides else "  "    print(line)
Output
  * * * *   
*         * 
*         * 
* * * * * * 
*         * 
*         * 
*         * 

Line by line

Line 5

The top row has stars only between the corners.

Line 6

The middle row (index 3) is the full cross bar.

Line 7

Every other row has stars at the two edges.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Number diamond pattern

Python Practice › Loop Programs › Program 42

Number diamond pattern

HardLoop Programs · 42 of 90

Write a Python program to print rows of numbers that grow from 1 to 5 numbers (1 to 15) and then shrink, counting back down (14 to 5).

Output
 1
 2  3
 4  5  6
 7  8  9 10
11 12 13 14 15
14 13 12 11
10  9  8
 7  6
 5

Approach

  1. Upper half: keep a counter that increases across rows of length 1 to 5.
  2. Lower half: continue from 14 counting down, rows of length 4 to 1.

Solution

Python
n = 5num = 1for row in range(1, n + 1):    print(" ".join(f"{num + i:2}" for i in range(row)))    num += rownum -= 2                     # last printed was 15, continue from 14for row in range(n - 1, 0, -1):    print(" ".join(f"{num - i:2}" for i in range(row)))    num -= row
Output
 1
 2  3
 4  5  6
 7  8  9 10
11 12 13 14 15
14 13 12 11
10  9  8
 7  6
 5

Line by line

Line 4

Each row prints row consecutive numbers starting at num.

Line 5

The next row starts after the numbers just printed.

Line 6

After the top half num is 16, so subtracting 2 gives 14.

Lines 7-9

The lower half counts down, row lengths 4, 3, 2, 1.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Letter T pattern

Python Practice › Loop Programs › Program 41

Letter T pattern

MediumLoop Programs · 41 of 90

Write a Python program to print the letter ‘T’ with stars: two full rows of 9 stars and a 3-star stem in the middle.

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

Approach

  1. Print the top bar twice, then the stem rows padded with spaces to centre them.

Solution

Python
width, stem, height = 9, 3, 5for _ in range(2):    print("* " * width)pad = (width - stem) // 2for _ in range(height):    print("  " * pad + "* " * stem)
Output
* * * * * * * * * 
* * * * * * * * * 
      * * * 
      * * * 
      * * * 
      * * * 
      * * * 

Line by line

Line 3

"* " * 9 repeats the star-space pair 9 times.

Line 4

The stem starts 3 star-widths in, so it sits in the centre.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Keep only the digits of the input

Python Practice › Loop Programs › Program 47

Keep only the digits of the input

EasyLoop Programs · 47 of 90

Write a Python program that reads text and adds each digit to an empty list as a number.

Input
125python
Output
Enter text: 125python
[1, 2, 5]

Approach

  1. Loop over the characters and append the digits converted with int().

Solution

Python
text = input("Enter text: ")numbers = []for ch in text:    if ch.isdigit():        numbers.append(int(ch))print(numbers)
Outputtyped input: 125python
Enter text: 125python
[1, 2, 5]

Line by line

Line 4

isdigit() skips the letters.

Line 5

int(ch) stores numbers, not digit characters.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Count odd numbers 1 to 100

Python Practice › Loop Programs › Program 46

Count odd numbers 1 to 100

EasyLoop Programs · 46 of 90

Write a Python program to count the odd numbers from 1 to 100.

Output
50

Approach

  1. Increase a counter for each number with remainder 1.

Solution

Python
count = 0for n in range(1, 101):    if n % 2 != 0:        count += 1print(count)
Output
50

Learn the concept

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

More loop programs

← All 90 Loop Programs

Count even numbers 1 to 100

Python Practice › Loop Programs › Program 45

Count even numbers 1 to 100

EasyLoop Programs · 45 of 90

Write a Python program to count the even numbers from 1 to 100.

Output
50

Approach

  1. Increase a counter for each number with remainder 0.

Solution

Python
count = 0for n in range(1, 101):    if n % 2 == 0:        count += 1print(count)
Output
50

Learn the concept

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

More loop programs

← All 90 Loop Programs