Search⌘ K

The if statement

Explore how to use Python's if statement to perform conditional actions based on true or false evaluations. Understand proper indentation, the use of else and elif for multiple conditions, and how to incorporate user input in decision-making. This lesson prepares you to write basic condition-based programs in Python.

We'll cover the following...

Python’s if statement is pretty easy to use. Let’s spend a few minutes looking at some examples to better acquaint ourselves with this construct.

Python
if 2 > 1:
print("This is a True statement!")
#This is a True Statement!

This conditional tests the “truthfulness” of the following statement: 2 > 1. Since this statement evaluates to True, it will cause the last line in the example to print to the screen or standard out (stdout).


Python Cares About Space

The Python language cares a lot about space. You will notice that in our conditional statement above, we indented the code inside the if statement four spaces. This is ...