Homework 1: Python fundamentals#
✅ Put your name here
#
Learning Goals#
Content Goals#
Review the arithmetic operators in Python
Utilize loops to perform repeated operations and to store, access, and manipulate data in one more or lists
Utilize variables and lists to store and access data
Use conditional statements to control code flow.
Practice Goals#
Use the internet as a resource for learning how to write code, learn new things, and troubleshoot problems
Solve a variety of problems using code
Troubleshoot errors and debug your code as issues arise (you’ll do this all the time in this course!)
Assignment instructions#
Work through the following assignment, making sure to follow all the directions and answer all the questions.
This assignment is due at 11:59 pm on Friday, September 25th. It should be uploaded into the “Homework Assignments” submission folder for Homework #1. Submission instructions can be found at the end of the notebook.
Version check#
Before you begin, make sure your Jupyter kernel is Python 3. There are significant differences between Python 2 and Python 3, and we use Python 3 in this course. The kernel type at the upper-right corner should say “Python 3”, and the following code should show a version of the form: 3.x.y (where “x” and “y” are the sub-version numbers).
If your kernel is Python 2, try selecting Python 3 from the Kernel menu above under the sub-menu “Change kernel”. If that does not work, consult the Teams help channel for assistance.
import sys
print(sys.version)
3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]
IMPORTANT NOTE: Throughout this notebook, you will see a number of cells that say “DO NOT EDIT OR MOVE THIS CELL”. Please leave these cells where they are and do not edit them. Moving or editing them makes it more challenging to divide the homework into questions for grading. Your TAs will thank you for leaving them where they are!#
Grading#
Integrity statement and office hours
Academic Integrity Statement (2 points)
Going to Help Room (8 Points)
Doing basic math with Python (10 points)
Storing and accessing data using lists (20 points)
Writing for loops in Python (20 points)
Writing while loops in Python (20 points)
Writing if statements in Python (20 points)
Total points possible: 100
0.1 Academic integrity statement (2 Points)#
In the markdown cell below, paste your personal academic integrity statement. You should have generated this in the Day 4 pre-class assignment, if you did not do this pre-class assignment, locate that assignment, find the appropriate section, and draft your integrity statment now.
By including this statement, you are confirming that you are submitting this as your own work and not that of someone else.
✎ Put your personal academic integrity statement here.
DO NOT EDIT OR MOVE THIS CELL
0.2 Going to the CMSE 201 Help Room (8 Points)#
Why are we doing this?#
We want to make sure that everyone knows how to access the resources available to you. One of the best resources you have at your disposal is office/help room hours, which are offered by the instructors, TAs, and LAs associated with CMSE 201.
What will you do?#
(At minimum) Go to one help room session(it doesn’t matter which one you go to). Come with one question that you would like to talk about. It can be big or small. Introduce yourself to someone there, ask your question, and remember their name. Note that this does not have to be a ULA, TA, or Instructor. There will be a QR code in the help room that you can scan. This is how we will be giving you credit for this part of the assignment. Scan the QR code while in the help room, fill out the questions it asks, and submit it. If you cannot scan the QR code, there will be a link under it that you can manually enter into your computer to fill out the questions.
If your schedule is such that you cannot attend the help room hours, send and email to the CMSE 201 lead instructor, Dr. Rachel Frisbie (salmonra@msu.edu), to coordinate an option to make sure you can get credit for this part of the assignment.
NOTE: The day when the homework is due (Friday, September 25th) will be the busiest time for folks to go to the help room. You are STRONGLY encouraged to go to the help room before that Friday to get credit for this part of this assignment. If you show up to help room hours on that Friday and it’s very busy, there is no guarantee that one of the instructors, TAs, or LAs will be able to help you before they have to go home.
You can find the office hours and help room calendar here CMSE 201 help room and office hours schedule.

