Lab 4: Introduction to Python
Contents
Lab 4: Introduction to Python¶
In this lab you will be working on python basics.
I have recorded a video to get you started with this lab: Click here to view the video. The lab is slightly different and updated, but the idea is very similar.
This lab must be completed individually.
Where provided, try your best to match the Sample Output as best as you can.
Accept the lab¶
To accept this lab on GitHub Classroom, go to Canvas
–> Course Content
–> Lab Links and Solutions
.
Task 2: Python Fundamentals II¶
This part of the lab takes you through some of the fundamental components of Python.
2.1: if-elif-else
statements¶
Fill in missing pieces of the following code such that print statements make sense. You should replace <YOUR_CODE_HERE> with your code.
name = 'Marie Antoinette'
### Your solution here
if <YOUR_CODE_HERE>:
print(f"Name {name} is more than 20 chars long")
length_description = 'long'
elif <YOUR_CODE_HERE>:
print(f"Name {name} is more than 18 chars long")
length_description = 'semi long'
elif <YOUR_CODE_HERE>:
print(f"Name {name} is more than 15 chars long")
length_description = 'semi long'
elif <YOUR_CODE_HERE>:
print(f"Name {name} is 9, 10 or 11 chars long")
length_description = 'semi short'
else:
print(f"Name {name} is a short name")
length_description = 'short'
File "/tmp/ipykernel_1971/2905628095.py", line 3
if <YOUR_CODE_HERE>:
^
SyntaxError: invalid syntax
2.2: for
loops¶
Fill <YOUR_CODE_HERE> in the code snippet below so that this sample output is printed:
Sample Output¶
A
AA
AAA
AAAA
AAAAA
AAAA
AAA
AA
A
### Your solution here
n = 10
for i in range(1,n):
if <YOUR_CODE_HERE>:
print("A" * <YOUR_CODE_HERE>)
else:
print("A" * (<YOUR_CODE_HERE>))
2.3: for
loops and lists
¶
We have given you some sample code, as well as a list called data
.
data = [53,9,5,90,63,5,97,40,92,48,53,8,38,63,13,15,66,81,57,79,42,91,25,89,66,4,73,45,80,17]
For each of the exercises 1C1 - 1CX, use the data
list to answer the question by writing the appropriate bits of python code.
2.3.1: Print values that are within 25 (lower bound) and 45 (upper bound) including both ends.¶
Hint: Use a for loop and loop through each of the elements in data, and store it into a list. Then, print the list
Sample output:¶
[40, 38, 42, 25, 45]
data = [53,9,5,90,63,5,97,40,92,48,53,8,38,63,13,15,66,81,57,79,42,91,25,89,66,4,73,45,80,17]
### Your solution here
2.3.2: Write code to calculate and print the maximum, minimum, sum, count, and average of items in data
¶
Hint: for the mean, you will need to combine the sum and the count
Sample output¶
The max is: 97
The min is: 4
The sum is: 1507
The count is: 30
The mean is: 50.233333333333334
### Your solution here
print(f"The max is: {<YOUR_CODE_HERE>}")
print(f"The min is: {<YOUR_CODE_HERE>}")
print(f"The sum is: {<YOUR_CODE_HERE>}")
print(f"The count is: {<YOUR_CODE_HERE>}")
print(f"The mean is: {<YOUR_CODE_HERE>}")
2.4: Using list comprehension (NOT for loops), print out the numbers within the specified upper and lower bounds (inclusive) of 12 and 80.¶
Sample output:¶
[53, 63, 40, 48, 53, 38, 63, 13, 15, 66, 57, 79, 42, 25, 66, 73, 45, 80, 17]
### Your solution here