Flow Control & Statements
Python control structures
 According to the structure theorem, any computer
program can be written using the basic control
structures .
 A control structure (or flow of control) is a block of
programming that analyses variables and
chooses a direction in which to go based on given
parameters. In simple sentence, a control
structure is just a decision that the computer
makes. So, it is the basic decision-making
process
Flow of control
Flow of control through any given program is
implemented with three basic types of control
structures:
 Sequential,
 Selection
 Repetition
Selection
 Python if statements
Python if..else statements
 The else statement is to specify a block of code
to be executed, if the condition in the if statement
is false. Thus, the else clause ensures that a
sequence of statements is executed.
if..elif..else statements
Python Loop / Iteration
Statements
 Loops are one of the most important features in
computer programming languages . As the
name suggests is the process that get repeated
again and again
Every loop has 3 parts:
 Initialization
 Condition
 Updation
Iteration Statements
What is for loop in Python?
 The for loop in Python is used to iterate over a
sequence (list, tuple, string) or other iterable
objects.
 Iterating over a sequence is called traversal.
Syntax of for Loop
for val in sequence:
Body of for loop
The range() function
 We can generate a sequence of numbers
using range() function. range(10) will generate
numbers from 0 to 9 (10 numbers).
 We can also define the start, stop and step size
as range(start, stop,step_size). step_size defaults
to 1 if not provided.
Examples :
 print(range(10))
 Print(list(range(10))
 print(list(range(2, 8)))
 print(list(range(2, 20, 3)))
for loop with else
 A for loop can have an optional else block as well.
The else part is executed if the items in the
sequence used in for loop exhausts.
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
What is while loop in Python?
 The while loop in Python is used to iterate over a
block of code as long as the test expression
(condition) is true.
Python break and continue
 In Python, break and continue statements can alter
the flow of a normal loop.
 Loops iterate over a block of code until the test
expression is false, but sometimes we wish to
terminate the current iteration or even the whole loop
without checking test expression.
 The break statement terminates the loop containing it.
Control of the program flows to the statement
immediately after the body of the loop.
 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.
Python break statement
Syntax of break
break
The working of break statement in for
loop and while loop is shown below.
Python continue statement
The working of continue statement in
for and while loop is shown below.

Types of Statements in Python Programming Language

  • 1.
    Flow Control &Statements
  • 2.
    Python control structures According to the structure theorem, any computer program can be written using the basic control structures .  A control structure (or flow of control) is a block of programming that analyses variables and chooses a direction in which to go based on given parameters. In simple sentence, a control structure is just a decision that the computer makes. So, it is the basic decision-making process
  • 3.
    Flow of control Flowof control through any given program is implemented with three basic types of control structures:  Sequential,  Selection  Repetition
  • 4.
  • 5.
    Python if..else statements The else statement is to specify a block of code to be executed, if the condition in the if statement is false. Thus, the else clause ensures that a sequence of statements is executed.
  • 6.
  • 7.
    Python Loop /Iteration Statements  Loops are one of the most important features in computer programming languages . As the name suggests is the process that get repeated again and again Every loop has 3 parts:  Initialization  Condition  Updation
  • 8.
  • 9.
    What is forloop in Python?  The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects.  Iterating over a sequence is called traversal. Syntax of for Loop for val in sequence: Body of for loop
  • 10.
    The range() function We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers).  We can also define the start, stop and step size as range(start, stop,step_size). step_size defaults to 1 if not provided. Examples :  print(range(10))  Print(list(range(10))  print(list(range(2, 8)))  print(list(range(2, 20, 3)))
  • 11.
    for loop withelse  A for loop can have an optional else block as well. The else part is executed if the items in the sequence used in for loop exhausts. digits = [0, 1, 5] for i in digits: print(i) else: print("No items left.")
  • 12.
    What is whileloop in Python?  The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
  • 13.
    Python break andcontinue  In Python, break and continue statements can alter the flow of a normal loop.  Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression.  The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.  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.
  • 14.
  • 15.
    The working ofbreak statement in for loop and while loop is shown below.
  • 16.
  • 17.
    The working ofcontinue statement in for and while loop is shown below.