ITEC-425 / SENG-425: Python Programming Lab Lab 5: Loops and Iteration Task 1
ITEC-425 / SENG-425: Python Programming Lab Lab 5: Loops and Iteration Task 1
In [1]:
print("Starting countdown ...")
t = 10
while t > 0:
print(t)
t = t - 1
print("Liftoff!")
Task 2
Modify the above program to use a for loop instead of a while loop.
Starting value is 10
Ending value is 1
Step size is -1
In [2]:
print("Starting countdown ...")
print("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 [3]:
def sum_list(list_of_nums):
ans = 0
for val in list_of_nums:
ans = ans + val
return ans
In [5]:
list2 = [2, 4, 6, 8]
print("Sum of numbers in list2 is", sum_list(list2))
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
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
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