Lab 3A: Python Fundamentals¶
In this lab you will be working on python basics.
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
.
Objectives¶
In this lab you will:
Practice creating and using Python functions.
Practice using Python to work with directories and files.
Practice working with vectors and the numpy package.
Practice working with Dictionaries and Lists.
Practice creating a pandas data frame from a dictionary.
# Add your import commands here
Task 1: Creating Functions (3 marks)¶
In your projects, you may need to create your own functions to do some complex calculations (beyond mean, median etc…). In this question you will write a function to find the roots of a quadratic equation.
To refresh your memory from high school math, a quadratic function looks like this:

Given the standard form of a quadratic equation, \(ax^2 + bx + c = 0\) where a, b, c are known real numbers and \(a\neq0\), the quadratic formula to find unknown \(x\) is: $\(x=\frac{-b ± \sqrt{b^2-4ac}}{2a}\)$
Write a python function that calculates and prints all possible real (i.e. non-complex) answers for \(x\).
Check your function by running it on some test data points:
solution(1,1,1)
,solution(1,0,-4)
,solution(1,2,1)
.
Sample Output¶
–> For a=1, b=1, and c=1:
The equation does not have any real solution
–> For a=1, b=0, and c=-4:
x1 = 2.0 and x2 = -2.0
–> For a=1, b=2, and c=1:
x = -1.0
def solution(a, b, c):
# Your solution here
File "/tmp/ipykernel_1918/1302331763.py", line 2
# Your solution here
^
SyntaxError: unexpected EOF while parsing
solution(1,1,1)
solution(1,0,-4)
solution(1,2,1)
Task 2: Functions (Total: 6 marks)¶
Remember in a previous lab, you used the tree command to show a map of all directories and files within a directory. In this section, we want to make a similar program, but using python instead.
2.1: ListFiles()
(2 marks):¶
Task: Your task is to create a python function called ListFiles()
that takes in a directory path and then finds and print all the files (not directories) in a given directory (relative to ./
).
We suggest you use the listdir()
function and isfile()
functions from the os
module.
In your GitHub repository, there is a folder called directory_list
which contains some sample files.
Hint 1: don’t forget to first import os
to use os.listdir()
and os.path.isfile()
.
Hint 2: You can see the following tutorials if you need some extra help or some worked examples: link1 and link2.
Sample Output¶
directory/file1.txt
directory/file2.txt
directory/file3.txt
directory/file4.txt
def ListFiles(address):
# Your Solution here
File "<ipython-input-1-3ebdd80ac7e9>", line 2
# Your Solution here
^
SyntaxError: unexpected EOF while parsing
ListFiles("directory_list")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-6a896d3fc113> in <module>
----> 1 ListFiles("directory_list")
NameError: name 'ListFiles' is not defined
2.2: ListDirectories()
(2 marks):¶
Task: Use the os.listdir()
and os.path.isdir()
function to find and print all the directories (not files) in a given folder.
Create a python function ListDirectories(path)
that prints all the directories in that folder (and subfolders).
You may also use the new pathlib
package from Python if you like*
Sample Output (order does not matter)¶
directory_list/dir2/
directory_list/dir4/
directory_list/dir3/
directory_list/dir1/
def ListDirectories(address):
# Your Solution here
ListDirectories("directory_list")
2.3: tree()
(2 marks):¶
Task: Write a function tree(path)
that prints a map of all directories AND files within a directory.
Hint: you can use the functions you created above, or you may use the os.walk()
function to complete this task (there are many ways to do it). Another option is to use glob. You can explore multiple solutions if you like.
Sample Output¶
directory_list/file1.txt
directory_list/file2.txt
directory_list/file3.txt
directory_list/file4.txt
directory_list/dir1/
directory_list/dir1/file24.txt
directory_list/dir1/file23.txt
directory_list/dir2/
directory_list/dir2/file34.txt
directory_list/dir3/
directory_list/dir3/file110.txt
directory_list/dir3/file140.txt
directory_list/dir3/file130.txt
directory_list/dir3/file120.txt
directory_list/dir4/
directory_list/dir4/file11.txt
directory_list/dir4/file12.txt
directory_list/dir4/file13.txt
directory_list/dir4/file14.txt
def tree(path):
# Your Solution here
# Test your function
tree("directory_list")
Task 3: numpy
(Total: 8 marks)¶
In this section, we will practive using the numpy
library.
First, import the numpy
library
import numpy as np
3.1: Create a vector (3 marks)¶
Task: Create an empty vector of size 10 filled with NaN.
Hint: you need to use zeros()
method or the empty()
method
# Your Solution here
3.2: Working with Vectors (2 marks)¶
Task: Create a random vector of size 10 and then find its mean, max, min, and sum value.
Hint: for random vector you need to use np.random.random()
and for the mean, max, min, sum you need to use build-in numpy methods mean()
,max()
,min()
,sum()
.
Sample output (Your numbers will be different)¶
[0.66698639 0.32937943 0.12074085 0.21788496 0.75628444 0.56461791 0.38162184 0.60966053 0.00491222 0.80007239]
The max is: 0.800
The min is: 0.005
The sum is: 4.452
The mean is: 0.445
# Your Solution here
3.4: More vectors (3 marks)¶
Task: Using numpy
Create a vector of size 15 which contains random values ranging from 10 to 90 and replace the minimum value with 100 and maximum value with 0. Print the mean, befor and after.
Hint: you may need to use argmax()
and argmin()
.
Documentation for that can be found here.
Sample output¶
before: [20 27 10 63 57 71 50 18 76 56 38 62 10 9 12]
mean: 38.6
after: [ 20 27 10 63 57 71 50 18 100 56 38 62 10 9 12]
mean: 40.2
# Your Solution here
Task 4 - Dictionaries, Lists and data manapulation (10 marks)¶
In this part we explore another fundamental data structure in python, called Dictionaries.
4.1: Create a dictionary that has 3 keys: name, age and salary, and enter in the following dummy information. (2 marks)¶
Sample output¶
{‘name’: ‘Tim Cook’, ‘age’: 59, ‘salary’: 3000000.0}
### Your solution here
4.2: Create a second dictionary, this time with at least 5 different names, ages, and salaries. (2 marks)¶
Hint: There should only be three keys, and the values should be a list.
Sample Output¶
{‘name’: [‘Tim Cook’, ‘Person 2’, ‘Person 3’], ‘age’: [59, 24, 40], ‘salary’: [30000000.0, 200000.0, 900000.0]}
### Your solution here
4.3: Create a pandas dataframe using the dictionary you created above (2 marks)¶
Hint: Use the pd.DataFrame.from_dict()
method