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

Day 3#

✅ Put your name here

#

git logo

Goals for today’s pre-class assignment#

  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.

Assignment instructions#

Watch the videos below, do the readings linked to below the videos, and complete the assigned programming problems. Please get started early, and come to office hours if you have any questions! You should also use Slack to help each other thought any issues you run into!

This assignment is due by 11: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.#

Note: If you’re using “JupyterLab” instead of the standard Jupyter Notebook interface, you may find that this table runs off the edge of your browser window. If this happens, you are encouraged to open it as a standard Jupyter Notebook to end up with a version of the table that will scroll left-to-right. Newer versions of JupyterLab appear to have resolved this issue though.

Container Type

Mutable or Immutable

Initialization Without Values

Initializtion With Values

Adding Values to Container

Removing Values from Container

Modifying Values

Access Method

Notable Operations and Additional Information

List

Mutable

  • a=list()
  • a=[]

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

  • list.append(item) #Adds item to the end of the list
  • list.insert(index, item) #Adds item to the specified index in the list

list.remove(item) #removes the first instance of 'item' from the list. If there is not such element, this will cause an error

>>> a[0] = 'cat'
>>> a
['cat', '2', '3']

Access by index:
>>> a[0]
1

See webpage at http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch14s07.html for some helpful methods when dealing with lists.

Dictionary

Mutable

student={}

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

>>> student['major']='Computer Science'
>>> student
{'name': 'John Doe', 'age': 22, 'college': 'MSU', 'major': 'Computer Science'}

del dictName[keyName] #This method removes all entries associated with the given key

>>> student['age'] = 23
>>> student
{'name': 'John Doe', 'age': 23, 'college': 'MSU', 'major': 'Computer Science'}

Access by key word. Note that this key must be a string.
>>>student['college']
MSU

The ‘in’ keyword can be very helpful with dictionaries. Ex:

  • 'k' in dict #Returns true if key 'k' is in dictionary dict
  • 'k' not in dict #Returns true if key 'k' is not in dicitonary dict
  • for key in dict #This will iterate over all keys in dictionary dict

See webpage at http://www.python-course.eu/python3_dictionaries.php for additional helpful methods and operations

Set

Mutable. However the objects contained within a set must be immutable.

s=set()

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

s.add(item)

  • set.discard(item) #If item is in the set, the item is removed, otherwise nothing happens
  • set.remove(item) #If item is in the set, the item is removed, otherwise raise a KeyError
  • set.pop() #Remove and return an arbitrary element from the set. If the set is empty, raise a KeyError

Sets are unordered, therefore indexing does not mean anything. To modify a set, you must directly add or remove elements.

>>> set.pop() #This will remove and return an arbitrary element from the set

Some helpful methods include:

  • difference()
  • intersection()
  • isdisjoint()
  • union()

See webpage at http://www.programiz.com/python-programming/set for additional helpful methods and operations

Tuple

Immutable

  • t=()
  • t=tuple()

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

N/A

N/A

N/A

t=('Hello','Goodbye','Goodnight')

  • Access By Index:
    >>> t[0]
    'Hello'

  • Access By Slice
    >>>t[0:1:2]
    ('Hello','Goodbye')

  • Packing and Unpacking

  • Tuple to List: list(tupleName)

NumPy Array*

Mutable

a=np.array([])

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

  • np.insert(arrayName,index,values,axis) #Inserts a value for an array at the given index.
  • np.append(arrayName,value,axis) #Appends values to the end of an array.

np.array(array,index/indices,axis) #Returns a new array with the given index or array of indices deleted on the given axis

>>> a[4] = 12
array([ 1,  2,  3,  4, 12])

For additional information on manipulating NumPy Arrays see the webpage at http://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html

  • Access By Index:
    >>> a[0]
    1
  • Access By Slice:
    >>> a[0:5:2]
    array([1, 3, 5])

See webpage at http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html for further information about indexing of NumPy Arrays

See webpages at http://www.scipy-lectures.org/intro/numpy/array_object.html and https://docs.scipy.org/doc/numpy-dev/user/quickstart.html for additional information on NumPy Arrays

*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”


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.

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 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?

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 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.

#Do This - put your code here to generate a 3D container.

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?#

Answer: Sustainable Software Development#

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!

2.1.1:#

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.

Write your answer here.

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?

2.1.2:#

What are some possible solutions to these problems?

Write your answer here.

2.2: What is Git?#

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

2.2.1#

In your own words, describe what a commit is.

Write your answer here.

2.2.2#

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?

Write your answer here.

A BRIEF INTERLUDE TO GET GIT SETUP#

Github Account#

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

As a quick, but important, aside: 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.

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 working locally (not on JupyterHub) 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
    

2.3: Using Git#

Let’s start getting our hands dirty with Git.

👀 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.

2.3.1#

Make a new repo called CMSE202-f24-turnin. This will be the repo where you will turn in your homework assignments and your exams.

Once you’ve made your repo, clone it to JupyterHub, modify the README file so that it has your name and what section of CMSE 202 you’re in, and commit your changes. Then, push your changes to the remote repo.


Follow-up Questions#

Copy and paste the following questions into the appropriate box in the assignment survey include below and answer them there. (Note: You’ll have to fill out the section number and the assignment number and go to the “NEXT” section of the survey to paste in these questions.)

  1. Python review: How often do you use the “dictionary” variable type in Python? Can you come up with an example for when a dictionary is particularly useful for storing information?

  2. Have you ever used Git prior to this course? If so, in what context?

  3. In your own words, explain why using a software tool like Git might be useful when you’re working on a collaborative coding project.


Assignment wrap-up#

Hopefully you were able to get through all of that! We’ll be trouble-shooting any issues you had, working with remote git repositories, and testing out some more complicated git commands in class.

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!

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://cmse.msu.edu/cmse202-pc-survey" 
	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 2024, Department of Computational Mathematics, Science and Engineering at Michigan State University