Python Programming - If-Else Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better.

Python Programming – If-Else Statement

The statement if also allows answering for the false value by using an else clause. The else statement con¬tains the block of code that executes if the conditional expression in the if statement evaluates to 0 or a false value. The else statement is an optional statement; if it is provided, in any situation one of the two blocks gets executed, not both.
The syntactic form of the if-else statement is as follows:

if (expression):
statements BLOCK 1
else:
statements BLOCK 2

When the expression evaluates to a non-zero value, the condition is considered to be true, and statements BLOCK 1 are executed; otherwise, statements BLOCK 2 are executed. Blocks can have multiple lines. As long as they are all indented with the same amount of space, they constitute one block. Figure 5.5 shows the flow diagram of the if-else statement.
For example,

Python Programming - If-Else Statement chapter 5 img 1

if 10>100:
print ( ” 10 is greater than 100 ” )
else:
print ( ” 10 is less than 100 ” )

Simple Programs

Example 2.
Program to print absolute value of a number provided by the user.

#Take a number from the user and print its absolute value.
num = eval ( input ( “Enter a number : “)
print (‘|’, num, ‘| = ‘ , (num if num <0 else num) , sep=’ ‘)RUN
>>>
Enter a number : -9
|-9| = 9
>>>
>>>
Enter a number : 9
|9| = 9
>>>

Example 3.
Program to check divisibility of a number.

# Program to check divisibility of a number
num1 = int(input(“Enter first number : “))
num2 = int(input(“Enter second number : “))
remainder = numl%num2
if remainder == 0:
print(“First number is divisible by second number”) else:
print(“First number is not divisible by second number”)RUN
>>>
Enter first number: 100
Enter second number: 2
The first number is divisible by the second number
>>>
>>>
Enter first number: 9
Enter second number: 2
The first number is not divisible by the second number
>>>

Example 4.
Program for tax calculation.

# Program for -tax calculation
name=input(“Enter the name of Employee :”)
salary=float(input(“Enter the salary :”))
if (salary>50000):
tax=0.10*salary
netsalary=salary-tax
print(“The net salary of “+name+” is”,netsalary)
else:
netsalary=salary
print(“No taxable Amount”)
print(“The net salary of “+name+ “is”,salary)RUN
>>>
Enter the name of Employee: Swati
Enter the salary: 60000
The net salary of Swati is 54000.0
>>>
>>>
Enter the name of Employee: Geeta
Enter the salary:45000
No taxable Amount
The net salary of Geeta is 45000.0
>>>

Example 5.
Program to print the largest number.

# Program to print the largest no.
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
num3 = int (input (“Enter third number: “))
max = num1
if num2 > max:
max= num2
if num3> max:
max=num3
print(“Largest number is: “, max)RUN
>>>
Enter first number: 27
Enter second number: 37
Enter third number: 17
Largest number is: 37
>>>

Let’s Try

Write a program to print the smallest number.
Suppose, you want to write a program that accepts a value of a variable from the user and prints whether the number is positive or negative using the if-else statement. The program is shown in Figure 5.10.

Example 6.
Program to Print negative or positive number.

# Demo of if-else statement
num = int(input(“Enter a number :”))
if (num < 0) :
print(“Number is negative.”)
else:
print(“Number is positive.”)RUN
>>>
Enter a number: 5
The number is positive.
>>>
Enter a number:-7
The number is negative.
>>>

Example 7.
Program to read two numbers and print quotient.

# Program to print quotient
dividend, divisor = eval(input(‘Please enter two numbers to divide: ‘))
if divisor != 0:
print(dividend, ‘/’, divisor. “=”, dividend/divisor)
else:
print(‘Division by zero is not allowed’)RUN
>>>
Please enter two numbers to divide : 8 , 2
8 / 2 = 4.0
>>>
Please enter two numbers to divide : 2 , 8
2 / 8 = 0.25
>>>
Please enter two numbers to divide: 8,0
Division by zero is not allowed
>>>

Example 8.
Program to display menu to calculate area of square or rectangle.

# Program to display menu to calculate area of square or rectangle
print(“1. Calculate area of a square “)
print(“2. Calculate area of rectangle “)
choice = int(input(“Enter your choice (1 or 2): “))
if (choice == 1):
side = float(input(“Enter side of a square : “))
area_square = side * side
print(“Area of a square is , areasquare)
else:
length = float(input(“Enter length of a rectangle : “))
breadth = float(input(“Enter breadth of a rectangle : “))
area_rectangle = length * breadth
print(“Area of a rectangle is , arearectangle)RUN
>>>
1. Calculate the area of a square
2. Calculate the area of a rectangle
Enter your choice (1 or 2): 2
Enter the length of a rectangle: 8
Enter breadth of a rectangle: 4
The area of a rectangle is: 32.0
>>>
>>>
1. Calculate the area of a square
2. Calculate the area of the rectangle
Enter your choice (1 or 2): 1
Enter side of a square: 2.5
The area of a square is: 6.25
>>>

Example 9.
Program to print larger number using swap

In the program shown in figure 5.13, the if statement rearranges any two values stored in x and y in such a way that the smaller number will always be in x and the largest number always in y. If the two numbers are already in the proper order, the compound statement will not be executed. You will always get the larger value first and then the smaller value.

# print larger number using swap
x=y=0
x=int(input (“Enter a number : “) )
if (num % 2 ==0):
print (num, ” is an even number. “)
else:
print (num , ” is an odd number . ” )RUN
>>>
Enter a number: 2
2 is an even number.
>>>
>>>
Enter a number: 5
5 is an odd number.
>>>

Example 10.
Program to print if the number is even or odd.

# Program to print no. is even or odd
num=int(input(“Enter a number: “))
if (num % 2 == 0):
print(num, “is an even number.”)
else:
print(num, “is an odd number.”)RUN
>>>
Enter a number: 2
2 is an even number.
>>>
>>>
Enter a number: 5
5 is an odd number.
>>>

Example 11.
Program to display the menu to calculate the sum or product of entered numbers.

# Program to display menu to calculate sum or product of entered numbers
numl = int(input(“Enter first number : “))
num2 = int(input(“Enter second number : “))
num3 = int(input(“Enter third number : “))
print(“1. Calculate sum”)
print(“2. Calculate product”)
choice = int(input(“Enter your choice (1 or 2): “))
if (choice == 1):
sum = num1+num2+num3
print(“Sum of three numbers are , sum) else:
product = numl*num2*num3
print(“Product of three numbers are : “.product )
RUN
>>>
Enter the first number: 2 Enter second number : 3 Enter third number: 4
1. Calculate the sum
2. Calculate the product
Enter your choice (1, or 2): 1
The sum of the three numbers are: 9
>>>
>>>
Enter first number: 2
Enter second number : 3
Enter third number: 4
1. Calculate the sum
2. Calculate the product
Enter your choice (1 or 2): 2
Product of three numbers are: 24
>>>

Leave a Reply

Your email address will not be published. Required fields are marked *