0
0
PythonProgramBeginner · 2 min read

Python Program to Calculate Electricity Bill

You can calculate the electricity bill in Python by taking the number of units consumed as input and applying a rate per unit, for example: bill = units * rate_per_unit.
📋

Examples

Input50
OutputElectricity bill: 250.0
Input150
OutputElectricity bill: 750.0
Input0
OutputElectricity bill: 0.0
🧠

How to Think About It

To calculate the electricity bill, first get the number of units consumed from the user. Then multiply the units by a fixed rate per unit to find the total cost. This is like buying items where each item has a price, and you pay for how many you buy.
📐

Algorithm

1
Get the number of units consumed as input
2
Set the rate per unit of electricity
3
Multiply units by rate to get the bill amount
4
Display the calculated bill
💻

Code

python
units = float(input('Enter units consumed: '))
rate_per_unit = 5.0
bill = units * rate_per_unit
print('Electricity bill:', bill)
Output
Enter units consumed: 100 Electricity bill: 500.0
🔍

Dry Run

Let's trace the program with input 100 units.

1

Input units

User enters 100

2

Set rate

rate_per_unit = 5.0

3

Calculate bill

bill = 100 * 5.0 = 500.0

4

Print result

Print 'Electricity bill: 500.0'

unitsrate_per_unitbill
1005.0500.0
💡

Why This Works

Step 1: Get input

We use input() to get the number of units consumed from the user as a string, then convert it to a float for calculation.

Step 2: Set rate

We define a fixed rate per unit, here 5.0, which means each unit costs 5 currency units.

Step 3: Calculate bill

We multiply the units by the rate using * operator to get the total bill.

Step 4: Display output

We print the result using print() so the user sees the total electricity bill.

🔄

Alternative Approaches

Using tiered rates
python
units = float(input('Enter units consumed: '))
if units <= 100:
    bill = units * 5
else:
    bill = 100 * 5 + (units - 100) * 7
print('Electricity bill:', bill)
This method uses different rates for first 100 units and remaining units, which is common in real bills.
Using function
python
def calculate_bill(units):
    rate = 5
    return units * rate
units = float(input('Enter units consumed: '))
bill = calculate_bill(units)
print('Electricity bill:', bill)
Encapsulates calculation in a function for reuse and clarity.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).

Space Complexity

It uses a few variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using a function adds clarity but no significant speed difference.

ApproachTimeSpaceBest For
Simple multiplicationO(1)O(1)Basic fixed rate calculation
Tiered ratesO(1)O(1)Realistic billing with slabs
Function encapsulationO(1)O(1)Code reuse and clarity
💡
Always convert input to a number type before calculations to avoid errors.
⚠️
Forgetting to convert input from string to number causes calculation errors.