0% found this document useful (0 votes)
27 views10 pages

Python LAB 05

This document discusses control statements and loops in Python programming. It describes while loops, for loops, and nested loops. While loops repeatedly execute statements as long as a condition is true. For loops iterate over elements in a sequence. Examples are provided to illustrate while and for loops. The document also provides tasks to write Python code using loops, including generating random numbers, printing odd/even numbers within ranges, calculating factorials based on user input, printing tables, finding highest/lowest of user-entered numbers, and calculating multiple factorials.

Uploaded by

khadijaaaa2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
27 views10 pages

Python LAB 05

This document discusses control statements and loops in Python programming. It describes while loops, for loops, and nested loops. While loops repeatedly execute statements as long as a condition is true. For loops iterate over elements in a sequence. Examples are provided to illustrate while and for loops. The document also provides tasks to write Python code using loops, including generating random numbers, printing odd/even numbers within ranges, calculating factorials based on user input, printing tables, finding highest/lowest of user-entered numbers, and calculating multiple factorials.

Uploaded by

khadijaaaa2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 10

Programming Fundamentals (CS-116L) SSUET/QR/114

LAB # 05

Control Statements
OBJECTIVE:
To get familiar with the concept of control statement for simple controlling and repetition of program
statements.

Theory:
Control Statements (Loops)

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.

Loop Type Description


Repeats a statement or group of statements while a given condition is
while loop
TRUE. It tests the condition before executing the loop body.
Executes a sequence of statements multiple times and abbreviates the
for loop
code that manages the loop variable.
nested loops You can use one or more loop inside any another while, for loop.

The while Loop


A while loop statement in Python programming language repeatedly executes a target
statement as long as a given condition is True.

Syntax

The syntax of a while loop in Python programming language is:

while expression:

statement(s)

Electronic Engineering Department 1


Programming Fundamentals (CS-116L) SSUET/QR/114

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.

In Python, all the statements indented by the same number of character spaces after a
programming construct are considered to be part of a single block of code. Python uses
indentation as its method of grouping statements.

Example-1: Example-2:
i=1 i=1
while i < 4: while i < 4:
print (i) print i
i+=1 i+=1
print( “END”) print “END”

Output-1: Output-2:

1 END 1
2 END 2
3 END 3
END

Example: Write a program to display factorial of a given number.


Program:
n=input("Enter the number: ")
f=1
while n>0:
f=f*n
n=n-1
print "Factorial is", f

Output:
Enter the number: 5 Factorial is 120

The For loop:


Electronic Engineering Department 2
Programming Fundamentals (CS-116L) SSUET/QR/114

The for loop is useful to iterate over the elements of a sequence. It means, the for loop
can be used to execute a group of statements repeatedly depending upon the number of
elements in the sequence. The for loop can work with sequence like string, list, tuple, range
etc.
The syntax of the for loop is given below:
for var in sequence:
statement (s)

The first element of the sequence is assigned to the variable written after „for‟ and then the
statements are executed. Next, the second element of the sequence is assigned to the variable
and then the statements are executed second time. In this way, for each element of the sequence,
the statements are executed once. So, the for loop is executed as many times as there are
number of elements in the sequence.

1 END 1
2 END 2
3 END 3
END

for i range(1,5): for i range(1,5):


print i print i
print “END” print “END”

Output-1: Output-2:

Example-3: Example-4:
Name= "python"
for x in range(10,0,-1):
for letter in Name:
print x,
print letter

Output-3: Output-4: 10 9 8 7 6 5 4 3 2 1

py t h o n

Electronic Engineering Department 3


Programming Fundamentals (CS-116L) SSUET/QR/114

Exampe: Write a program to display the factorial of given number.

Program:

Output:

Enter the number: 5


Factorial is 12

Electronic Engineering Department 4


Programming Fundamentals (CS-116L) SSUET/QR/114

TASK:
Perform the following tasks using Python source code using loops
1. To generate 10 random numbers from 3 to 13.

CODE:
OUTPUT:

2. To print the odd number from 10 to 100 and even numbers from 200 to 96.

CODE:

OUTPUT:

Electronic Engineering Department 5


Programming Fundamentals (CS-116L) SSUET/QR/114

Electronic Engineering Department 6


Programming Fundamentals (CS-116L) SSUET/QR/114

3. To take a integer value from a user and print the factorial of that number.

CODE:

OUTPUT:

Electronic Engineering Department 7


Programming Fundamentals (CS-116L) SSUET/QR/114

4. To take two integer values from the user and print the table. The first indicates the table
number and the second value represents the limit of the table.
CODE:

OUTPUT:

5. To take two integer values from the user and print out the highest and lowest value. Ask
the user if they want to continue entering more numbers until they choose no.
CODE:

Electronic Engineering Department 8


Programming Fundamentals (CS-116L) SSUET/QR/114

OUTPUT:

6. To calculate the factorial of 3 numbers which will be provided by the user.


CODE:

OUTPUT:

Electronic Engineering Department 9


Programming Fundamentals (CS-116L) SSUET/QR/114

Electronic Engineering Department 10

You might also like