Assignment 1 Introduction to Python¶
Preparation:¶
- Open the assignment link on Canvas.
- Accept the assignment and link it with your name.
- A repository will be created under your name.
Use Amarel to launch a Shell terminal and use the terminal to do the following tasks:
- Create a new directory called
rcaes
within your home directory - Go to 'rcaes' directory.
- GitHub Authentication: gh auth login
- What account do you want to log into? GitHub.com
- What is your preferred protocol for Git operations? SSH
- Generate a new SSH key to add to your GitHub account? Yes
- Enter a passphrase for your new SSH key (Optional) *********
- Title for your SSH key: GitHub CLI
- How would you like to authenticate GitHub CLI? Login with a web browser
- First copy your one-time code: XXXXXX
- Press Enter to open github.com in your browser... Enter
- In the browser, enter the one-time code.
- Go back to your GitHub repository.
- Clone your GitHub repository: 'Code' -> 'GitHub CLI' -> 'gh repo clone rcaes2023/assignment_1-YOURGHNAME'
Python Lists and Loops¶
In this problem, we will explore the basic data structures and flow controls of python by manually parsing a CSV file.
Note that this is a futile exercise. In the "real world" you should never manually parse a CSV file. There are utilities out there that will do it for you much more quickly and efficiently. However, it is a useful exercise for learning python.
Before starting the python part, use the JupyterLab file browser to browse to this file. Click to open it. What do you see?
Now we will begin the process of reading the file with python
Open the file using the open
function¶
Specifically, run the command
file = open('rcaes_roster.csv')
Use the help
function to get the documentation for your new variable file
¶
This will produce a long list of methods you can use with file
.
Read the lines of the file into a variable called lines
¶
Hint: use the documentation above to find the method that sounds most likely to do what you want.
It should be a familiar type we learned about in class.
Display lines
at the end of a cell in order to see its contents¶
Display the number of students in class¶
Use slicing to display the first three items of the list. And the last 3¶
Now iterate through lines
and print
the item if it contains your NetID¶
By now you have figured out what is in this data. Let's now transform it into a more useful format.
Write code to transform the data into a dictionary whose keys are NetIDs and whose values are full names.¶
(Hint: You might need to review Python's string methods. You can start with creating an empty dictionary and adding keys/values in a loop.)
Use this dictionary to look up your own name using your NetID¶
Figure out who has the longest last name in the class (Bonus: 10)¶
(Hint: First create a list of last names.)
Assignment submission:¶
When you're done with your assignment, submit your assignment using git:
git add *
git commit -m 'first commit' (or any other message)
git push