#5 Python If Statement

Previous post: https://codinghelp.site/wiki/python/4-python-variables/

Next post: https://codinghelp.site/wiki/python/python-elif-statement/

The “IF” Statement

The if statement is used to compare two values. If the provided expression is met, the statement (indent below) will get run.

“IF” Statement Syntax

The following picture explains the syntax of the if statement:

Expressions in the if statement are written like usual logical conditions from math:

The “statement” must be indented (4 spaces or a tab), else you will get an error.

Examples

Here, “Blueberries have the value of 1!” would get printed as the blue_berries variable has the value of 1. The if statement in english would sound like “IF BLUE_BERRIES IS EQUAL TO ONE PRINT BLUEBERRIES HAVE THE VALUE OF 1” 

Exercise

Write following if statements:

IF BLUE_BERRIES IS EQUAL TO ONE PRINT BLUEBERRIES 

IF BLUE_BERRIES IS NOT EQUAL TO ONE PRINT BLUEBERRIES 

IF BLUE_BERRIES IS GREATER THAN ONE PRINT BLUEBERRIES 

IF BLUE_BERRIES IS LESS THAN ONE PRINT BLUEBERRIES 

IF BLUE_BERRIES IS LESS THAN OR EQUAL TO ONE PRINT BLUEBERRIES

IF BLUE_BERRIES IS GREATER THAN OR EQUAL TO ONE PRINT BLUEBERRIES 

Answer key

image