Lab05 - Assignment Mohammed Muddassir
Lab05 - Assignment Mohammed Muddassir
March 2, 2021
0
1
4
9
16
25
36
49
64
81
100
• Iterating through list indices (indexes)
1
Sometimes, we may need to know the index of the element we are currently examining. We can
use the range() function in our for loop to do this. For example, the following for loop executes 10
times, and prints all integers between 0 and 10, inclusive, along with their squares:
[45]: ##Try the code below
list = [1,4,9,16,25,36,49,64,81,100]
for index in range(len(list)):
print(index, list[index])
0 1
1 4
2 9
3 16
4 25
5 36
6 49
7 64
8 81
9 100
0.2 Task 1
Write a code to iterate over all multiples of 3 between 0 and 100 (inclusive), and print the ones
that are divisible by 2.
• Use the range() function to obtain a list of multiples of 3 between 0 and 100.
• Use the modulus operator to check for divisibility(%) –
12
18
24
30
2
36
42
48
54
60
66
72
78
84
90
96
0.3 Task 2
For this part of the lab, write a function to take a list of numbers and a number to check if the
number is in the list or not.
Print out the index of the element, if it exists in the list, and the message ‘The element does not
exist in the list’, otherwise.
Part A
Solve Task 2 without using the list built-in function for finding elements.
[3]: #### creating an empty list
lst = []
3
for i in range(0,n):
if lst[i]==test:
print("element ", i, " exists in the list")
else:
print("element ", i," does not exist in list")
i+=1
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
Part B
Solve Task 2 using the list built-in function for finding elements.
[4]: ##Your code here
4
# looping until you reach the range
for i in range(0, n):
value = int(input())
for i in range(0,n):
if lst[i]==test:
print("element ", lst.index(test), " exists in list")
else:
print("element ",i," not in list")
i+=1
[ ]:
0.4 Submission
1. Remember to add the code below to the beginning of your assignemnet with today’s date and
your name and student ID
[1]: #Created on WEEKDAY, MONTH DD, YYYY
#@author: Firstname Lastname (student ID)
#Lab 05 - Jupyter Notebook
2. Follow the steps from the labs/how-to-submit.pdf file to submit your work.
Note. Instructions are updated to include both .ipynb and .pdf notebook versions.
[ ]: