0% found this document useful (0 votes)
77 views

ITEC-425 / SENG-425: Python Programming Lab Lab 5: Loops and Iteration Task 1

The document describes 6 tasks to practice loops and iterations in Python. Task 1 and 2 demonstrate using while and for loops to count down from 10 to 1. Task 3 defines a function that takes a list as a parameter and uses a for loop to calculate the sum of all numbers in the list. Task 4 calculates the average of numbers in a list using a for loop. Task 5 finds the maximum number in a list using a for loop. Task 6 asks to write a function that finds the minimum number in a list.

Uploaded by

Umm E Farwa Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

ITEC-425 / SENG-425: Python Programming Lab Lab 5: Loops and Iteration Task 1

The document describes 6 tasks to practice loops and iterations in Python. Task 1 and 2 demonstrate using while and for loops to count down from 10 to 1. Task 3 defines a function that takes a list as a parameter and uses a for loop to calculate the sum of all numbers in the list. Task 4 calculates the average of numbers in a list using a for loop. Task 5 finds the maximum number in a list using a for loop. Task 6 asks to write a function that finds the minimum number in a list.

Uploaded by

Umm E Farwa Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

12/15/2020 Lab_5_Loops and Iterations

ITEC-425 / SENG-425: Python Programming Lab

Lab 5: Loops and Iteration


Task 1
Write a program that uses while loop and produces the following output.

Starting countdown ...


10
9
8
7
6
5
4
3
2
1
Liftoff!

In [1]:
print("Starting countdown ...")

t = 10

while t > 0:
print(t)
t = t - 1

print("Liftoff!")

Starting countdown ...


10
9
8
7
6
5
4
3
2
1
Liftoff!

Task 2
Modify the above program to use a for loop instead of a while loop.

Notice the use of the range(10, 0, -1) function:

Starting value is 10
Ending value is 1
Step size is -1

In [2]:
print("Starting countdown ...")

for t in range(10, 0, -1):


print(t)

print("Liftoff!")

Starting countdown ...

file:///C:/Users/bisha/Desktop/Teaching/BS - ITEC-SENG-424 - Python Programming/Lab tasks/Lab_5_Loops and Iterations.html 1/3


12/15/2020 Lab_5_Loops and Iterations
10
9
8
7
6
5
4
3
2
1
Liftoff!

Task 3
Write a function that takes a list of numbers as argument and uses a for loop to find the sum of
numbers in the given list.

In python a list of numbers is given as shown in the example below:

primes = [2, 3, 5, 7, 11, 13, 17, 19]

In [3]:
def sum_list(list_of_nums):
ans = 0
for val in list_of_nums:
ans = ans + val
return ans

Possible executions and outputs


In [4]:
list1 = [1, 2, 3, 4, 5]
print("Sum of numbers in list1 is", sum_list(list1))

Sum of numbers in list1 is 15

In [5]:
list2 = [2, 4, 6, 8]
print("Sum of numbers in list2 is", sum_list(list2))

Sum of numbers in list2 is 20

Task 4
Write a program that uses for loop to compute the average of numbers in a given list.

In [6]:
my_list = [2, 3, 5, 7, 11, 13, 17, 19]

list_count = len(my_list)
list_sum = 0

for item in my_list:


list_sum = list_sum + item

list_avg = list_sum / list_count

print("The average of numbers is: ", list_avg)

The average of numbers is: 9.625

Task 5
Write a program that uses a loop to find the maximum number in a given list of numbers.

In [7]:
my_list = [3, 1, 0, 9, 5, 2, 6, 4, 9, 8, 7]

largest = my_list[0]
file:///C:/Users/bisha/Desktop/Teaching/BS - ITEC-SENG-424 - Python Programming/Lab tasks/Lab_5_Loops and Iterations.html 2/3
12/15/2020 Lab_5_Loops and Iterations

for num in my_list:


if num > largest:
largest = num

print("The largest number is: ", largest)

The largest number is: 9

Task 6
Similar to the above task write a program that finds the smallest number in a given list of numbers.

In [8]:
# Write your code here
# Write this as a function which takes the list of numbers as an argument
# Call the function with different lists to verify that it's returing the smallest number

file:///C:/Users/bisha/Desktop/Teaching/BS - ITEC-SENG-424 - Python Programming/Lab tasks/Lab_5_Loops and Iterations.html 3/3

You might also like