0% found this document useful (0 votes)
44 views4 pages

Control Flow Cheatsheet

The document summarizes Python control flow statements including comparison operators (==, !=, <, >, <=, >=), Boolean operators (and, or, not), if/else statements, and boolean values. Key comparison operators check for equality and inequality. The and operator returns True if both expressions are True, or returns False. The or operator returns True if one or more expressions are True. The not operator inverts the Boolean value of its operand. If/else statements execute code based on expression evaluations.

Uploaded by

Pranay Bagde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
44 views4 pages

Control Flow Cheatsheet

The document summarizes Python control flow statements including comparison operators (==, !=, <, >, <=, >=), Boolean operators (and, or, not), if/else statements, and boolean values. Key comparison operators check for equality and inequality. The and operator returns True if both expressions are True, or returns False. The or operator returns True if one or more expressions are True. The not operator inverts the Boolean value of its operand. If/else statements execute code based on expression evaluations.

Uploaded by

Pranay Bagde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Cheatsheets / Learn Python 3

Control Flow
Equal Operator ==
The equal operator, == , is used to compare two values,
variables or expressions to determine if they are the # Equal operator
same.
If the values being compared are the same, the operator if 'Yes' == 'Yes':
returns True , otherwise it returns False . # evaluates to True
The operator takes the data type into account when print('They are equal')
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')

Not Equals Operator !=


The Python not equals operator, != , is used to compare
two values, variables or expressions to determine if they # Not Equals Operator
are NOT the same. If they are NOT the same, the operator
returns True . If they are the same, then it returns if "Yes" != "No":
False . # evaluates to True
The operator takes the data type into account when print("They are NOT equal")
making the comparison so a value of 10 would NOT be
equal to the string value "10" and the operator would
val1 = 10
val2 = 20
return True . If expressions are used, then they are
evaluated to a value of True or False before the
if val1 != val2:
comparison is made by the operator.
print("They are NOT equal")

if (10 > 1) != (10 > 1000):


# True != False
print("They are NOT equal")

/
Comparison Operators
In Python, relational operators compare two values or
expressions. The most common ones are: a = 2
b = 3

< less than a < b # evaluates to True

> greater than a > b # evaluates to False
a >= b # evaluates to False

<= less than or equal to a <= b # evaluates to True

>= greater than or equal too a <= a # evaluates to True

If the relation is sound, then the entire expression will


evaluate to True . If not, the expression evaluates to
False .

and Operator
The Python and operator performs a Boolean
comparison between two Boolean values, variables, or True and True # Evaluates to True
expressions. If both sides of the operator evaluate to True and False # Evaluates to False
True then the and operator returns True . If either False and False # Evaluates to False
side (or both sides) evaluates to False , then the and 1 == 1 and 1 < 2 # Evaluates to True
operator returns False . A non-Boolean value (or
1 < 2 and 3 < 1 # Evaluates to False
"Yes" and 100 # Evaluates to True
variable that stores a value) will always evaluate to True
when used with the and operator.

or Operator
The Python or operator combines two Boolean
expressions and evaluates to True if at least one of the
True or True # Evaluates to True
True or False # Evaluates to True
expressions returns True . Otherwise, if both
False or False # Evaluates to False
expressions are False , then the entire expression
1 < 2 or 3 < 1 # Evaluates to True
evaluates to False .
3 < 1 or 1 > 6 # Evaluates to False
1 == 1 or 1 < 2 # Evaluates to True

not Operator
The Python Boolean not operator is used in a Boolean
expression in order to evaluate the expression to its not True # Evaluates to False
inverse value. If the original expression was True , not False # Evaluates to True
including the not operator would make the expression
1 > 2 # Evaluates to False
not 1 > 2 # Evaluates to True
False , and vice versa.
1 == 1 # Evaluates to True
not 1 == 1 # Evaluates to False

/
if Statement
The Python if statement is used to determine the
execution of code based on the evaluation of a Boolean # if Statement
expression.
test_value = 100

If the if statement expression evaluates to
True , then the indented code following the if test_value > 1:
statement is executed. # Expression evaluates to True
● print("This code is executed!")
If the expression evaluates to False then the
indented code following the if statement is
if test_value > 1000:
skipped and the program executes the next line of
code which is indented at the same level as the # Expression evaluates to False
print("This code is NOT executed!")
if statement.

print("Program continues at this point.")

else Statement
The Python else statement provides alternate code to
execute if the expression in an if statement evaluates
# else Statement

to False .
test_value = 50
The indented code for the if statement is executed if
the expression evaluates to True . The indented code
if test_value < 1:
immediately following the else is executed only if the print("Value is < 1")
expression evaluates to False . To mark the end of the else:
else block, the code must be unindented to the same print("Value is >= 1")
level as the starting if line.
test_string = "VALID"

if test_string == "NOT_VALID":
print("String equals NOT_VALID")
else:
print("String equals something else!")

Boolean Values
Booleans are a data type in Python, much like integers,
oats, and strings. However, booleans only have two is_true = True
values: is_false = False


True print(type(is_true))

False # will output: <class 'bool'>

Speci cally, these two values are of the bool type.


Since booleans are a data type, creating a variable that
holds a boolean value is the same as with other data
types.

/
elif Statement
The Python elif statement allows for continued
checks to be performed after an initial if statement.
# elif Statement

An elif statement di ers from the else statement


because another expression is provided to be checked,
pet_type = "fish"

just as with the initial if statement.


if pet_type == "dog":
If the expression is True , the indented code following
print("You have a dog.")
the elif is executed. If the expression evaluates to
elif pet_type == "cat":
False , the code can continue to an optional else print("You have a cat.")
statement. Multiple elif statements can be used elif pet_type == "fish":
following an initial if to perform a series of checks. # this is performed
Once an elif expression evaluates to True , no print("You have a fish")
further elif statements are executed. else:
print("Not sure!")

Handling Exceptions in Python


A try and except block can be used to handle error
in code block. Code which may raise an error can be def check_leap_year(year):
written in the try block. During execution, if that code is_leap_year = False
block raises an error, the rest of the try block will
if year % 4 == 0:
is_leap_year = True
cease executing and the except code block will
execute.
try:
check_leap_year(2018)
print(is_leap_year)
# The variable is_leap_year is declared
inside the function
except:
print('Your code raised an error!')

You might also like