MODULE 2
Control statements
Flow control
Flow control statements can decide which Python instructions to
execute under which conditions.
Flow control
• In a flowchart, there is usually more than one way to go from
the start to the end.
• The same is true for lines of code in a computer program.
• Flowcharts represent these branching points with diamonds,
while the other steps are represented with rectangles.
• The starting and ending steps are represented with rounded
rectangles.
Boolean Values
• Boolean data type has only two values: True and False.
Comparison Operators
• Comparison operators compare two values and evaluate down to a
single Boolean value
Comparison Operators
Boolean Operators
• The three Boolean operators (and, or, and not) are used to compare
Boolean values.
• Like comparison operators, they evaluate these expressions down to a
Boolean value
Binary Boolean Operators
• The and & or operators always take two Boolean values (or expressions), so
they’re considered binary operators.
• The and operator evaluates an expression to True if both Boolean values
are True; otherwise, it evaluates to False
>>> True and True
True
>>> True and False
False
Boolean Operators
• The or operator evaluates an expression to True if either of the two Boolean
values is True.
• If both are False, it evaluates to False.
>>> False or True
True
>>> False or False
False
The not Operator
• The not operator operates on only one Boolean value (or expression). The not
operator simply evaluates to the opposite Boolean value.
>>> not True
False
>>> not not not not True
True
Mixing Boolean and Comparison Operators
>>> (4 < 5) and (5 < 6)
True
>>> (4 < 5) and (9 < 6)
False
>>> (1 == 2) or (2 == 2)
True
• The computer will evaluate the left expression first, and then it will
evaluate the right expression. When it knows the Boolean value for
each, it will then evaluate the whole expression down to one Boolean
value.
Elements of Flow Control
• Flow control statements often start with a part called the condition,
and all are followed by a block of code called the clause
Conditions
• Condition is a more specific name in the context of flow control
statements.
• Conditions always evaluate down to a Boolean value, True or False.
• A flow control statement decides what to do based on whether its
condition is True or False, and almost every flow control statement
uses a condition.
Elements of Flow Control
Blocks of Code
• Lines of Python code can be grouped together in blocks.
• There are three rules for blocks.
1. Blocks begin when the indentation increases.
2. Blocks can contain other blocks.
3. Blocks end when the indentation decreases to zero or to a containing block’s indentation.
Program Execution
• The program execution (or simply, execution) is a term for the current
instruction being executed.
Flow Control Statements
if Statements
• An if statement’s clause (that is, the block following the if statement)
will execute if the statement’s condition is True. The clause is skipped
if the condition is False.
• In Python, an if statement consists of the following:
• The if keyword
• A condition (that is, an expression that evaluates to True or False)
• A colon
• Starting on the next line, an indented block of code (called the if
clause)
Flow Control Statements
• All flow control statements end with a colon and are followed by a
new block of code (the clause).
Flow Control Statements
else Statements
• The else clause is executed only when the if statement’s condition is
False
• An else statement doesn’t have a condition, and in code, an else
statement always consists of the following:
• The else keyword
• A colon
• Starting on the next line, an indented block of code (called the else
clause)
Flow Control Statements
if name == 'Alice':
print('Hi, Alice.')
else:
print('Hello, stranger.')
Flow Control Statements
elif Statements
• The elif statement is an “else if” statement that always follows an if or
another elif statement.
• It provides another condition that is checked only if any of the
previous conditions were False.
• In code, an elif statement always consists of the following:
• The elif keyword
• A condition (that is, an expression that evaluates to True or False)
• A colon
• Starting on the next line, an indented block of code (called the elif
clause)
Flow Control Statements
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
Flow Control Statements
Flow Control Statements
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
elif age > 100:
print('You are not Alice, grannie.')
e value 3000 before this code is executed. You might expect the
elif age > 2000:
code to print the string 'Unlike you, Alice is not an undead, immortal
vampire.'. Ho
wever, because the age > 100 condition is True (after all, 3000 is greater than
100) u, the string 'You are not Alice, grannie.' is printed, and the rest o
print('Unlike you, Alice is not an undead, immortal vampire.')
Say the age variable contains thf the elif statements are automatically
skipped
Flow Control Statements
while Loop Statements
• The code in a while clause will be executed as long as the while
statement’s condition is True.
• In code, a while statement always consists of the following:
• The while keyword
• A condition (that is, an expression that evaluates to True or False)
• A colon
• Starting on the next line, an indented block of code (called the while clause)
while Loop Statements
• The difference between if and while lies in how they behave.
• At the end of an if clause, the program execution continues after the
if statement.
• But at the end of a while clause, the program execution jumps back to
the start of the while statement.
• The while clause is often called the while loop or just the loop
while Loop Statements
• In the while loop, the condition is
always checked at the start of
each iteration (that is, each time
the loop is executed).
• If the condition is True, then the
clause is executed, and
afterward, the condition is
checked again.
• The first time the condition is
found to be False, the while
clause is skipped.
An Annoying while Loop
An Annoying while Loop
break Statements
• If the execution reaches a break statement, it
immediately exits the while loop’s clause.
• In code, a break statement simply contains the
break keyword.
• The first line creates an infinite loop; it is a
while loop whose condition is always True.
• The program execution will always enter the
loop and will exit it only when a break
statement is executed
break Statements
continue Statements
• Continue statements are used inside loops.
• When the program execution reaches a continue statement, the
program execution immediately jumps back to the start of the loop
and reevaluates the loop’s condition.
for Loops and the range() Function
• The while loop keeps looping while its condition is True, but what if you
want to execute a block of code only a certain number of times?
• You can do this with a for loop statement and the range() function.
• In code, a for statement looks something like for i in range(5): and always
includes the following:
• The for keyword
• A variable name
• The in keyword
• A call to the range() method with up to three integers passed to it
• A colon
• Starting on the next line, an indented block of code (called the for clause)
for Loops and the range() Function
• print('My name is')
• for i in range(5):
• print('Jimmy Five Times (' + str(i) + ')')
The Starting, Stopping, and Stepping Arguments to
range()
The Starting, Stopping, and Stepping
Arguments to range()
Importing Modules
• Python also comes with a set of modules called the standard library.
• Each module is a Python program that contains a related group of
functions that can be embedded in your programs.
• For example, the math module has mathematics related functions,
the random module has random number–related functions, and so
on.
• Before you can use the functions in a module, you must import the
module with an import statement.
• In code, an import statement consists of the following:
• The import keyword
• The name of the module
• Optionally, more module names, as long as they are separated by
commas
Importing Modules
Ending a Program Early with [Link]()
• The program can be terminated, or exit, by calling the [Link]()
function.
• Since this function is in the sys module, you have to import sys before
your program can use it.