Learn Python 3 - Control Flow Cheatsheet - Codecademy
Learn Python 3 - Control Flow Cheatsheet - Codecademy
Control Flow
SyntaxError
A SyntaxError is reported by the Python interpreter when some portion of the code age = 7 + 5 = 4
is incorrect. This can include misspelled keywords, missing or too many brackets or
parentheses, incorrect operators, missing or too many quotation marks, or other
conditions. File "<stdin>", line 1
SyntaxError: can't assign to operator
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-control-flow/cheatsheet 1/7
09/12/2023, 08:07 Learn Python 3: Control Flow Cheatsheet | Codecademy
elif Statement
The Python elif statement allows for continued checks to be performed after an # elif Statement
initial if statement. An elif statement differs from the else statement because
another expression is provided to be checked, just as with the initial if statement.
If the expression is True , the indented code following the elif is executed. If the
pet_type = "fish"
expression evaluates to False , the code can continue to an optional else statement.
Multiple elif statements can be used following an initial if to perform a series of if pet_type == "dog":
checks. Once an elif expression evaluates to True , no further elif statements are
print("You have a dog.")
executed.
elif pet_type == "cat":
print("You have a cat.")
elif pet_type == "fish":
# this is performed
print("You have a fish")
else:
print("Not sure!")
or Operator
The Python or operator combines two Boolean expressions and evaluates to True if True or True # Evaluates to True
at least one of the expressions returns True . Otherwise, if both expressions are
True or False # Evaluates to True
False , then the entire expression evaluates to False .
False or False # Evaluates to False
1 < 2 or 3 < 1 # Evaluates to True
3 < 1 or 1 > 6 # Evaluates to False
1 == 1 or 1 < 2 # Evaluates to True
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-control-flow/cheatsheet 2/7
09/12/2023, 08:07 Learn Python 3: Control Flow Cheatsheet | Codecademy
Equal Operator ==
The equal operator, == , is used to compare two values, variables or expressions to # Equal operator
determine if they are the same.
If the values being compared are the same, the operator returns True , otherwise it
returns False . if 'Yes' == 'Yes':
The operator takes the data type into account when making the comparison, so a # evaluates to True
string value of "2" is not considered the same as a numeric value of 2 . print('They are equal')
c = '2'
d = 2
if c == d:
print('They are equal')
else:
print('They are not equal')
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-control-flow/cheatsheet 3/7
09/12/2023, 08:07 Learn Python 3: Control Flow Cheatsheet | Codecademy
The Python not equals operator, != , is used to compare two values, variables or # Not Equals Operator
expressions to determine if they are NOT the same. If they are NOT the same, the
operator returns True . If they are the same, then it returns False .
The operator takes the data type into account when making the comparison so a value if "Yes" != "No":
of 10 would NOT be equal to the string value "10" and the operator would return # evaluates to True
True . If expressions are used, then they are evaluated to a value of True or False print("They are NOT equal")
before the comparison is made by the operator.
val1 = 10
val2 = 20
if val1 != val2:
print("They are NOT equal")
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-control-flow/cheatsheet 4/7
09/12/2023, 08:07 Learn Python 3: Control Flow Cheatsheet | Codecademy
Comparison Operators
In Python, relational operators compare two values or expressions. The most common a = 2
ones are:
b = 3
< less than
> greater than a < b # evaluates to True
<= less than or equal to a > b # evaluates to False
>= greater than or equal too a >= b # evaluates to False
If the relation is sound, then the entire expression will evaluate to True . If not, the
a <= b # evaluates to True
expression evaluates to False .
a <= a # evaluates to True
if Statement
The Python if statement is used to determine the execution of code based on the # if Statement
evaluation of a Boolean expression.
If the if statement expression evaluates to True , then the indented code
following the statement is executed. test_value = 100
If the expression evaluates to False then the indented code following the if
statement is skipped and the program executes the next line of code which is
if test_value > 1:
indented at the same level as the if statement.
# Expression evaluates to True
print("This code is executed!")
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-control-flow/cheatsheet 5/7
09/12/2023, 08:07 Learn Python 3: Control Flow Cheatsheet | Codecademy
else Statement
The Python else statement provides alternate code to execute if the expression in an # else Statement
if statement evaluates to False .
The indented code for the if statement is executed if the expression evaluates to
True . The indented code immediately following the else is executed only if the test_value = 50
expression evaluates to False . To mark the end of the else block, the code must be
unindented to the same level as the starting if line. if test_value < 1:
print("Value is < 1")
else:
print("Value is >= 1")
test_string = "VALID"
if test_string == "NOT_VALID":
print("String equals NOT_VALID")
else:
print("String equals something else!")
and Operator
The Python and operator performs a Boolean comparison between two Boolean True and True # Evaluates to True
values, variables, or expressions. If both sides of the operator evaluate to True then
True and False # Evaluates to False
the and operator returns True . If either side (or both sides) evaluates to False ,
then the and operator returns False . A non-Boolean value (or variable that stores a
False and False # Evaluates to False
value) will always evaluate to True when used with the and operator. 1 == 1 and 1 < 2 # Evaluates to True
1 < 2 and 3 < 1 # Evaluates to False
"Yes" and 100 # Evaluates to True
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-control-flow/cheatsheet 6/7
09/12/2023, 08:07 Learn Python 3: Control Flow Cheatsheet | Codecademy
Boolean Values
Booleans are a data type in Python, much like integers, floats, and strings. However, is_true = True
booleans only have two values:
is_false = False
True
False
Specifically, these two values are of the bool type. Since booleans are a data type, print(type(is_true))
creating a variable that holds a boolean value is the same as with other data types.
# will output: <class 'bool'>
not Operator
The Python Boolean not operator is used in a Boolean expression in order to evaluate not True # Evaluates to False
the expression to its inverse value. If the original expression was True , including the
not False # Evaluates to True
not operator would make the expression False , and vice versa.
1 > 2 # Evaluates to False
not 1 > 2 # Evaluates to True
1 == 1 # Evaluates to True
not 1 == 1 # Evaluates to False
Print Share
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-control-flow/cheatsheet 7/7