Questions

What is an Example of an If Statement?

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.

Example 1

                    

# 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')

Output

                    

Enter a number: 10.4
Positive Number found

Example

                    

# 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")

Output –

                    

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.

Example 3

                    

# 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')

Output

                    

Toy price is Less than Rs 500

Related Questions

Related Topics

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.