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