Lab Session 5 (Loop)
Lab Session 5 (Loop)
Programming Fundamentals
BS 2021
Department of CS andSE
Lab Session 5: Loops
Learning Outcomes
After this lab, students will be able to:
Explain how using loops help you to write DRY (Don’t Repeat Yourself) code in Python
Describe the syntax for two basic looping structures used in Python: while and for
Remove repetition in code by using loops to automate data tasks
Design and perform looping statements to solve problems
Python programming language provides following types of loops to handle looping requirements.
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.
CSS 102
Programming Fundamentals
BS 2021
Department of CS andSE
nested loops
3
You can use one or more loop inside any another while, for or do..while loop.
Program 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
Good bye!
Program Output
Enter a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3
You entered: 3
Enter a number between :Traceback (most recent call last):
File "test.py", line 5, in <module>
num = raw_input("Enter a number :")
KeyboardInterrupt
Above example goes in an infinite loop and you need to use CTRL+C to exit the program.
Program Output
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
CSS 102
Programming Fundamentals
BS 2021
Department of CS andSE Good bye!
Program Output
0
1
2
3
4
5
Program Output
2
3
4
5
The range() function defaults to increment the sequence by 1, however it is possible to specify the increment
value by adding a third parameter: range(2, 30, 3).
Example 3
Increment the sequence with 3 (default is 1):
for x in range(2, 30, 3):
print(x)
Program Output
2
5
8
11
14
17
CSS 102
Programming Fundamentals
BS 2021
Department of CS andSE 20
23
26
29
Program Output
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
Here, we took the assistance of the len() built-in function, which provides the total number of elements in
the tuple as well as the range() built-in function to give us the actual sequence to iterate over.
Program Output
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
CSS 102
Programming Fundamentals
BS 2021
Department of CS andSE
Exercises
1. Make a program that lists the countries in the set.
CSS 102
Programming Fundamentals
BS 2021
Department of CS andSE clist = ['Canada','USA','Mexico','Australia']
2. Rewrite the following code so it calls the range function instead of using the
list [0, 1, 2, 3, 4, 5].
for x in [0, 1, 2, 3, 4, 5]:
print('I love to program!')
3. What will the following code fragments display?
a. for number in range(2, 6):
print(number)
b. for number in range(0, 501, 100):
print(number)
c. for number in range(10, 5, -1):
print(number)
4. Write a python program to print the square of all numbers from 0 to 10.
5. Write a python program to find the sum of all even numbers from 0 to 10.
6. Print multiplication table of 24, 50 and 29 using loop.
7. Take 10 integers from keyboard using loop and print their average value on the screen.
8. Write a python program to read three numbers (a,b,c) and check how many numbers between ‘a’ and ‘b’
are divisible by ‘c’.
9. Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1 and 0! = 1
Write a program to calculate factorial of a number.
10. Write code that prompts the user to enter a positive nonzero number and validates the input.
11. Write a while loop that asks the user to enter two numbers. The numbers should be added and the sum
displayed. The loop should ask the user if he or she wishes to perform the operation again. If so, the loop
should repeat, otherwise it should terminate.
12. The distance a vehicle travels can be calculated as follows:
Distance = speed * time
For example, if a train travels 40 miles per hour for three hours, the distance traveled is 120 miles. Write
a program that asks the user for the speed of a vehicle (in miles per hour) and the number of hours it has
traveled. It should then use a loop to display the distance the vehicle has traveled for each hour of that
time period. Here is an example of the desired output:
Sample Output:
What is the speed of the vehicle in mph? 40
How many hours has it traveled? 3
Hour Distance Traveled
1 40
2 80
3 120