0% found this document useful (0 votes)
79 views38 pages

002.1 Python - Revision Tour

The document discusses various control flow statements in Python including empty statements, simple statements, compound statements, if statements, else statements, elif statements, nested if statements, for loops, while loops, break statements, continue statements, and else clauses with loops. It also covers the range() function and generating random numbers.

Uploaded by

Arushi Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views38 pages

002.1 Python - Revision Tour

The document discusses various control flow statements in Python including empty statements, simple statements, compound statements, if statements, else statements, elif statements, nested if statements, for loops, while loops, break statements, continue statements, and else clauses with loops. It also covers the range() function and generating random numbers.

Uploaded by

Arushi Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Revision Tour

Control Flow Statements in Python


Types o f Statement in Python

 Statements are the instructions given to


computer to perform any task. Task may be
simple calculation, checking the condition or
repeating action.
 Python supports 3 types of statement:
 Empty statement
 Simple statement
 Compound statement
Empty Statement

 It is the simplest statement i.e. a statement


which does nothing. It is written by using
keyword – pass
 Whenever python encounters pass it does
nothing and moves to next statement in flow
of control
 Required where syntax of python requires
presence of a statement but where the logic
of program doesn’t require anything to be
done. More detail will be explored with loop.
Simple Statement

 Any single executable statement in Python is


simple statement. For e.g.
 name = input(“enter your name “)
 print(name)
Compound Statement

 It represent group of statements executed as


unit. The compound statement of python are
written in a specific pattern:

Compound_Statement_Header :
indented_body containing multiple
simple or compound statements
Compound Statement has:

 Header which begins with keyword/function


and ends with colon(:)
 A body contains of one or more python
statements each indented inside the header
line. All statement in the body or under any
header must be at the same level of
indentation.
Statement Flow Control

 In python program statement may execute in


a sequence, selectively or iteratively. Python
programming supports 3 Control Flow
statements:
1. Sequence
2. Selection
3. Iteration
i f Statement o f Python
 ‘if’ statement of python is used to execute
statements based on condition. It tests the
condition and if the condition is true it
perform certain action, we can also provide
action for false situation.
 if statement in Python is of many forms:
 if without false statement
 if with else
 if with elif
 Nested if
Simple “ i f ”
 In the simplest form if statement in Python
checks the condition and execute the
statement if the condition is true and does
nothing if the condition is false.
 Syntax: All statement
belonging to if
if condition: must have same
indentation level
Statement1
Statements ….
* * if statement is compound statement having
header and a body containing indented
statement.
Points to remember with “ i f ”

 It must contain valid condition which


evaluates to either True or False
 Condition must followed by Colon (:) , it is
mandatory
 Statement inside if must be at same
indentation level.
Input monthly sale of employee and give bonus of 10% if
sale is more than 50000 otherwise bonus will be 0
bonus = 0
sale = int(input("Enter Monthly Sales :"))
if sale>50000:
bonus=sale * 10 /100
print("Bonus = " + str(bonus))
if with else

 if with else is used to test the condition and if


the condition is True it perform certain
action and alternate course of action if the
condition is false.
 Syntax:
if condition:
Statements
else:
Statements
Input Age of person and print whether the person is
eligible for voting or not

age = int(input("Enter your age "))


if age>=18:
print("Congratulation! you are eligible for voting ")
else:
print("Sorry! You are not eligible for voting")
if with elif
 if with elif is used where multiple chain of condition is to
be checked. Each elif must be followed by condition: and
then statement for it. After every elif we can give else
which will be executed if all the condition evaluates to
false
 Syntax:
if condition:
Statements
elif condition:
Statements
elif condition:
Statements
else:
Statement
Input temperature of water and print its physical state

temp = int(input("Enter temperature of water "))


if temp>100:
print("Gaseous State")
elif temp<0:
print("Solid State")
else:
print("Liquid State")
Nested i f
 In this type of “if” we put if within another if as a
statement of it. Mostly used in a situation where we
want different else for each condition. Syntax:
if condition1:
if condition2:
statements
else:
statements
elif condition3:
statements
else:
statements
Python Loop Statements

 To carry out repetition of statements Python


provide 2 loop statements
 Conditional loop (while)
 Counting loop (for)
range() function

 Before we proceed to for loop let us


understand range() function which we will
use in for loop to repeat the statement to n
number of times.
 Syntax:
 range(lower_limit, upper_limit)
 The range function generate set of values
