Python if statements are used when there arises a situation to choose between multiple options depending on the results of the conditionality. For example, a number entered by the user needs to be categorized as a positive number or negative number. For such a case, the if statement provides a path for implementation. Let see example of an if statement.
# Python program to check if number is positive or negative num = float(input('Enter a number: ')) if num >= 0: print('Positive Number found') else: print('Negative Number found')
Enter a number: 10.4 Positive Number found
# Python program to check voting eligibility age = int(input("Enter your age: ")) if age >=18: print("Eligible for Voting") else: print("Not eligible for voting")
Enter your age: 45 Eligible for Voting
Another example where if statements can be used is when we want to categorize the price of an item.
# Python program to indicate toy price using if…elif…else toy_price = 380 if toy_price > 500: print('Toy price is Greater than Rs 500') elif toy_price < 500: print('Toy price is Less than Rs 500') else: print('Toy price is Rs 500')
Toy price is Less than Rs 500