Day-2: Control
Statement and
String
Manipulation
22/06/2018
Workshop on “Introduction to
Python”
Department Of
Computer Science
Engineering
SKFGI, Mankundu
, Control statements:
In general, statements are executed sequentially: The first statement in a
function is executed first, followed by the second, and so on. There may be
a situation when you need to execute a block of code several number of
times.
Programming languages provide various control structures that allow for
more complicated execution paths.
A loop statement allows us to execute a statement or group of statements
multiple times. The following diagram illustrates a loop statement −
Python programming language provides following types of loops to handle
looping requirements.
Sl No. Loop Type & Description
1 while loop
Repeats a statement or group of statements while a given
condition is TRUE. It tests the condition before executing the
loop body.
2 for loop
Executes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
3 nested loops
You can use one or more loop inside any another while, for or
do..while loop.
,1.While loop:-
A while loop statement in Python programming language repeatedly
executes a target statement as long as a given condition is true. The
syntax of a while loop in Python programming language is −
while expression:
statement(s)
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value. The
loop iterates while the condition is true. When the condition becomes false,
program control passes to the line immediately following the loop.
Program-1:
count = 0
while count < 9:
print ('The count is:', count)
count = count + 1
print ("Exit from loop")
output:-
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Exit from loop
, Program-2
print("enter the number")
x=(int)(input())
f=1
while x > 0:
f=f * x
x= x - 1
print(f)
output:-
enter the number
6
factorial is 720
Program-3
print("enter the number")
x=(int)(input())
rev=0
while x > 0:
r=x % 10
rev=rev*10+r
x= x // 10
print("reverse is",rev)
output:-
enter the number
123
reverse is 321
Program-4
print("enter the number")
x=(int)(input())
f=x
rev=0
while x > 0:
Statement and
String
Manipulation
22/06/2018
Workshop on “Introduction to
Python”
Department Of
Computer Science
Engineering
SKFGI, Mankundu
, Control statements:
In general, statements are executed sequentially: The first statement in a
function is executed first, followed by the second, and so on. There may be
a situation when you need to execute a block of code several number of
times.
Programming languages provide various control structures that allow for
more complicated execution paths.
A loop statement allows us to execute a statement or group of statements
multiple times. The following diagram illustrates a loop statement −
Python programming language provides following types of loops to handle
looping requirements.
Sl No. Loop Type & Description
1 while loop
Repeats a statement or group of statements while a given
condition is TRUE. It tests the condition before executing the
loop body.
2 for loop
Executes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
3 nested loops
You can use one or more loop inside any another while, for or
do..while loop.
,1.While loop:-
A while loop statement in Python programming language repeatedly
executes a target statement as long as a given condition is true. The
syntax of a while loop in Python programming language is −
while expression:
statement(s)
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value. The
loop iterates while the condition is true. When the condition becomes false,
program control passes to the line immediately following the loop.
Program-1:
count = 0
while count < 9:
print ('The count is:', count)
count = count + 1
print ("Exit from loop")
output:-
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Exit from loop
, Program-2
print("enter the number")
x=(int)(input())
f=1
while x > 0:
f=f * x
x= x - 1
print(f)
output:-
enter the number
6
factorial is 720
Program-3
print("enter the number")
x=(int)(input())
rev=0
while x > 0:
r=x % 10
rev=rev*10+r
x= x // 10
print("reverse is",rev)
output:-
enter the number
123
reverse is 321
Program-4
print("enter the number")
x=(int)(input())
f=x
rev=0
while x > 0: