Day 03: Pre-Class Assignment: Python Review and Intro to Git#

git logo

Student Identification#

Please do not modify the structure or format of this cell. Follow these steps:

  1. Look for the “YOUR SUBMISSION” section below

  2. Replace [Your Name Here] with your name

  3. If you worked with groupmates, include their names after your name, separated by commas

  4. Do not modify any other part of this cell

Examples:

Single student:  John Smith
Group work:      John Smith, Jane Doe, Alex Johnson

YOUR SUBMISSION (edit this line only):

✅ [Your Name Here]

Note:

  • Keep the “✅” symbol at the start of your submission line

  • Use commas to separate multiple names

  • Spell names exactly as they appear in the course roster

  • This cell has been tagged as “names” - do not modify or remove this tag

Table of Contents#

Learning Goals#

  1. Continue to revisit some basic Python skills (exploring Python Tutor and reviewing Python data structures)

  2. Learn the basics of version control using git.

  3. Set up git and GitHub

This assignment is due by 4:59 p.m. the day before class, and should be uploaded into the appropriate “Pre-class assignments” submission folder. Submission instructions can be found at the end of the notebook.


Part 1. Continuing to revisit your Python skills#

IMPORTANT NOTE: The content in this section is meant to be an opportunity for you to continue dusting any lingering cobwebs off of your Python skills. If you feel like you could use some refreshers on some basic Python content, work through this material. If you’re feeling pretty good about your Python skills, feel free to skim through the content and only spend time on the stuff that seems useful.

Building a conceptual model Using Python Tutor#

This section introduces a learning tool called Python Tutor. This is a web-based tool you can use to help figure out how the python interpreter maintains the state of variables and modules. I recommend using this tool anytime you are confused about what is going on inside of python. Please watch the following video for an example about using Python Tutor.

from IPython.display import YouTubeVideo
YouTubeVideo("1OQ_E1y73Es",width=800,height=450)

Watch the above video and answer the following questions:#

Question 1: In words, describe how python tutor represents the following two dimensional list:

l = [ [('me','you'), 20, 30], [4, 5, 6], ['a','b','c']]

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text). Please use appropriate Markup formating to make your answer’s easy to read.

Question 2: Play with Python Tutor to see what else you can visualize. Write an interesting example. Either an example that demonstrates something you didn’t fully understand or an example that highlights a limitation of the tool.

#Do This - Erase the contents of this code cell and put your example code here

Question 3: Describe what you learned or what you found interesting in your answer to Question 2.

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text). Please use appropriate Markup formating to make your answer’s easy to read.

Review of Python Containers (lists, sets, dictionaries, tuples)#

The following video reviews all of the way that Python can natively story data (without something like Pandas). Note that the one exception to this is that the video talks about NumPy arrays, which are not a default Python functionality. You might find it useful to review this material to reminder yourself how you can store and manipulate data in Python since you’ll be working a lot with data this semester.

from IPython.display import YouTubeVideo
YouTubeVideo("aBqTgR-gP3g",width=800,height=450)

The cell below includes a reproduction of the table from the video.#

Attribute

List

Dictionary

Set

Tuple

Mutability

Mutable

Mutable

Mutable (contents must be immutable)

Immutable

Initialization Without Values

a=list() or a=[]

student={}

s=set()

t=() or t=tuple()

Initialization With Values

a=['1', '2', '3']

student={'name': 'John Doe', 'age': 22, 'college': 'MSU'}

s=set(['a','b','c'])

1-tuple: t=('Hello',), 2-tuple: t=('Hello', 'Goodbye')

Adding Values

list.append(item) or list.insert(index, item)

student['major']='Computer Science'

s.add(item)

N/A

Removing Values

list.remove(item)

del dictName[keyName]

set.discard(item), set.remove(item), or set.pop()

N/A

Modifying Values

a[0] = 'cat'

student['age'] = 23

Must add or remove elements

N/A

Access Method

By index: a[0]

By key: student['college']

set.pop() (arbitrary element)

By index: t[0] or by slice: t[0:1:2]

Notable Operations

Various list methods

‘in’ keyword, key iteration

Set operations (difference, intersection, etc.)

Packing, unpacking, conversion to list

Use of the NumPy Array requires the NumPy Python Module. In these examples, it is assumed that the import statement is “import numpy as np”

Attribute

NumPy Array

Mutability

Mutable

Initialization Without Values

a=np.array([])

Initialization With Values

a=np.array([1,2,3,4,5])

Adding Values

np.insert(arrayName,index,values,axis) or np.append(arrayName,value,axis)

Removing Values

np.array(array,index/indices,axis)

Modifying Values

a[4] = 12

Access Method

By index: a[0] or by slice: a[0:5:2]

Notable Operations

Array manipulation routines


🗒️ Question 4: What is the difference between mutable and immutable objects? Give an example of a scientific model where you would choose one over the other (ex. maybe a tuple vs. a list). Explain the choice.

✏️ Answer:

🗒️ Question 5: Imagine a function that takes filename as a string input and counts the number of times each word appears in the file. What type of container would make the most sense for storing the words and the word counts? Why?

✏️ Answer:

🗒️ Question 6: A digital image is often represented as a three dimensional array. Where the first dimension is row, the second dimension is column and the third dimension is a color. Create a three dimensional containers of numbers with 10 rows, 10 columns and 3 colors (red, green and blue) and initialize all of the numbers to zero.

### ANSWER

Part 2. Getting to Know Git#

(NOTE: This material was either inspired by or drawn from Frisbie, R. L, Grete, P., & Glines, F. W. (2022))

2.1: Why use Git?#

Short answer: Sustainable Software Development. So what is Sustainable Software Development? Generally, when we say sustainable software, we mean software that:

  1. Is useful for more than a single task (like having a publication quality plotting program you can use for all plots instead of redoing it every time),

  2. Is shareable (so other people can read and understand it, and perhaps use it),

  3. It works not just on your computer with your particular settings (for file locations, python libraries, etc.)

  4. Finally, if the software is used by a larger community, then that community should know how to interact with the code and each other. Some examples: Android, Linux, Python, SciPy, matplotlib, Enzo, and many more!

🗒️ Task:

Before moving on, broadly think about collaborative software development from small to large projects. Write down questions, concerns, or general topics of interest pertaining to challenges and processes in different collaborative software development environments.

✏️ Answer:

Examples of Sustainable Software Challenges

In a professional setting, you’ll be asked to work on large projects with many people working on them simultaneously. This can create a logistical nightmare, as you want all of the different pieces that everyone is working on to be able to work together.

Let’s look at a few situations that can help us understand why Git is such a powerful tool in software development.

  1. You’re working on an app with four friends–Han, Mika, Jacob, and Navi–just for fun. You’re collaborating on code meant to look up files in a database, and Navi wants to add a function to the code. How can you both work on the same file at the same time?

  2. As you’re working on your app, you realize that yesterday’s change has broken the entire app. It was a pretty big change, and you’re not sure you can remember what the code looked like previously, and you don’t have time to rewrite it anyways. Moreover, your collaborators have also made changes to the code in the intervening time. How could you fix this problem?

  3. You’ve finished your app, and now you want to make it public, both so that the public can use it and that future employers can see it and see what you’re capable of doing. You also want to ensure that the code you worked so hard on isn’t lost if your computer dies. How can you make it accessible to others and ensure it isn’t easily lost?

🗒️ Question: What are some possible solutions to these problems?

✏️ Answer:

2.2: What is Git?#

👀 Watch the following video (<- click the link!) to get an understanding of what Git is and does.

🗒️ Task: In your own words, describe what a commit is.

✏️ Answer:

🗒️ Task: Now, go back to the problems and concerns you identfied in 2.1.1, as well as the issues raised in the section “Examples of Sustainable Software Challenges.”

Based on your new understanding of Git, if/how could you use Git to solve these problems?

✏️ Answer:


Part 3. Get git and GitHub setup#

3.1 git vs GitHub#

Before moving on we need to make a very important distinction.

git is the name of the software system that we’re using.

GitHub is a hosting service that we’re using to store our remote repos.

There are other hosting services–like Gitlab and Bitbucket–but we’ll be sticking with Github for this class. In order to help you with this distiction I will write git as git (i.e. as code format) while I will not add any format to GitHub. Below is a table summarizing the main differences.

Image

link to image source

3.2 Install git on YOUR computer#

DO THIS: It is very likely that you have git already installed on your computer. Let’s check.

