Python if Statement

Created with Sketch.

Python if Statement

 

Summary: in this tutorial, you’ll learn how to use the Python if statement to execute a block of code based on a condition.

The simple Python if statement

You use the if statement to execute a block of code based on a specified condition.

The syntax of the if statement is as follows:

if condition:
if-block

Code language: Python (python)

The if statement checks the condition first.

If the condition evaluates to True, it executes the statements in the if-block. Otherwise, it ignores the statements.

Note that the colon (:) that follows the condition is very important. If you forget it, you’ll get a syntax error.

The following flowchart illustrates the if staetement:

For example:

age = input('Enter your age:')
if int(age) >= 18:
print("You're eligible to vote.")

Code language: Python (python)

This example prompts you to input your age. If you enter a number that is greater than or equal to 18, it’ll show a message "You're eligible to vote" on the screen. Otherwise, it does nothing.

The condition int(age) >= 18 converts the input string to an integer and compares it with 18.

Enter your age:18
You're eligible to vote.

Code language: Python (python)

See the following example:

age = input('Enter your age:')
if int(age) >= 18:
print("You're eligible to vote.")
print("Let's go and vote.")

Code language: Python (python)

In this example, if you enter a number that is greater than or equal to 18, you’ll see two messages.

In this example, indentation is very important. Any statement that follows the if statement needs to have has four spaces.

If you don’t use the indentation correctly, the program will work differently. For example:

age = input('Enter your age:')
if int(age) >= 18:
print("You're eligible to vote.")
print("Let's go and vote.")

Code language: Python (python)

In this example, the final statement always executes regardless of the condition in the if statement. The reason is that it doesn’t belong to the if block:

Enter your age:11
Let's go and vote.

Code language: Python (python)

Python if…else statement

Typically, you want to perform an action when a condition is True and another action when the condition is False.

To do so, you use the if...else statement.

The following shows the syntax of the if...else statement:

if condition:
if-block;
else:
else-block;

Code language: Python (python)

In this syntax, the if...else will execute the if-block if the condition evaluates to True. Otherwise, it’ll execute the else-block.

The following flowchart illustrates the if..else statement:

The following example illustrates you how to use the  if...else statement:

age = input('Enter your age:')
if int(age) >= 18:
print("You're eligible to vote.")
else:
print("You're not eligible to vote.")

Code language: Python (python)

In this example, if you enter your age with a number less than 18, you’ll see the message "You're not eligible to vote." like this:

Enter your age:11
You're not eligible to vote.

Code language: Python (python)

Python if…elif…else statement

If you want to check multiple conditions and perform an action accordingly, you can use the if...elif...else statement. The elif stands for else if.

Here is the syntax if the if...elif...else statement:

if if-condition:
if-block
elif elif-condition1:
elif-block1
elif elif-condition2:
elif-block2
...
else:
else-block

Code language: Python (python)

The if...elif...else statement checks each condition (if-condition, elif-condition1, elif-condition2, …) in the order that they appear in the statement until it finds the one that evaluates to True.

When the if...elif...else statement finds one, it executes the statement that follows the condition and skips testing the remaining conditions.

If no condition evaluates to True, the if...elif...else statement executes the statement in the else branch.

Note that the else block is optional. If you omit it and no condition is True, the statement does nothing.

The following flowchart illustrates the if...elif...else statement:

The following example uses the if...elif..else statement to determine 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")

Code language: Python (python)

In this example:

  • If the input age is less than 5, the ticket price will be $5.
  • If the input age is greater than or equal to 5 and less than 16, the ticket price is $10.
  • Otherwise, the ticket price is $18.

Summary

  • Use the if statement when you want to run a code block based on a condition.
  • Use the if...else statement when you want to run another code block if the condition is not True.
  • Use the if...elif...else statement when you want to check multiple conditions and run the corresponding code block that follows the condition that evaluates to True.

Leave a Reply

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