A201 Introduction to Programming 1

A201 Lab 8.1

Date: Tuesday, October 10, 2023.
Due Date: Tuesday, October 31, 2023.

Goal: to use lists in Python.

Create a Python file called lab8.1.py and add your name and the lab number as a comment at the top.

Ex. 1. List Manipulation

Copy the following Python fuction discussed in class into your file:

from random import *
def makeRandomList(size, bound):
    a = []
    for i in range(size):
        a.append(randint(0, bound))
    return a

a. Add another function that receives a list as a parameter and computes the sum of the elements of the list. The header of the function should be

def sumList(a):

The function should return the result and not print it. If the list is empty, the function should return 0. Use a for loop iterating over the list.

Test the function in the console to see if it works.

b. Write a function that receives a list as a parameter and a value, and returns True if the value can be found in the list, and False if not. Use a for loop that goes over the list and compares each element to the value. If any of them is equal to the value, it returns True. Otherwise after the loop is done, you can return False. Thus, if the loop completes without a return statement happening, then the value is not in the list. The function header should look like this:

def searchList(a, val):

Test this second function a few times the same way as the first one.

c. Below the three functions, add a piece of code that

Test the program to make sure that it works fine.

Lab Submission

Upload the file lab8.1.py in Canvas, Assignments, Homework 8. You can wait until you finish the second part of the lab and the homework to upload all the files at the same time.