1. Flow Control
if, else, elif, while, break,
continue, for
Ranjana Thakuria
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru
1
2. Flow Control Statements
if Statement
else Statements
elif Statements
while Loop Statements
break Statements
continue Statements
for Loops and the range() Function
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru
2
3. if else Statement
if Statement :
The most common type of flow control statement is the if statement.
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.
else Statements
An if clause can optionally be followed by an else statement.
The else clause is executed only when the if statement’s condition is
False.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru
3
4. if statement consists
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)
else statement consists
The else keyword
A colon
Starting on the next line, an indented block of code (called the else
clause)
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 4
5. If Example:
if name == 'Alice’:
print('Hi, Alice.')
Flowchart:
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 5
6. If –else Example:
if name == 'Alice’:
print('Hi, Alice.')
else:
print('Hello, stranger.')
Flowchart:
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 6
7. Rules for 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.
Example:
if name == 'Mary’:
print('Hello Mary')
if password == 'swordfish’:
print('Access granted.')
else:
print('Wrong password.')
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 7
8. while Loop Statements
• You can make a block of code execute over
and over again with a while statement.
• The code in a while clause will be executed
as long as the while statement’s condition is
True.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 8
9. A while statement always consists:
• 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)
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 9
11. break Statements
• ‘Break’ in Python is a loop control statement.
• There is a shortcut to getting the program execution to
break out of a while loop’s clause early.
• If the execution reaches a break statement, it immediately
exits the while loop’s clause.
• break statement is put inside the loop body (generally
after if condition).
• In code, a break statement simply contains the break
keyword.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 11
12. Example- break:
while True:
print('Please type your
name.’)
name = input()
if name == 'your name’:
break
print('Thank you!')
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 12
13. continue Statements
The continue statement is used to skip the rest of the code
inside a loop for the current iteration only.
Loop does not terminate but continues on with the next
iteration.
When the program execution reaches a continue statement,
the program execution immediately jumps back to the start
of the loop and re-evaluates the loop’s condition.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 13
15. for Loops and the range() Function
To execute a block of code only a certain number of
times
It uses for loop statement and the range() function
together
Eg. for i in range(5):
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru
15
16. for 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)
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 16
17. Example:
# display your name five times:
print('My name is')
for i in range(5):
print('Jimmy Five Times (' + str(i) + ')')
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 17
18. Example 1:
The Starting, Stopping, and Stepping Arguments to range()-
for i in range(12, 16):
print(i)
Explanation:
*The first argument will be where the for loop’s variable
starts.
*The second argument will be up to, but not including, the
number to stop at.
Output:
12
13
14
15
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 18
19. Example 1:
The Starting, Stopping, and Stepping Arguments to range()-
for i in range(0, 10, 2):
print(i)
:
• The range() function can also be called with three
arguments.
• The first two arguments will be the start and stop
values, and the third will be the step argument.
• The step is the amount that the variable is increased by
after each iteration.
Output:
0
2
4
6
8
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 19
20. Example 3:
for i in range(5, -1, -1):
print(i)
Explanation:
The 3rd arguments to range() function
is -1.So backward propagation can be
achieved.
Output:
5
4
3
2
1
0
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 20
21. Importing Modules
• All Python programs can call a basic set of functions called built-in
functions, including the print(), input(), and len() functions.
• 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.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 21
22. 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
• Once you import a module, you can use all the
functions of that module.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 22
23. Example : import
1)
import random for i in range(5):
print(random.randint(1, 10))
Note:
The random.randint() is a
function call.
randint() is function name which
is present in the random module.
2)
import random, sys, os, math
3) An alternative form of the import
statement
from random import *
Note:
composed of the from keyword,
followed by the module name, the
import keyword, and a star;
With this form of import statement,
calls to functions in random will not
need the random. prefix.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 23
24. Ending a Program early with sys.exit()
• This can cause the program to terminate, or exit, by calling the
sys.exit() function.
• Since this function is in the sys module, you have to import sys before
your program can use it.
Example: import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed ' + response + '.')
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 24
25. Bibliography:
1. Al Sweigart,“Automate the Boring Stuff with Python”,1 stEdition, No Starch Press, 2015. (Available
under CC-BY-NC-SA license at https://automatetheboringstuff.com/) (Chapters 1 to 18, except 12) for
lambda functions use this link: https://www.learnbyexample.org/python-lambda-function/
2. Allen B. Downey, “Think Python: How to Think Like a Computer Scientist”, 2 nd Edition, Green Tea
Press, 2015. (Available under CC-BY-NC license at
http://greenteapress.com/thinkpython2/thinkpython2.pdf (Chapters 13, 15, 16, 17, 18) (Download
pdf/html files from the above link)
3. Introduction to Python Programming - 22PLC15B/25B (Module 1) notes Prepared by, Mrs. Divyaraj G
N, Assistant Professor, CSE, SVCE, Bengaluru
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 25