0% found this document useful (0 votes)
22 views26 pages

Control Statements

The document provides an overview of control statements in Python, including if, else, elif, while, and for loops, along with their syntax and examples. It emphasizes the importance of indentation in defining code blocks and discusses nested loops and the use of else with loops. Additionally, it includes practice exercises to reinforce understanding of these concepts.

Uploaded by

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

Control Statements

The document provides an overview of control statements in Python, including if, else, elif, while, and for loops, along with their syntax and examples. It emphasizes the importance of indentation in defining code blocks and discusses nested loops and the use of else with loops. Additionally, it includes practice exercises to reinforce understanding of these concepts.

Uploaded by

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

Control statements

Control statements are statements which control or change the flow of


execution. The following are the control statements available in python.
i.if statement
ii.if … else statement
iii.if … elif statement
iv.while loop
v.for loop
vi.else suite
vii.break statement
viii.continue statement
ix.pass statement
x.assert statement
xi.return statement

1
Indentation
•Indentation refers to spaces that are used in the beginning of a statement.
•The statement with same indentation belong to same group called a
Suite.
•Python uses 4 spaces but it can be increased or decreased by the
programmers.
Example
if x==1:
print('a')
print('b')
if(y==2):
print('c')
print('d')
print('end')

2
i. if Statement
The if statement is a conditional statement in Python used to
determine whether a block of code will be executed. If the program
finds the condition defined in the if statement true, it will execute
the code block inside the if statement.

Syntax:
if condition:
# execute code block
Example
if 10 > 5:
print("10 greater than 5")
print("Program ended") 3
ii. if-else Statement
As discussed above, the if statement executes the code block when the condition
is true. Similarly, the else statement works in conjuncture with the if statement to
execute a code block when the defined if condition is false.

Syntax:
if condition:
# execute code if condition is true
else:
# execute code if condition if False

4
Example:
To know if a given number is even or odd
x=10
if x%2==0:
print(x," is even number")
else:
print(x,"is not even number")

5
Example:
To know if a given number is even or odd
x=int(input("Enter a Number:"))
if x%2==0:
print(x," is even number")
else:
print(x,"is not even number")

Practice: Program to test whether a given number is between 1 and


10

6
iii. if-elif-else ladder
•The elif statement is used to check for multiple conditions and execute
the code block within if any of the conditions are evaluated to be true.
•The elif statement is similar to the else statement in that it is optional.
Still, unlike the else statement, multiple elif statements can be in a block
of code following an if statement.
if condition1:
# execute this statement1
elif condition2:
# execute this statement2
elif condition3:
# execute this statement3
else:
statement 4
# if non of the above conditions evaluate to True execute this statement
7
Ex: Program to know if a given number is zero , positive or
negative
num=-5
if num==0:
print(num,"is zero")
elif num>0:
print(num,"is positive")
else:
print(num,"is negative")

8
Accept numeric digit from keyboard and display in words
x=int(input('Enter Digit: '))
if x==0:print("ZERO")
elif x==1:print("ONE")
elif x==2:print("TWO")
elif x==3:print("THREE")
elif x==4:print("FOUR")
elif x==5:print("FIVE")
elif x==6:print("SIX")
elif x==7:print("SEVEN")
elif x==8:print("EIGHT")
elif x==9:print("NINE")
9
While Loop
•"A while loop in Python is a control flow statement that allows a
block of code to be executed repeatedly based on a given Boolean
condition. In other words, the while loop will keep iterating and
running the code block inside of it until the specified condition
evaluates to False.”
Syntax
while condition:
statements

10
Display numbers from 1 to 10
x=1
while x<=10:
print(x)
x+=1
print("End")

Practice
Display even numbers from 100 to 200

11
Display even numbers between m and n
m,n=[int(i) for i in input("Enter minimum and maximum range:").split(',')]
x=m
if x%2!=0:
x=x+1
while x>=m and x<=n:
print(x)
x+=2

12
For loop
In Python, a for loop is used to iterate over sequences such as lists,
strings, tuples, etc.
for val in sequence:
# statement(s)
Here, val accesses each item of the sequence on each iteration. The
loop continues until we reach the last item in the sequence.

13
Example
Display characters of a string using for loop
language = 'Python'

# iterate over each character in language


for x in language:
print(x)

14
for Loop with range()
•In Python, the range() function returns a sequence of numbers. For
example,
values = range(4)
•Here, range(4) returns a sequence of 0, 1, 2 ,and 3.
•Since the range() function returns a sequence of numbers, we can
iterate over it using a for loop.

For example,
# iterate from i = 0 to i = 3
for i in range(4):
print(i)
15
for x in range(3):
print("Printing:", x)

In the below example, number 3 as the step and you can see the output
numbers are the previous number + 3.

for n in range(1, 10, 3):


print("Printing with step:", n)

Negative value for our step argument to iterate backwards, but you have to
adjust our start and stop arguments accordingly:

for i in range(100,0,-10):
print(i)
16
Practice
i.Program to display and find the sum of a list of numbers
using for loop.
ii.Program to display and find the sum of a list of
numbers using while loop.

17
Infinite loops
i=1
while i<=10:
print(i)
i+=1

18
Nested Loops
•It is possible to write one loop inside another loop.
•For, example you can write a for loop inside a while loop or a for
loop inside another for loop.
•Such loops are called ‘nested loops’

19
for i in range(3):
for j in range(4):
print('i=',i ,'\t','j=',j)

The outer for loop repeats for 3 times by changing i values from 0
to 2.The inner loop repeats for 4 times by changing values from o
to 3.

When outer for loop is executed once, the inner for loop is executed
for 4 times. It means ,when i value is 0, j values will change from
0 to 3.

20
Printing multiplication table using Python nested for loops

for i in range(2, 4):


for j in range(1, 11):
print(i, "*", j, "=", i*j)
print()

21
Python program to display stars(* s) in a right angled triangle
form.
In the first row ,there is only star. In the second row , there are tow
stars. In the third row ,there are three stars.

Since there are 10 rows ,you can write an outer for loop that
repeats for 10 times from 1 to 10 as:
Row number = number of stars in that row

22
• To represent the columns(or stars) in each row, you can write an
inner loop. This for loop should repeat as many times as the row
number indicated by the outer for loop. Suppose row number is
‘i’ then the inner for loop should repeat for 1 to i times

Inner loop written as follows:


For i in range(1, i+1): // repeat for 1 to i times

23
• Python program to display stars(* s) in a right angled triangle
form using a single loop.
• Python program to display stars in an equilateral triangular form
using a single loop.
• Python program to display numbers from 1 to 100 in 10 rows
and 10 columns

24
The else suite
In python, it is possible to use ‘else’ statement along with for loop
or while loop as below

for with else while with else


for(var in sequence): While(condition):
statements statements
else else
Statements statements

25
• Python program to search for an element in the list of elements
• Python program to display numbers from 10 to 1 in descending
order using while loop.
• Write a python program to display prime number series.

26

You might also like