0% found this document useful (0 votes)
226 views20 pages

Control Statements

Control statements in Python include if/else statements, while loops, for loops, and other statements like break, continue, pass, assert, and return. The if statement executes code based on a condition, while loops repeat code while a condition is true, and for loops iterate over items in a sequence. Other statements like break exit a loop, continue skips to the next iteration, pass acts as a null operation, assert validates conditions, and return exits a function. These control structures allow Python code to execute conditionally and repeat tasks to handle different data flow scenarios.
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)
226 views20 pages

Control Statements

Control statements in Python include if/else statements, while loops, for loops, and other statements like break, continue, pass, assert, and return. The if statement executes code based on a condition, while loops repeat code while a condition is true, and for loops iterate over items in a sequence. Other statements like break exit a loop, continue skips to the next iteration, pass acts as a null operation, assert validates conditions, and return exits a function. These control structures allow Python code to execute conditionally and repeat tasks to handle different data flow scenarios.
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/ 20

Control Statements

Control statements which control or change the flow of


execution. The following are the control statements available
in python
if statement
if…else statement
if..elif…else statement
while loop
for loop
else suite
break statement
continue statement
pass statement
assert statement
return statement
The if statement
Syntax:
if condition:
statements
Statement-x
age=int(input('Enter a age of a person:'))
if age>18:
print('Person is eligible to vote')
print("End")
Output
Enter a age of a person:23
Person is elgible to vote
End
Or
Enter a age of a person:14
End
if…else…statement
if condition:
statement-block1
else:
statement-block2
Statement-x
Example:python program to check given number is even or odd. In case
even find square of it in case odd find cube of it
Filename:ex1.py
if..elif..else statement
Syntax:
if condition1:
statement-block1
elif condition2:
statement-block2
elif condition3:
statement-block3
...
…..
else:
statement-blockn
statementx
Example: python program to read a digit and print it in word
Filename ex2.py
Nested if statement
if condition1:
if condition2:
statement-block1
else:
statement-block2
else:
if condition3:
statement-block3
else:
statement-block4
Example: Python program to find largest of three numbers
Filename:ex3.py
While Loop statements
while condition:
statement-block
Statement-x
Example:
Python program to find sum of first N natural numbers
Filename:ex4.py
for loop statement
for loop is used to iterate over individual element in a sequence data
type object
x=[12,23,45,66]
for i in x:
print(i)
Output:
12
23
45
66
range() in for loop
Form1 syntax:
for var in range(end):
statements
Statement-x
Example:
print numbers from 0 to 10
Filename:ex5.py
Form2 syntax:
for var in range(start,end):
statements
Statement-x
Example:
print numbers from 1 to 10
Filename: ex6.py
Contd..
form3 syntax:
for var in range(start,stop,step):
satements
Statement-x
Examples: program to print even numbers from 2 to 10
File name:ex7.py
Program to print odd numbers from 33 to 1
Filename:ex8.py
Nested for loop
for within for is called nested for loop
Syntax:
for i in range(start,stop,step):
for j in range(start,stop,step):
statements
statement-x
Statement-x1
Example: python program to print pattern
1
2 3
4 5 6
7 8 9 10
Filename ex9.py
The else suite
It is possible to use else statement along with for loop or while loop in
python
for with else while with else
for var in sequence: while condition:
statements statements
else: else:
statements satements
Statements written after else are called else suite. Control will enter
the else clause of for or while loop only when the loop has a reached
the normal termination. Control will not enter the else clause if for or
while loop terminated prematurely because of execution of break
statements within the loop.
Example
Python program to perform linear search on a given list and searching
key( use for with else)
Example:ex10.py

Python program to check given number is prime or not using while with
else
Example:ex11.py
Break and continue statement
Break statement can be used inside for or while loop to skip the remaining
iterations and come out of loop.
#program to illustrate break statement
x=10
while x>=1:
print(“x=“,x)
x-=1
if x==5:
break
print(“out of loop”)
Continue statement
Continue statement will skip particular iteration of loop and continues
with next iteration
#python program to print numbers from 1 to 10 and skip 6
for i in range(1,10):
if i==6:
continue
print("i=",i)
print("out of loop")
pass statement
Sometime we do not have any statements to write in a block. In python block
can not be empty. If we do not have any statements to write then we must
use pass keyword which indicates null statement
Example: Check number that is not divisible by three
x=5
if x%3!=0:
print(x)
Or
x=5
if x %3==0:
pass
else:
print(x)
assert statement
assert statement is useful to check if a particular condition is fulfilled or not.
syntax
assert expression, message
In the above syntax the ‘message’ is not compulsory.

"""
python program to assert that the user enters a
number greater than zero
"""
x=int(input('Enter a number greater than 0:'))
assert x>0, "worng input entered"
print("U entered a number",x)
output
Enter a number greater than 0:6
U entered a number 6

Enter a number greater than 0:-8


Traceback (most recent call last):

File "C:\Users\Hp\untitled0.py", line 7, in <module>


assert x>0, "worng input entered"

AssertionError: worng input entered


Assertion error shown in the above output is called as exception. An
exception is an error that occurs during runtime. To avoid such exception we
can use try ..except block
continue
"""
python program to assert that the user enters a
number greater than zero
"""
x=int(input('Enter a number greater than 0:'))
try:
assert x>0
print("U entered a number",x)
except AssertionError:
print("Entered a worng number")
Output
Enter a number greater than 0:-8
Entered a worng number
Thank you

You might also like