Lab 12
Lab 12
Lab 12
LAB # 12
FOR LOOP
OBJECTIVE
THEORY
A loop can be used to tell a program to execute statements repeatedly. In other word, to
keep a computer doing useful work we need repetition, looping back over the same
block of code again and again.
a) for i in range(endValue):
#Loop body
A ‘sequence’ holds multiple items of data, stored one after the other. In
sequence introduce strings and data storing techniques.They are sequence-type
objects in Python.
The variable ‘Var’ takes on each successive value in the sequence, and
The statements in the body of the loop are executed once for each value.
Output:
>>> %Run task1.py
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Output:
>>> %Run task2.py
Programming is fun
Programming is fun
Programming is fun
Programming is fun
Programming is fun
For Loop
Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.
Python supports the following control statements. Click the following links to check their
detail.
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition
prior to reiterating.
Syntax
for iterating_var in sequence:
statements(s)
If a sequence contains an expression list, it is evaluated first. Then, the first item in the
sequence is assigned to the iterating variable iterating_var. Next, the statements block is
executed. Each item in the list is assigned to iterating_var, and the statement(s) block is
executed until the entire sequence is exhausted.
CE-119 : Computing Fundamentals 81
Lab # 11: FOR Loop SSUET/QR/114
Sample Program - 1
Output:
0
1
2
3
Sample Program - 2
Output:
The sum is 48
We can generate a sequence of numbers using range() function. range(10) will generate
numbers from 0 to 9 (10 numbers).
We can also define the start, stop and step size as range(start, stop,step_size). step_size
defaults to 1 if not provided.
print(range(10))
print(list(range(10)))
print(list(range(2, 8)))
print(list(range(2, 20, 3)))
Output
range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7]
[2, 5, 8, 11, 14, 17]
We can use the range() function in for loops to iterate through a sequence of numbers. It
can be combined with the len() function to iterate through a sequence using indexing.
Here is an example.
Sample Program - 3
Output
I like pop
I like rock
I like jazz
The break keyword can be used to stop a for loop. In such cases, the else part is ignored.
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
Output:
EXERCISE
1. Code
for(;;)
{
printf("SSUET")
}
Output:
2.Code
for v in range(4:8)
print(v)
Output:
1. Code
Lab Task:
1. Write a program that prints the first 10 natural numbers and their sum using for
loop’.
Sample output:
The Sum is : 55
2. Write a program to print the multiplication table of the number entered by the user.
The table should get displayed in the following form.
29 x 1 = 29
29 x 2 = 58
…
29 10 = 290
3. Write a program that take vowels character in a variable named “vowels” then
print each vowels characters in newline using ‘for loop’.
4. Write a program which finds the average of 10 numbers using for loop.