
Syntax: If Statements
Creating conditional code with Python if statements.
Python if Conditionals
In Python, the if statement is used to execute a block of code if the conditional expression evaluates to True. If the conditional expression given to the if statement evaluates to True, the indented code block which follows the if statement will run. Once the code in the indented block completes running, the unindented code after the code block will begin to run.
Notice the example below:
if True:
print("This code is inside the conditional")
print("This code is also inside the conditional")
print("This code is not inside the conditional")
> "This code is inside the conditional"
> "This code is also inside the conditional"
> "This code is not inside the conditional"
As you can see above, because the conditional expression, (in this case True) evaluates to True, the indented code with the print functions indside the conditional both run, and then the code outside the conditional runs.
In the next example, you will see what happens when the conditional expression given to the if statement evaluates to False:
if False:
print("This code is inside the conditional")
print("This code is also inside the conditional")
print("This code is not inside the conditional")
> "This code is not inside the conditional"
As you can see above, because the conditional expression given to the if statement evaluated to False, the indented code inside the conditional does not run, and only run print statement runs printing that "this code is not inside the conditional".
Let's look at another example, this time with a different conditional expression:
value = 5
if value < 10:
print("This code runs if the value is less than 10.")
print("This code runs no matter what.")
> "This code runs if the value is less than 10."
> "This code runs no matter what."
Note that the expression above is checking if value is less than 10, and here because it is, the indented print function runs.
But, if the expression does not evaluate to True, the indented code will not run:
value = 15
if value < 10:
print("This code runs if the value is less than 10.")
print("This code runs no matter what.")
> "This code runs no matter what."
So because this time value was 15, which is not less than 10, the indented code does not run, and only the print statement after the indented code runs.
The big thing to remember is that an if statement is designed to run a block of code only if the conditional expression given to the if statement evaluates to True.
One last thing to point out here, both truthy and falsy values can be used for the conditional expression of an if statement. For example:
value = 0
if value:
print("This code runs if the value variable contains a truthy value.")
print("This code runs no matter what.")
> "This code runs no matter what."
Notice that the indented code did not run in the above example. That is because the value 0 is a falsy value in Python. Any falsy value given as a conditional expression will evaluate to False.
- Note, truthy and falsiness are not the same as True and False.
- truthy values are things like strings, integers, floats, etc
- falsy values are things like 0, 0.0, None, undefined
Finally, let's see an expression with a truthy value:
value = "hello"
if value:
print("This code runs if the value variable contains a truthy value.")
print("This code runs no matter what.")
> "This code runs if the value variable contains a truthy value."
> "This code runs no matter what."
As you can see, because the string hello is a truthy value, the indented code of the conditional will run.
Practice: if Conditionals
Based on the information above. Complete the following steps:
-
Create a variable named
ageand assign it a numeric value that represents your age. -
Create an
ifstatement which checks if the value in the age variable is less than 100. -
Add indented code inside the if statement which prints a messege stating that the value of the age variable is less than 100.
-
Confirm that the indented print statement prints correctly.
HINT*: Click here for a hint
> The value of age is less than 100.
SOLUTION*: Click here for an example solution
age = 42 if age < 100: print("The value of age is less than 100.") -
Next, repeat the above steps with the value of age greater than 100, and confirm the indented code does not run.
HINT*: Click here for a hint
>
SOLUTION*: Click here for an example solution
age = 103 if age < 100: print("The value of age is less than 100.")
When the above is completed and you are getting the proper output, you are ready to move onto the next exercise!