# Python Program to Calculate Electricity Bill

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

In this article, we will create a Python program that will assist us in calculating our electricity cost based on the inputs and conditions we provide. Keep in mind that conditions are not constant, as electricity rates vary from location to location. You can create your own program by adjusting the unit cost based on your present area.

Examples:

Example1:

Input:

Given number of units = 176

Output:

The Electricity bill for the units 176 = 1468.4207999999999

Example2:

Input:

Given number of units = 259

Output:

The Electricity bill for the units 259 = 2323.3871999999997

## Python Program to Calculate Electricity Bill

Below are the ways to Calculate the Electricity Bill in Python.

### Method #1:Using If  Else Statement (Static Input)

Approach:

Give the number of units consumed as static input and store it in a variable.

Here, I’m using the Indian Rupee (Rs) as the currency, and the requirements are as follows:

If the number of units used equals 100, the cost per unit is Rs 3.46.
If the number of units used is greater than 101 and less than 300, the cost per unit is Rs 7.43.
If you consume more than 301 units and less than 500 units, the cost per unit is Rs 10.32.
If the number of units consumed exceeds 501, the cost per unit is Rs 11.71.
The monthly line rent is Rs 1.45 per unit.
The additional fixed meter rent is Rs 100.
The tax on the bill is 16%, which is equal to 0.16.

Using If Else statements we check the conditions and calculate the cost per unit.

After Calculating cost per unit we multiply the number of units by 1.45 and add it to the bill(Adding monthly line rent to the bill)

We add Rs 100 to the bill as additional fixed meter rent.

We multiply the present bill amount by 0.16 (Adding the tax) and add this bill to the past bill.

Print the Final Electricity Bill.

The Exit of the Program.

Below is the Implementation:

# Give the number of units consumed as static input and store it in a variable.
numbOfUnits = 176
# Using If Else statements we check the conditions and calculate the cost per unit.
# If the number of units used equals 100, the cost per unit is Rs 3.46.
if numbOfUnits <= 100:
totalbill = numbOfUnits * 3.46
# If the number of units used is greater than 101 and less than 300,
# the cost per unit is Rs 7.43.
elif numbOfUnits >= 101 and numbOfUnits <= 300:
totalbill = 346 + ((numbOfUnits - 100) * 7.43)
# If you consume more than 301 units and less than 500 units,
# the cost per unit is Rs 10.32.
elif numbOfUnits >= 301 and numbOfUnits <= 500:
totalbill = 346 + 1486 + ((numbOfUnits - 300) * 10.32)
# If the number of units consumed exceeds 501,
# the cost per unit is Rs 11.71.
else:
totalbill = 346 + 1486 + 2064 + ((numbOfUnits - 500) * 11.71)
# After Calculating cost per unit we multiply the number of units by 1.45 and
# add it to the bill(Adding monthly line rent to the bill)
totalbill = totalbill + (numbOfUnits*1.45)
# We add Rs 100 to the bill as additional fixed meter rent.
totalbill = totalbill + 100
# We multiply the present bill amount by 0.16 (Adding the tax) and add this bill to the past bill.
totalbill = totalbill + (totalbill*0.16)
# Print the Final Electricity Bill.
print("The Electricity bill for the units", numbOfUnits, '=', totalbill)


Output:

The Electricity bill for the units 176 = 1468.4207999999999

### Method #2:Using If  Else Statement (User Input)

Approach:

• Give the number of units consumed as user input using int(input()) and store it in a variable.
• Calculate the bill using the above method.
• Using If Else statements we check the conditions and calculate the cost per unit.
• After Calculating cost per unit we multiply the number of units by 1.45 and add it to the bill(Adding monthly line rent to the bill)
• We add Rs 100 to the bill as additional fixed meter rent.
• We multiply the present bill amount by 0.16 (Adding the tax) and add this bill to the past bill.
• Print the Final Electricity Bill.
• The Exit of the Program.

Below is the Implementation:

# Give the number of units consumed as static input and store it in a variable.
numbOfUnits = int(input('Enter some random number of units ='))
# Using If Else statements we check the conditions and calculate the cost per unit.
# If the number of units used equals 100, the cost per unit is Rs 3.46.
if numbOfUnits <= 100:
totalbill = numbOfUnits * 3.46
# If the number of units used is greater than 101 and less than 300,
# the cost per unit is Rs 7.43.
elif numbOfUnits >= 101 and numbOfUnits <= 300:
totalbill = 346 + ((numbOfUnits - 100) * 7.43)
# If you consume more than 301 units and less than 500 units,
# the cost per unit is Rs 10.32.
elif numbOfUnits >= 301 and numbOfUnits <= 500:
totalbill = 346 + 1486 + ((numbOfUnits - 300) * 10.32)
# If the number of units consumed exceeds 501,
# the cost per unit is Rs 11.71.
else:
totalbill = 346 + 1486 + 2064 + ((numbOfUnits - 500) * 11.71)
# After Calculating cost per unit we multiply the number of units by 1.45 and
# add it to the bill(Adding monthly line rent to the bill)
totalbill = totalbill + (numbOfUnits*1.45)
# We add Rs 100 to the bill as additional fixed meter rent.
totalbill = totalbill + 100
# We multiply the present bill amount by 0.16 (Adding the tax) and add this bill to the past bill.
totalbill = totalbill + (totalbill*0.16)
# Print the Final Electricity Bill.
print("The Electricity bill for the units", numbOfUnits, '=', totalbill)


Output:

Enter some random number of units =259
The Electricity bill for the units 259 = 2323.3871999999997

Related Programs: