Table of Contents
Python code to calculate Electricity bill | Python example
In this article, we will discuss the concept of Python code to calculate Electricity bill
In this program, we are going to learn how to calculate electricity bill using different methods in Python language.
This is done using if else if , function in Python language
Program to calculate Electricity bill
Calculate Electricity bill using if elif statements
In this program, we will calculate electricity bill using if elif statements in Python language
Program 1
#program for calculate electrycity bill in Python units=int(input("please enter the number of units you consumed: ")) if(units<=100): #when the statements is true payAmount=units*1.5 #executes this ,otherwise fixedcharge=25.00 elif(units<=200):#when the statements is true payAmount=(100*1.5)+(units-100)*2.5 #executes this ,otherwise fixedcharge=50.00 elif(units<=300): #when the statements is true payAmount=(100*1.5)+(200-100)*2.5+(units-200)*4 #executes this ,otherwise fixedcharge=75.00 elif(units<=350): #when the statements is true payAmount=(100*1.5)+(200-100)*2.5+(300-200)*4+(units-300)*5 fixedcharge=100.00 #executes this ,otherwise else: #when the the all statements are false payAmount=0 fixedcharge=1500.00#executes this Total=payAmount+fixedcharge; print("\nElecticity bill=%.2f" %Total) #display total bill amount
When the above code is executed, it produces the following result
Case 1
please enter the number of units you consumed: 400 Electricity bill=1500.00
Case 2
please enter the number of units you consumed: 100 Electricity bill=175.00
Case 3
please enter the number of units you consumed: 200 Electricity bill=450.00
Calculate Electricity bill using if elif with & operator
In this program, we will calculate electricity bill using if elif with & operator in Python language
Program 2
#calculate electicity bill '''1 - 50 -5/= 51 - 100 - 7/= 101 - 200- 10/= 201 - 300 - 15/= above 300 - 20/=''' unit=int(input("Enter the usage unit ")) if(unit >0)& (unit <=50):#When the statement is true print("Your bill is",unit*5) #display this,otherwise elif(unit >=51)& (unit<=100):#When the statement is true print("Your bill is",50*5+(unit-50)*7) #display this,otherwise elif(unit >=101)& (unit<=200):#When the statement is true print("Your bill is",50*5+(100-50)*7+(unit-100)*10) #display this,otherwise elif(unit >=201)& (unit<=300):#When the statement is true print("Your bill is",50*5+(100-50)*7+(200-100)*10+(unit-200)*15) #display this,otherwise elif(unit >=300): print("Your bill is",50*5+(100-50)*7+(200-100)*10+(300-200)*15+(unit-300)*20)
When the above code is executed, it produces the following result
Enter the usage unit 250 ('Your bill is', 2350)
Calculate Electricity bill using if elif with ‘and’ operator
In this program, we will calculate electricity bill using if elif with ‘and’ operator in Python language
Program 3
#program for calculating electricity bill in Python units=int(input("Number of unit consumed: ")) if(units>0 and units<=100): payAmount=units*1.5 fixedcharge=25.00 elif(units>100 and units<=200): payAmount=(100*1.5)+(units-100)*2.5 fixedcharge=50.00 elif(units>200 and units<=300): payAmount=(100*1.5)+(200-100)*2.5+(units-200)*4 fixedcharge=50.00 elif(units>300): payAmount=2500;#fixed rate fixedcharge=75.00 else: payAmount=0; Total= payAmount+fixedcharge print("\nElectricity bill pay=%.2f: " %Total);
When the above code is executed, it produces the following result
Number of unit consumed: 300 Electricity bill pay=850.00:
Calculate Electricity bill using function
In this program, we will calculate electricity bill using function in Python language
Program 4
units=int(input("Number of unit consumed: ")) def calc_Bill(units): #function definition if(units>0 and units<=100): print(units*1.5) elif(units>100 and units<=200): print((100*1.5)+(units-100)*2.5) elif(units>200 and units<=300): print((100*1.5)+(200-100)*2.5+(units-200)*4) elif(units>300): print(2500);#fixed rate else: print(0); calc_Bill(units) #call the function with argument
When the above code is executed, it produces the following result
Number of unit consumed: 345 2500
Suggested for you
Similar post
Python code to calculate Electricity bill
C++ code to calculate Electricity bill
C code to calculate Electricity bill
Java code to calculate Electricity bill