from lower_limit to upper_limit-1
range() function

 For e.g.
 range(1,10) will generate set of values from
1-9
 range(0,7) will generate [0-6]
 Default step value will be +1 i.e.
 range(1,10) means (1,2,3,4,5,6,7,8,9)
range() function

 To change the step value we can use third


parameter in range() which is step value
 For e.g.
 range(1,10,2) now this will generate value
[1,3,5,7,9]
 Step value can be in –ve also to generate set
of numbers in reverse order.
 range(10,0) will generate number
as [10,9,8,7,6,5,4,3,2,1]
range() function

 To create list from ZERO(0) we can use


 range(10) it will generate
[0,1,2,3,4,5,6,7,8,9]
Operators in and not in

 The operator in and not in is used in for loop


to check whether the value is in the range /
list or not
 For e.g.
>>> 5 in [1,2,3,4,5]
True
>>> 5 in [1,2,3,4]
False
>>>’a’ in ‘apple’
True
>>>’national’ in ‘international’
True
f o r loop

 for loop in python is used to create a loop to


process items of any sequence like List, Tuple,
Dictionary, String
 It can also be used to create loop of fixed
number of steps like 5 times, 10 times, n
times etc using range() function.
Example – f o r loop with L i s t

School=["Principal","PGT","TGT","PRT"]
for sm in School:
print(sm)

Example – for loop with Tuple


Code=(10,20,30,40,50,60)
for cd in Code:
print(cd)
Let us under st and how f or l oop
works
Code=(10,20,30,40,50,60)
for cd inCode:
print(cd)
for loop with string

for ch in ‘Plan’:
print(ch)

The above loop product output


P
l
a
n
for with range()
Let us create a loop to print all the natural number from 1 to
100

for i in range(1,101):
print(i,end='\t')

* * here end=‘\t’ will cause output to appear without


changing line and give one tab space between next output.
while loop

 While loop in python is conditional loop


which repeat the instruction as long as
condition remains true.
 It is entry-controlled loop i.e. it first check the
condition and if it is true then allows to enter
in loop.
 while loop contains various loop elements:
initialization, test condition, body of loop
and update statement
while loop elements

1. Initialization : it is used to give starting


value in a loop variable from where to
start the loop
2. Test condition : it is the condition or last
value up to which loop will be executed.
3. Body of loop : it specifies the
action/statement to repeat in the loop
4. Update statement : it is the increase or
decrease in loop variable to reach the
test condition.
Example o f simple while loop

i=1
while i<=10:
print(i)
i+=1
Jump Statements – break & continue

break statement in python is used to terminate


the containing loop for any given condition.
Program resumes from the statement
immediately after the loop

Continue statement in python is used to skip


the statements below continue statement
inside loop and forces the loop to continue
with next value.
Example – break
for i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop Over”)

The above code produces output


1
2
3
4
5
Loop Over
when the value of i reaches to 6 condition will become True and
loop will end and message “Loop Over” will be printed
Example – continue
for i in range(1,20):
if i % 6 == 0:
continue
print(i, end = ‘ ‘)
print(“Loop Over”)

The above code produces output


1 2 3 4 5 78 9 10 11 13 14 15 16 17 19
Loop Over
when the value of i becomes divisible from 6, condition will
becomes True and loop will skip all the statement below
continue and continues in loop with next value of iteration.
Loop . . else . . Statement

 Loop in python provides else clause with loop


also which will execute when the loop
terminates normally i.e. when the test
condition fails in while loop or when last value
is executed in for loop but not when break
terminates the loop
Example ( “ e l s e ” with while)

i=1 Output
while i<=10: 1
2
print(i) 3
4
i+=1 5
6
else: 7
8
print("Loop Over") 9
10
Loop Over
Example ( “ e l s e ” with f o r )
names=["allahabad","lucknow","varanasi","kanpur","agra","gha
ziabad" ,"mathura","meerut"]
city = input("Enter city to earch: ")
for c in names:
if c == city:
print(“City Found")
Break Output
else: Enter city to search : varanasi
City Found
print("Not found") Enter city to search : unnao
Not found
Generating Random numbers

 Python allows us to generate random number


between any range
 To generate random number we need to
import random library
 Syntax: (to generate random number)
 random.randint(x,y)
 it will generate random number between x to y
 For e.g. : random.randint(1,10)

You might also like