Windows user You have several options. Choose one from below

  1. Using Command Prompt:

    1. Open Command Prompt and type: git --version

  2. Using Windows PowerShell:

    1. Open PowerShell and type: Get-Command git

  3. Check for Git in the Start Menu:

    1. Look for “Git” or “Git Bash” in your Windows Start menu.

Mac and Unix user You have several options. Choose one from below

  1. Open Terminal and type:

    1. git --version

    2. or which git

    3. or command -v git

If git isn’t found using these methods, it’s likely not installed on your system. To install go to this page, download, and install git on your computer.

3.3 Setup your GitHub account#

Go to github.com and make an account (if you haven’t already done so).

IMPORTANT: Generating a Personal Access Token for GitHub#

In order to clone private repositories and push/pull changes to those repositories on GitHub, you will also need a Personal Access Token (PAT) to access GitHub via the terminal on your computer or within JupyterHub (for security reasons).

To get a PAT, watch this video. 👀

Useful suggestions: Once you’ve set up a PAT and you’ve confirmed that you can use it to access GitHub, we have a couple of suggestions:

  1. Save it somewhere safe (like in a password manager or in a file on a secure computer) because you’ll need it again later.

  2. You may wish to store your GitHub login credentials in your git configuration file so that you don’t have to enter them every time you want to push/pull changes to/from GitHub. To do this, you can run the following command in your terminal:

    git config --global credential.helper store
    

    Note: If you are working on your computer and you’re using a Mac, you may need to use the following command instead of the one above:

    git config --global credential.helper osxkeychain
    

Part 4. Using Git#

Finally, let’s start getting our hands dirty with git.

4.1 Make a remote repo#

DO THIS: Make a new repo called CMSE802-S25-turnin. This will be the repo where you will turn in your report and final project. Follow the instructions below to create a remote repo

  1. At the GitHub Dashboard

    • Find and click New on the left hand side of the page under your account name.

  2. In the Create a new repository page:

    • Set Repository name as CMSE802-S25-turnin.

    • Set this repo to be Private.

  3. Click Add a README file.

  4. Click Add .gitignore and select Python.

    • This will allow you to ignore certain temporary Python files.

  5. Click Choose a license and select a license you like if you know what they mean otherwise choose None.

  6. Click Create repository.

  7. Once created, you will be in your new repo.

Congrats you have now a remote repo in GitHub. Since this is a private repo you need to provide access to your instructor and TA.

  1. Click ... on the right side of the page right under your avatar, select Settings.

  2. In the General settings page, click Collaborators in the menu on the left.

  3. Click Add people :

  • Type lucianogsilvestri, yep that’s me the one with the funny glasses and grin, select and click Add...

  • Type ZhyShe, select and click Add...

4.2 Clone the repo and make your first commit#

👀 Watch this video to get a quick tutorial on using Git and GitHub. (<- click the link!)

NOTE: There is a misstatement around 3:00 in the video; the version of the repo that they are in is the local repo, not the remote repo.

DO THIS: with reference to the video above

  1. Clone the repo you created on your computer using a terminal (or anaconda prompt)

  2. Modify the README file, using your IDE or text editor of choice, so that it has your name and the departmenmt you are in,

  3. Commit your changes.

  4. Push your changes to the remote repo.

4.3 Sync GitHub and VS Code#

DO THIS: It is convenient sync your GitHub account with VS Code.

  • Open VS Code.

  • Click the portrait icon on the lower left corner and select sign in to sync settings.

    • You may be prompted to login to GitHub if you log out from your browser.

  • Click the portrait icon again to see if it shows your_github_user_name (github).

    • If so, you are good.

    • If not, you need to make sure you login to GitHub in your browser.


Assignment wrap-up#

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment! If running the cell doesn’t work in VS Code copy the link src and paste in the browser. Make sure to sign in with your MSU email.

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://forms.office.com/r/AEc6LS6xKF" 
	width="800px" 
	height="600px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, you’re done with your pre-class assignment!#

Now, you just need to submit this assignment by uploading it to the course Desire2Learn web page for the appropriate pre-class submission folder (Don’t forget to add your name in the first cell).

© Copyright 2023, Department of Computational Mathematics, Science and Engineering at Michigan State University