0% found this document useful (0 votes)
78 views5 pages

Lab05 - Assignment Mohammed Muddassir

This document provides instructions for Lab05 assignment. Students are asked to complete two tasks - (1) iterate over multiples of 3 between 0-100 and print even numbers, and (2) write a function to check if a number exists in a list and print the index if it does, or a message if it doesn't. The lab aims to apply concepts of loops, lists and functions learned. Students are also instructed to include their name and ID at the top of the assignment and submit both ipynb and pdf files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views5 pages

Lab05 - Assignment Mohammed Muddassir

This document provides instructions for Lab05 assignment. Students are asked to complete two tasks - (1) iterate over multiples of 3 between 0-100 and print even numbers, and (2) write a function to check if a number exists in a list and print the index if it does, or a message if it doesn't. The lab aims to apply concepts of loops, lists and functions learned. Students are also instructed to include their name and ID at the top of the assignment and submit both ipynb and pdf files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab05_assignment

March 2, 2021

0.1 Lab05 - Assignment


Due date: 1 week from your lab session.
Hello Everyone!
In this lab, you’ll practice with loops, lists and functions. The goal of this lab is to bring
together all the subjects you learnt up unitl now.
Background
• Iterating through list elements
#Created on Wednesday, February 24, 2021 #@author: Mohammed Muddassir 100814205 #Lab
05 - Jupyter Notebook
A for loop repeats a sequence of statements until a condition (the continue condition) becomes false.
For example, the following code prints out the square root of all integers between 0 and 10:
[ ]: #Created on Tuesday, 2nd March 2021
#@author: Mohammed Muddassir (100814205)
#Lab 05 - Jupyter Notebook

[1]: ##Uncomment to run


list = [0,1,4,9,16,25,36,49,64,81,100]
for element in list:
print(element)

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(%) –

[9]: ##Your code here

for element in range(0,101,3):


if element%2==0:
print(element)
else:
print('\t')

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 = []

# number of elements as input


n = int(input("Enter number of values you want to enter : "))

# looping until you reach the range


for i in range(0, n):
value = int(input())

lst.append(value) # adding the element to the list

test = int(input("enter the test number"))

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

Enter number of values you want to enter : 3


11
19
220
enter the test number 220
element 0 does not exist in list
element 1 does not exist in list
element 2 exists in the list

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

Part B
Solve Task 2 using the list built-in function for finding elements.
[4]: ##Your code here

#### creating an empty list


lst = []

# number of elements as input


n = int(input("Enter number of values you want to enter : "))

4
# looping until you reach the range
for i in range(0, n):
value = int(input())

lst.append(value) # adding the element to the list

test = int(input("enter the test number"))

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

Enter number of values you want to enter : 5


11
12
13
15
16
enter the test number 12
element 0 not in list
element 1 exists in list
element 2 not in list
element 3 not in list
element 4 not in list

[ ]:

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.
[ ]:

You might also like