Conditional Statements
Conditional Statements
else :
disc = Amt * .05 block 2
print(“Discount :”, disc) (will be executed incase condition is False)
• Program to print the positive difference of
two numbers.
• num1 = int(input("Enter first number: "))
• num2 = int(input("Enter second number: "))
• if num1 > num2:
• diff = num1 - num2
• else:
• diff = num2 - num1
• print("The difference
of",num1,"and",num2,"is",diff)
• Output:
• Enter first number: 5
• Enter second number: 6
• The difference of 5 and 6 is 1
Write a program in Python to check
whether the given number is Odd
or Even by using if else statement
Answer
num=int(input(“Enter a Number”))
If(num%2==0):
print(“The Given number is Even”)
else:
print(“The Given Number is Odd”)
The if..elif statement
• The if - elif statement has • Syntax
multiple test conditions and if <condition1> :
statement
in case the condition1 is Block 1
[statements]
True, it executes statements elif <condition2> :
in block1, and in case the statement
condition1 is False, it moves [statements] Block 2
to condition2, and in case elif <condition3> :
the condition2 is True, statement
[statements] Block 3
executes statements in
:
block2, so on. In case none :
of the given conditions is else :
true, then it executes the statement
statements under else block [statements]
Example of if-elif statement
• Program to find discount (20%) if amount>3000, disc(10%) if
Amount <=3000 and >1000, otherwise (5%).
Price = float (input(“Enter Item Price : ” ))
Qty = float (input(“Enter the Quantity : ” ))
Amt = Price* Qty
print(“ The purchased Amount = ” , Amt)
if Amt >3000 :
disc = Amt * .20 block 1
print(“Discount :”, disc) (will be executed incase condition 1 is true)
elif Amt>1000 and Amt<= 3000:
disc = Amt * .10 block 2
print(“Discount :”, disc) (will be executed incase condition2 is True)
else :
block 3
disc = Amt * .05
(will be executed incase both the
print(“Discount :”, disc)
condition1 &conditon2 are False)
Write a program to create a simple
calculator performing only four basic
operations.
#Program to create a four function calculator
result = 0
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program terminated")
else:
result = val1/val2
else:
print("Wrong input, program terminated")
if X > Y :
if X > Z: 10,4 , 9
Largest = X
else:
Largest = Z
else:
if Y > Z:
Largest = Y
else:
Largest = Z
print("Largest Number :",Largest)
Write Program that read three integer numbers and
print them in ascending order by using Nested if
statement.
else:
x=int(input(“Enter the first number”))
If x<y:
y=int(input(“Enter the first number”))
min,mid,max =z,x,y
z=int(input(“Enter the first number”))
min=mid=max=None else:
If x<y and x<z min,mid,max =z,y,x
if y<z: Print(“ The givern numbers in
min,mid,max =x,y,z Ascending Order”,
else: min,mid,max)
min,mid,max =x,z,y
elif y<x and y<z:
if x<z
min,mid,max =y,x,z
else
min,mid,max = y,z,x
Assignments
• WAP to input a Year and check whether it is a Leap year by using
if else statement.
• WAP to input a number check whether it is Positive or Negative
or ZERO by using elif statement.
• Write a Program to get 5 subject Marks of a students, and find
the grade as per following Percentage criterion: if Percentage
>=90 grade is “A” if Percentage between 75-89 grade is “B” if
Percentage between 60-74 grade is “C” and Below 60
Percentage grade is “D” by using if..elif statement.
What is leap year
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
Write a program to find the Sum of series by using
while loop
Example: OUTPUT
k = 1, sum=0 1
while k <= 4 : 2
print (k) 3
sum=sum + k 4
print(“Sum of series:”, sum) Sum of series: 10
Program to calculate the factorial of a number by using while loop