The above image shows a map of the second floor STEM building with a red line going from STEM room 2010 where the CMSE 201 help room is to the elevator/stairs right below STEM Room 3201 where CMSE 201 class meets
DO NOT EDIT OR MOVE THIS CELL
1. Doing basic math with Python (10 points)#
As you explored with your order of magnitude modeling assignment in the Day 2 in-class assignment, Python is great at doing basic math – much like your favorite calculator! As you get more comfortable with writing Python code, you might find yourself writing a bit of code when you need to do some calculations instead of pulling digging out your calculator or launching the calculator app on your phone.
Doing basic math with Python involves using various “operators” – these operators are the Python symbols used to do the actual math. For example, to add two variables, we use the “+” operator. To subtract, we use the “-” operator.
There are a number of other arithmetic (math) operators as well.
Your task is to use many of the basic arithmetic operators so you can see how they are represented in Python.
Create the variables: a,b,c,d, and e and assign them values \( 8, 12, 0, 0.5,\) and \(3\). Then using arithmetic operators, calculate and print the result from this equation:
\begin{equation}
\mathrm{Result} = e (1 - \frac{b+c}{a})^{d}
\end{equation}
#Write your code here
DO NOT EDIT OR MOVE THIS CELL
2. Storing and accessing data using lists (20 points)#
The Python list datatype can be a really useful way to store and access data using the functionality that is built into the default version of Python. As the semester progresses, we’ll learn about other Python “libraries” (packages of code designed to do useful things) designed to work with data, but for now, the list datatype is our primary tool.
Creating a list from scratch#
There are a variety of ways you can create a list and fill it with values and you’ll want to make sure you’re comfortable doing this.
✅ 2.1 List Creation and Manipulation (5 points)#
Create a list containing your first pet’s name, your age when you had the pet, and what type of pet it was. Print that list. If you have never had a pet, please make up these details.
Then, answer the questions below.
#Put your code here
✅ 2.1.1 Metacognition#
What data types did you use in creating your list?
In this case, do you think the order of the items in this list matters? why or why not?
✎Write your answer to 2.1.1 here:
DO NOT EDIT OR MOVE THIS CELL
Accessing and changing values stored in a Python list#
Once we get comfortable with creating lists with some set of initial values, we need to get comfortable with accessing the information stored in the list. The way that we access information is by using an index to access a value at a particular position in the list. Index values are integers that allow you to refer to values at specific positions in the list. The first index is always 0 in Python. So the third element in a list would be accessed using an index of “2”. You’ll just need to get in the habit of thinking this way when working with Python.
Using an index not only allows us to access a value in a list, but it also allows us to update or change values in a list.
✅ 2.2 Accessing values (5 points)#
Imagine you are keeping track of the number of students attending a study session on five different days. The attendance numbers were: \(12, 18, 15, 21, and 16.\)
Complete the following tasks:
Create a list called
attendancethat stores these five values in the order shown.Print the first value in the list using its index.
Print the fourth value in the list using its index.
#Put your code here
✅ 2.2.1 Metacognition#
Why does
attendance[3]print the fourth value rather than the third?What mistake might you make if you forgot that Python starts counting list positions at 0?
✎Write your answer to 2.2.1 here:
DO NOT EDIT OR MOVE THIS CELL
Adding or “appending” values to a Python list#
From time to time, we might need to do more than just access and change values inside a list, we might need to add or “append” items to a list. This can be especially useful if our code generates information in a loop and we want to make sure we store that information as we go along. Or, alternatively, we might find situations where we want to append certain information to our list based on a specific condition. In these sorts of situations, the append() function is useful for adding items to a pre-existing list.
✅ 2.3 Exercise Minutes (10 points)#
You have decided to keep track of how many minutes you exercised each day over five days. Your exercise times were: \(30, 45, 20, 50,\) and \(35\) minutes.
Complete the following tasks:
Create a Python list called
exercise_minutesthat stores these five values in the order shown.Print the second value in the list using its index.
You realize that the third value is incorrect. Change it from 20 to 25 using its index.
On a sixth day, you exercised for 40 minutes. Add 40 to the end of the list using
.append().Print the last value in the list using its index.
Use
len()to print the number of values now stored in the list.Print the completed
exercise_minuteslist.
#write your code here
DO NOT EDIT OR MOVE THIS CELL
3. Writing for loops in Python (20 points)#
The for loop is a fundamental programming concept in many programming languages and it is one method of what is referred to as “control flow,” which is simply how the flow of execution of a program is controlled (get it?). The for loop basically tells the computer to execute the code inside the loop a specific numbers of times. The number of times the loop should be executed can be defined using the range() function or you can tell Python to iterate through sequence of values, e.g. a list, and run the loop until every value has been accessed. We’ll sometimes refer to these as “looping by index” or “looping by value”, respectively.
Looping by integers or index#
When we use the range() function in a for loop, we end up generating a set of integer values, which we often use as the index for accessing information stored in a Python container, like a list.
It’s important to understand how to use these integer index values to access elements of a list in the code that lies inside the loop, but for now we’ll just think about how we can loop over a set of integer values using a for loop.
Looping by value (e.g. through a list)#
Another way that Python allows us to write for loops is by looping through values stored in a sequence of some sort. We often seen this with for loops that iterate through a Python list.
This can be really useful if we have some important data stored in a list and want to go through and do something with each value/item stored in that list. For example, if you had a list of all the students in your section of CMSE 201, you could use a for loop to print out all of their names when you wanted to see the class roster.
Writing a loop that accesses list values using integers or “by index”#
Now that you’ve looked at how you can use a for loop to move through a series of integer values and how you can use a for loop to step through a Python list of values, you’re go to explore how you can use the integer-based for loop approach to index values in a list inside a loops.
If you find yourself in a situation where you need to do a set of repeated operations on using more than one list at a time, using this integer or index-based approach can be really valuable.
✅ Question 3.1: For Loops#
For this question, you will be working with some fundamental datatypes in Python. Namely, we will be working with lists, strings, and numbers.
Create a list called
grab_bagthat contains the strings: “laptop”, “coffee”, “moped”, “sweatshirt”, and “shoes”, in that order.Using a
forloop, print all of the elements that are in the list,grab_bag.Replace the string “coffee” with the string “Michigan State”. Then, replace the string “shoes” with the number 15. Then print the new list. After you print the list, set the number 15 to be the letter ‘c’.
Replace the string “sweatshirt” with the list of numbers
[4, 8, 15, 16, 23, 42]and print the new list.Use a command to find the length of the list
grab_bag. Then write a loop that finds the length of each element in the listgrab_bag. Include print statements to specify what you are finding the length of. For example, “The length of laptop is: 6” or “The length ofgrab_bagis: 5”.
# Write your solution for the first bullet point here
# Write your solution for the second bullet point here
# Write your solution for the third bullet point here
# Write your solution for the fourth bullet point here
# Write your solution for the fifth bullet point here
DO NOT EDIT OR MOVE THIS CELL
4. Writing while loops in Python (20 points)#
The while loop is another fundamental programming concept in many programming languages and another “control flow” method. The while loop basically tells the computer to execute the code inside the loop until a particlar condition is met. This means that the number of times the loop is executed may not be immediately known because the conditional statement used to determine when to exit the loop is often dependent on the code that is being called inside the loop.
While using a conditional statement to determine when to stop iterating over the loop can be really useful, it can also be really problematic. A poorly written while loop can result in what is often referred to as an “infinite” or “endless” loop. An infinite loop is a loop that, as you might expect, runs continuously with no end (hence the alternate “endless” name). Infinite loops occur when the conditional statement used in the while loop never becomes False. If the conditional statement is always True, then the loop will run forever.
When using while loops in Python, watch out for infinite loops!. You’ll often notice that you’ve created an infinite loop when you see In[*] on the left-hand side of the cell you just executed for longer than expected. Normally, when a cell finishes running, the * becomes a number. Take a look at your code cells above to confirm this.
If, in this assignment, you’ve found that you created an infinite loop, or suspect that you might have, you can “interrupt” the notebook and stop the code from trying to run. Then you can fix your error and try again. To interrupt your notebook and stop the cell from running, click on the “Kernel” tab at the top of your notebook and select “Interrupt” or, simply click on the ■ at the top of the notebook.
Looping by condition#
When we use a while loop, we are telling the computer to repeat a block of code until a particular condition is met. The condition may be met with only a few iterations or it may take many iterations. If we’re not sure in advance how many iterations will be necessary, using this condition-based while loop can be a great option.
Important: When deciding to use a while loop to tackle a problem make sure to first ask yourself, does this really require a while loop or would I have better luck with a for loop? It’s important to get into the habit of choosing the right tool for the job!
✅ Question 4.1: While Loops#
You invest \(\$ 1200\) into a savings account with an interest rate of 5\(\%\) compounded biannually (twice per year). Note that for an initial amont, \(P_0\), with interest rate \(r\) (in decimal form), the final amount when the interest is compounded \(n\) times per year after \(t\) years is given by the formula
Your goal is to write a while loop to approximate when (in years) the amount you have invested doubles in value.
# Write your solution to question 4.1 in this cell
DO NOT EDIT OR MOVE THIS CELL
5. Writing “if” statements in Python (20 points)#
Conditional statements that are used outside of while loops in Python, often referred to as “if” statements, are a really useful way to control the flow of your code so that certain pieces of code are only executed when specific conditions are met (hence the term “conditional”). Getting used to using if statements, and more complex if-else or if-elif-else statements, is an important part of learning to build evermore complex algorithms.
One of the most challenging aspects of writing if statements is just thinking through the logic of what the right conditions are to get the code to do what you want. When writing if statements, it can be a useful exercise to try and think through the variety of outcomes that might be possible when your code runs and determine how your if statements would be evaluated given these possible outcomes.
✅ 5.1 Distance to campus (10 points)#
You are helping your friends decide how they should get to campus based on how far away they live. Their distances from campus, in miles, are stored in the list below:
Write a for loop that goes through each distance in distances. For each distance:
If the student lives less than 1 mile from campus, print “Walk to campus.”
If the student lives between 1 and 3 miles from campus, print “Bike to campus.”
If the student lives more than 3 miles from campus, print “Take the bus or drive to campus.”
After all distances have been checked, print “All commute recommendations are complete!”
distances = [0.5, 2.5, 4, 1.2, 3.5, 0.8]
#Write your code here
✅ 5.1.1 Metacognition#
What do you think will happen when distance is exactly 1 mile? What about exactly 3 miles? Explain your reasoning.
This problem was solved using a for loop. Could you have solved it with a while loop instead? What would need to change in your logic to make a while loop work, and which approach feels more natural for this specific task?
✎Write your answer to 5.1.1 here:
DO NOT EDIT OR MOVE THIS CELL
✅ Question 5.2: Making a spending decision (10 points)#
Imagine you check your bank account before making plans for the weekend. Your current balance is: \(\$\)185
Create a variable called
account_balanceand set it equal to 185.Create a
whileloop that continues running as long asaccount_balanceis greater than 0.Inside the loop, print the current account balance so you can see how the amount changes each time the loop runs.
Use an
ifstatement to check whether the account balance is greater than \(\$\)150. If it is, print: “You have plenty of room in your budget.”Use
elifstatements to check the other balance levels:If the balance is greater than \(\$\)75, print: “You have some room in your budget.”
If the balance is greater than \(\$\)25, print: “Keep your weekend plans cheap.”
Use an
elsestatement for balances of \(\$\)25 or less and print: “Stay in and save money.”At the end of each loop, subtract \(\$\)20 from
account_balance.Once the balance is no longer greater than \(\$\)0 and the loop ends, print: “Your weekend budget is gone!”
#Write your code here
✅ Reflection Questions#
For the set of questions about if, elif, else statements, write your answers below these questions.
Which part did you have to think about the most before writing your code?
Was there a point where your code did not work as expected? What did you try next?
Did you use an example from class, your notes, or previous code to help you? How did it help?
If you had to complete a similar problem without your notes, which part would you feel most confident about? Which part would you be least confident about?
✎Write your answer here:
DO NOT EDIT OR MOVE THIS CELL
Wait! Before you submit your notebook, read the following important recommendations#
When your TA opens your notebook to grade the assignment, it will be really useful if your notebook is saved in a fully executed state so that they can see the output of all your code. Additionally, it may be necessary from time to time for the TA to actually run your notebook, but when they run the notebook, it is important that you are certain that all the code will actually run for them!
You should get into the following habit: before you save and submit your final notebook for your assignments, go to the “Kernel” tab at the top of the notebook and select “Restart and Run all”. This will restart your notebook and try to run the code cell-by-cell from the top to the bottom. Once it finished, review you notebook and make sure there weren’t any errors that popped up. Sometimes, when working with notebooks, we accidentally change code in one cell that break our code in another cell. If your TA stumbles across code cells that don’t work, they will likely have to give you a zero for those portions of the notebook that don’t work. Testing your notebook one last time is a good way to make sure this doesn’t happen.
Once you’ve tested your whole notebook again, make sure you save it one last time before you upload it to D2L.
Congratulations, you’re done!#
Submit this assignment by uploading it to the course Desire2Learn web page. Go to the “Homework Assignments” section, find the submission folder link for Homework #1, and upload it there.
Copyright © 2026, Department of Computational Mathematics, Science and Engineering at Michigan State University, All rights reserved.