Use of statement of Python:
Use of if – else statement:-if – else is the variation of if statement. We attach condition with if statement if given condition is true then if block code will executed and if the given condition is false then else block code will executed.
The syntax of if – else statement is given below:-
if condition:
Statement 1
Statement 2
else:
Statement 3
Statement 4
Write a program in python to check the given number is even or odd.
n=input(“Enter a number : ”)
if n%2==0:
print “The number ”,n,”is even”
else:
print “The number”,n,”is odd”
Use of if-else ladder:-
In Python language the switch statement is not worked. In Python we use if-else ladder in spite of using switch.
The syntax of if-else ladder is given below:-
if condition1:
Statement 1
elif condition 2:
Statement 2
elif condition 3:
Statement 3 >br>
else:
Statement 4
Write a program in python to find greatest number in three numbers.
a=input(“Enter first number : ”)
b=input(“Enter second number : ”)
c=input(“Enter third number : ”)
if a>b and a>c:
print “Greatest Number = ”,a
elif b>a and b>c:
print “Greatest Number = ”,b
else:
print “Greatest Number = ”,c
Develop a program in Python to calculate bill after taking units from user. Take the following logic to calculate the electricity bill; For 1 to 150 units rate is Rs. 2.40, for next 150 (150 to 300) units rate is Rs. 3.00 and Rs. 3.20 for units above 300.
unit=input(“Enter the number of units consumed : ”)
if unit<=150:
bill=unit*2.40
elif unit>150 and unit<=300:
bill=(150*2.40)+(unit-150)*3
else:
bill=(150*2.40)+(150*3)+(unit-300)*3.20
print “Your bill = ”,bill
Iteration Statements in python:-
while loop:- The while is a keyword in python which works as loop control. The syntax of while loop is
Comments
Post a Comment
Thank you for comment.