PF LAB 05 (Control Statements)
PF LAB 05 (Control Statements)
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)
Programming languages provide various control structures that allow for more
complicated execution paths.
Syntax
The syntax of a while loop in Python programming language is: while expression:
statement(s)
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
Output:
Enter the number: 5 Factorial is 120
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
Output-1: Output-2:
Example-3: Example-4:
Name= "python"
for x in range(10,0,-1):
10 9 8 7 6 5 4 3 2 1
for letter in Name:
print x,
print letter
Output-3: Output-4:
py t h o n
Program:
Output:
Factorial is 12
TASK:
Perform the following tasks using Python source code using loops
1. To generate a random number from 3 to 13.
CODE:
import random
random_number = random.randint(3, 13)
print(random_number)
OUTPUT:
2. To print the odd number from 10 to 100 and even numbers from 200 to 96.
CODE:
# Print odd numbers from 10 to 100
print("Odd numbers from 10 to 100:")
for number in range(10,101):
if number % 2 != 0:
print(number)
OUTPUT:
CODE:
# Print even numbers from 96 to 200
print("even numbers from 96 to 200:")
for number in range(96,201):
if number % 2 == 0:
print(number)
OUTPUT:
3. To take a integer value from a user and print the factorial of that number.
CODE:
n =int(input("Enter a Number:"))
factorial = 1
if n < 0:
print("Factorial does not exist")
elif n==0:
print("The factorial is 1")
else:
for i in range(1, n+1):
factorial = factorial*i
print("The factorial of",n,"is",factorial)
OUTPUT:
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:
table_number = int(input("Enter the table number: "))
limit = int(input("Enter the limit of the table: "))
if limit < 1:
print("Limit should be a positive integer.")
else:
print("Table of",table_number,"up to",limit)
5. To take two integer values from the user and print out the highest and lowest value.
CODE:
n_1 = int(input("Enter 1st Integer:"))
n_2 = int(input("Enter 2nd Integer:"))