You’ll learn how to utilise the Python if statement to run a block of code based on a condition in this article.
The if statement is used to execute a block of code based on a condition.
The if statement has the following syntax:
if condition: if-block
The condition is checked first by the if statement.
If the condition evaluates to True, the if-block statements are executed. Otherwise, the statements are ignored.
It’s worth noting that the colon (:) that follows the condition is crucial. You’ll get a syntax error if you forget it.
age = input('Enter your age:') if int(age) >= 18: print("You're eligible to vote.")
This sample asks you to provide your age. If you enter a number that is greater than or equal to 18, the screen will display the message “You are eligible to vote.” Otherwise, it has no effect.
The int(age) >= 18 condition turns the input string to an integer and compares it to 18.
Enter your age:18 You're eligible to vote.
Consider the following scenario:
age = input('Enter your age:') if int(age) >= 18: print("You're eligible to vote.") print("Let's go and vote.")
If you enter a number larger than or equal to 18, you’ll get two messages in this example.
The importance of indentation in this case is critical. Any statement that comes after the if statement requires four spaces.
The program will behave differently if you don’t use the indentation correctly. For instance:
age = input('Enter your age:') if int(age) >= 18: print("You're eligible to vote.") print("Let's go and vote.")
The final sentence is always executed in this example, regardless of the condition in the if statement. The reason for this is that it isn’t part of the if block:
Enter your age:11 Let's go and vote.
if else statement in Python
When a condition is True, you want to do one thing, and when it is False, you want to do something else.
The if…else expression is used to do this.
The syntax of the if…else sentence is as follows:
if condition: if-block; else: else-block;
If the condition evaluates to True, the if…else syntax will execute the if-block. Otherwise, the else-block will be executed.
The if…else sentence is demonstrated in the following example:
age = input('Enter your age:') if int(age) >= 18: print("You're eligible to vote.") else: print("You're not eligible to vote.")
If you enter a figure less than 18 as your age, you’ll get the message “You’re not able to vote.” like this:
Enter your age:11 You're not eligible to vote.
if…elif…else statement in Python
The if…elif…else statement can be used to check various conditions and take action based on the results. Elif stands for else if.
The if…elif…else sentence has the following syntax:
if if-condition: if-block elif elif-condition1: elif-block1 elif elif-condition2: elif-block2 ... else: else-block
The if…elif…else statement evaluates each condition (if-condition, elif-condition1, elif-condition2,…) in the order they appear in the statement until it finds one that evaluates to True.
When the if…elif…else statement finds one, it executes the statement that comes after the condition and skips the rest of the tests.
The if…elif…else statement performs the sentence in the else branch if no condition evaluates to True.
It’s important to note that the else block is optional. If you leave it out and none of the conditions are True, the statement has no effect.
The if…elif…else sentence is used in the following example to decide the ticket price based on the age:
age = input('Enter your age:') # convert the string to int your_age = int(age) # determine the ticket price if your_age < 5: ticket_price = 5 elif your_age < 16: ticket_price = 10 else: ticket_price = 18 # show the ticket price print(f"You'll pay ${ticket_price} for the ticket")
For example, consider the following:
The ticket fee will be $5 if the age entered is less than 5.
The ticket fee is $10 if the input age is greater than 5 but less than 16.
Ticket prices are $18 otherwise.
Leave a Reply