Subscribe Contact

Home  »  Chapter 3 : Decisions
if Statements

Overview

Very often in programming, we need to make decisions. Decisions can be simple or very complex. Python provides several ways to make decisions in the language. One of the first decision constructs in Python to consider is the if statement.

General Form

The general form of the if statement looks like this:

if condition:
    code_block
Concept: Indentation

In Python, indentation is significant for certain syntax structures. Notice in the general form above, that code_block is indented. This is a requirement in Python if statements. All code that is part of the code block must be indented, this is how the interpreter knows what code is included in the block of code to run when the if statement condition evaluates to True.

The flowchart depicts the general form of the if statement as well. The execution flow represents the flow of the program up to the point of the if condition. When the if condition is evaluated by the interpreter, the result will be either True or False. If the result is True then execution flow moves into the code_block and that code runs. When it is finished flow continues to the next statement after the indented code_block. If, however, the if condition returns False, then the code_block is skipped and execution flow continues to the next statement after the indented code_block. Let's take a look at an example:

low = 1
high = 10
secret_number = 8
print("Program started.")
guess = int(input("Enter your guess between " + str(low) + " and " + str(high) + ": "))
if guess == secret_number:
    print("You guessed right!")
print("Program complete.")

And running the program looks like this:

Program started.
Enter your guess between 1 and 10: 8
You guessed right!
Program complete.

Notes: In the example above we used the double-equal comparison symbol in our if statement condition. That symbol is one of the numerous comparison operators that exist in Python. Here is a list of all of them:

Comparison Operators

Operator Comparison Description
== Equal The double-equal symbol compares the value with the value. If they are equal, then it results in True. If the comparison is not equal, then it results in False.
Note: A common mistake in coding is to use the single-equal sign (=) instead of the double-equal (==). This mistake can cause significant problems because instead of making a comparison, the single-equal assigns the values of the right side to the left side.

Example:
x = 10
y = 20
if x == y:
Condition Result: False

!= Not Equal This comparison operator is the opposite of the equal operator above. It compares the value with the value. If they are not equal, then it results in True. If the comparison is equal, then it results in False.

Example:
x = 10
y = 20
if x != y:
Condition Result: True

> Greater Than This comparison operator checks if the value is greater than (determined by the data type) the value. If the is greater, then it results in True. If the is not greater than the right side, then it results in False.

Example:
x = 10
y = 20
if x > y:
Condition Result: False

< Less Than This comparison operator checks if the value is less than (determined by the data type) the value. If the is less, then it results in True. If the is not less than the right side, then it results in False.

Example:
x = 10
y = 20
if x < y:
Condition Result: True

>= Greater Than or Equal To This comparison operator checks if the value is greater or equal to (determined by the data type) the value. If the is greater or equal to, then it results in True. If the is not greater or equal to than the right side, then it results in False.

Example:
x = 10
y = 20
if x >= y:
Condition Result: False

<= Less Than or Equal To This comparison operator checks if the value is less than or equal to (determined by the data type) the value. If the is less than or equal to, then it results in True. If the is not less than or equal to the right side, then it results in False.

Example:
x = 10
y = 20
if x <= y:
Condition Result: True

Compound Conditions

The if statement examples provided above contain a single condition (x == y for example), however, our conditions can be more complex and make several comparisons in one compound condition. To create more complex conditions, we need a way to connect more than one condition together, which we do with logical operators. Here is a list of the logical operators in Python:

Logical Operators
Operator Description
and The and operator compares the condition to its left with the condition to its right, if both conditions are True then the compound condition is True. If either of the two conditions or both, are False, then the compound condition is False.

Examples:
x = 10
y = 20
z = 30
if (x < y) and (z > x):
Condition Result: True
x = 10
y = 20
z = 30
if (x < y) and (z < x):
Condition Result: False
x = 10
y = 20
z = 30
if (x < y) and (z > y) and (z == (x + y)):
Condition Result: True

or The or operator compares the condition to its left with the condition to its right, if either of the conditions is True then the compound condition is True. In a compound condition when more than two conditions, each separated by an or, as long as any one of the conditions is True then the compound condition is True.

Examples:
x = 10
y = 20
z = 30
if (x < y) or (z > x):
Condition Result: True (both are True)
x = 10
y = 20
z = 30
if (x < y) or (z < x):
Condition Result: True (the first condition is True)
x = 10
y = 20
z = 30
if (x < y) or (z > y) or (z == (x + y)):
Condition Result: True (at least one condition is True)

not The not operator reverses the result of the compound condition, so if the compound returns True the not operator will reverse it to False, and if the compound condition returns False the not operator will reverse it to True. In the examples below, notice that the compound condition is placed inside parenthesis and the not operator precedes that set of parenthesis.

Examples:
x = 10
y = 20
z = 30
if not((x < y) or (z > x)):
Condition Result: False
x = 10
y = 20
z = 30
if not((x < y) or (z < x)):
Condition Result: False
x = 10
y = 20
z = 30
if not((x < y) or (z > y) or (z == (x + y))):
Condition Result: False

Practice Problem

Problem 1

if Statement w/Compound Condition: Write a Python program that prompts the user for their age. If their age is between 40 and 50 inclusive, print "You are middle-aged."

Please enter your age: 43
You are middle-aged.




The if statement works well when we have a single decision to make. That decision can be a single condition or a compound condition. However, the simple if only executes code if the condition result is True. We often need one code_block if the result is True and a different block of code if the result is False. To accomplish this, we will learn the if-else statement on the next page.



 


«  Previous : Decisions : Booleans & Conditions
Next : Decisions : if-else Statements  »



© 2023 John Gordon
Cascade Street Publishing, LLC