Class 5 -For Loops

Class Overview

Today we will work on two things.

  1.  We are going to solve equation of a line and then plot it).
    1. Work together to do it by hand.
    2. This is “easy” and you have done it many of times.  But it can be hard for your brain to convert to python.
    3. Then each person will implement it themselves.
  2. Then we are going to work on For loops (pdf=for-loops).
    1. Here is some reading on for loops.  http://www.tutorialspoint.com/python/python_for_loop.htm
  3. Today’s files are at

VIDEO

Quiz is on courseworks for the video!

Homework 4 Due Monday September 22, 2025

QUESTION 1

Use 2 nested for loops.  This is a for loop inside a for loop.  Count up in the outer for loop from 0 to 9 and then at every step count back down to zero.  Remember that in np.arange(start,stop,skip).  You give it a start,stop, and skip.  We usually put numbers in.  But you don’t have to add numbers you could put a parameter in.  Add spaces like below.

Answer Question 1.

i= 0
k= 0

 

i= 1
k= 1
k= 0

 

i= 2
k= 2
k= 1
k= 0

 

i= 3
k= 3
k= 2
k= 1
k= 0

 

i= 4
k= 4
k= 3
k= 2
k= 1
k= 0

 

i= 5
k= 5
k= 4
k= 3
k= 2
k= 1
k= 0

 

i= 6
k= 6
k= 5
k= 4
k= 3
k= 2
k= 1
k= 0

 

i= 7
k= 7
k= 6
k= 5
k= 4
k= 3
k= 2
k= 1
k= 0

 

i= 8
k= 8
k= 7
k= 6
k= 5
k= 4
k= 3
k= 2
k= 1
k= 0

 

i= 9
k= 9
k= 8
k= 7
k= 6
k= 5
k= 4
k= 3
k= 2
k= 1
k= 0

 

QUESTION 2.

Go back and get the monthly precipitation from central park we used in class 3 (NOT class 1).  Remember that we used this data when we were learning about lists and learning how to make plots.   Use a for loop and sum up the monthly data.  Then call the function sum on your data.  It will be np.sum(precip_list).  How do they compare?

Answer Question 2.

For loop sum= 45.13

sum function sum= 45.13

QUESTION 3.

Create two lists.  One with the day of the week and one with the number of classes you have on that day. Use a for loop to go through your lists and printout how many classes you have each day.  Then write your total number of classes.

On Sunday Brian has 0 Classes
On Monday Brian has 2 Classes
On Tuesday Brian has 0 Classes
On Wednesday Brian has 2 Classes
On Thursday Brian has 0 Classes
On Friday Brian has 0 Classes
On Saturday Brian has 0 Classes

In total Brian has 4 Classes
QUESTION 4.

Using np.arange or np.linspace and a for loop to recreate what is below.  Remember that np.sin takes radians and not degrees. You can convert yourself or use the function np.deg2rad().  Also, I wrote out degrees and did not use the symbol because latex does not show up in print statements but it does on graphs.  Don’t forget what you learned about format statements a few classes ago to get all the decimals correct.

The sine of 0 degrees is 0.00
The sine of 10 degrees is 0.17
The sine of 20 degrees is 0.34
The sine of 30 degrees is 0.50
The sine of 40 degrees is 0.64
The sine of 50 degrees is 0.77
The sine of 60 degrees is 0.87
The sine of 70 degrees is 0.94
The sine of 80 degrees is 0.98
The sine of 90 degrees is 1.00
The sine of 100 degrees is 0.98
The sine of 110 degrees is 0.94
The sine of 120 degrees is 0.87
The sine of 130 degrees is 0.77
The sine of 140 degrees is 0.64
The sine of 150 degrees is 0.50
The sine of 160 degrees is 0.34
The sine of 170 degrees is 0.17
The sine of 180 degrees is 0.00
QUESTION 5.

Use two for loops to do something interesting.  In Markdown say what you are going to do and the program it.  So describe the code and then run the code.  Your choice.

Final 10%

Fun with two for loops, line breaks, new lines, and np.arange.  Can you make the pattern below?  Remember you can do math in np.arange.

0
0 

1
1 2 3 4 5 6 7 8 9 10 

2
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

3
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 

4
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 

5
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 

6
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 

7
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 

8
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

39 thoughts on “Class 5 -For Loops

  1. This assignment helped me get more comfortable using for loops, especially nested ones. Figuring out how to count down inside an outer loop was a bit confusing at first, but it made more sense after trying it out. I also learned how to convert math I already know—like plotting a line—into Python code. The sine function part was cool because it showed how useful formatting and np.deg2rad() can be. The last pattern problem was the most challenging but also the most fun. Overall, it was a good mix of review and learning new things.

  2. I know when I did the notebook homework for this class, I did struggle a little bit. I also think the four loops were used and relevant in the entire semester, especially for the fact that we could use it for our final project. I think more examples would be helpful to include so we get more practice.

  3. This was a tricky problem set, but I did really enjoy the puzzle-ish aspects of it! For loops are tricky but this was helpful. Maybe some more puzzle-ish in-class exercises with for loops could be helpful to lock the information in even more!

  4. This class was really foundational to the rest of the course! I used for loops in my final project. One thing I wish we had focused more on though was why you type something like “for i in my list” because I never really understood what “i” was supposed to be which gave me trouble with getting my for loops to loop what I wanted sometimes.

  5. I thought it was really useful to learn about for loops and definitely used them a lot throughout the semester. One thing I remember being confused about at first though, was why we would sometimes write “for i in list” and other times would use a different variable like for dept in in mystrlist, and I think that could be clarified when for loops are first introduced in the packet. Also, I think I would have liked more practice using for loops and if statements simultaneously. For the homework, I didn’t think to use an if statement to make it say I have 1 class instead of 1 classes, but I think if we had practiced using for loops and if statements together in class I might have figured that out earlier on.

  6. Since we use for loops for more basic stuff in this notebook, I think it could be useful to come back to them later in the semester when we get to more complex analysis. I got more confused as the semester went on as to how to apply them in practice.

  7. This lecture was straight forward in class but then was the first homework I found really difficult. I think the code.com work we did at the begging of the semester actually helped me with this assignment because it gave general examples of the concept of for loop statements. Loops are super useful and in this packet we also learn other functions like sum and mean that are used continuously with data analysis.

  8. Learning for loops was so fascinating as it really enabled me to think in binary or like a computer. You are tasked with simplifying the action you want the code to run within the confines of the language it understands. This was the first time I processed how smart the software is, and how I have to really learn a new language. Parts of it are intuitive but when it isn’t, it is extremely frustrating. Getting through it though is what makes the rest of the notebooks infinitely easier as you learn how to work the muscle.

  9. I believe that this class was very helpful, as it went over one of the fundamental parts of python: For Loops. I felt that this class in combination with the homework really helped to solidify my knowelege on the topic.

  10. For loops can be a difficult concept to pick up so I think it might be helpful to start the classwork with a supplemental video that might be helpful to visualize exactly what a for loop is doing. I noticed that a lot of people were struggling with similar concepts so it might also work to do a mini 5-minute lecture/explanation at the top of the class so everyone goes into the classwork with an understanding of how the syntax of the command works.

  11. I had a super hard time with for loops throughout the semester! I would spend more time fleshing this out earlier on (although by the end, they come up so often that I eventually got a handle on things.)

    One thing I think might be nice in general is for pitfalls that everyone seems to fall into, to have more lecture time breaking it down.

  12. I enjoyed this segment of the course but this homework took me the longest out of any homework assignment during the whole semester. However, in the end, understanding what a For Loop was, was very helpful!

  13. I thought this was a good lecture. Looking back, it was nice to start with very basic for loop structures because then, later, it was easier to integrate them into more complex codes. At first, I was a bit confused by the arbitrary assignment of the variable “i” (or something else) but I was understood it as I went through the problem set. Overall, this was a very helpful class for the rest of the course.

  14. I think learning a for loop is a really great skill to build upon throughout the semester of python. The coding is a little difficult at first, but it’s essential to understand as it makes the following homeworks a lot more efficient.

  15. If I am remembering correctly, this class was pretty straightforward. The code.org exercises were pretty helpful in understanding the way for loops worked. I agree with a previous comment that “k” and “i” were confusing, but I don’t think there is really a way around that initial confusion besides just practicing more for loops until you kind of get it.

  16. I found the homework for this class really difficult because. I understood the concept of for loops, but was very confused when it came to adding a parameter within a for loop especially when it came to nested for loops. Question number 1 had me spiraling for a while and turning to people who knew Python better than me for hints. In class we did deal with using i as a parameter when it came to stepping through a list, but I personally was incredibly confused when it came to doing it later. I think that including more examples like the pattern questions in the homework would be helpful for later.

  17. I think that this looping lecture was very good!!! I think that it was a great introduction to loops which we use constantly throughout the course. I also think that this homework assignment really solidified the idea.

  18. I think this was a good way of getting to know what a for loop is and how it works. I would have liked if we were also able to read in data from a csv and try to create some sort of for loop through that as well during the class packet.

  19. I found it easy to go through the packet on for loops during class time. However, when I started the homework I realized I really didn’t understand what a for loop actually is. I think it would have been helpful to talk through a real-world example (like the grocery list, but more in depth) as a class before starting. I also didn’t realize the importance of the order of code within the for loop until much later in the semester, and often had my outputs shifted incorrectly. Once I figured out how to use for loops though, they became invaluable!

  20. This lesson was initially a bit difficult for me to grasp, but in hindset, I think it’s a very useful function to know and master because of how much easier it is to handle commands in large datasets through loops. I think it would have helped to have started with an easier step by step walkthrough, but struggling through the notebook and continuing to practice over the semester did help solidify the concept in my mind.

  21. For loops are one of the most useful things to learn in data analysis with Python. You will almost always use a for loop when you are analyzing data. We learned a lot in just one class, but the workbook and the homework exercises were incredibly useful learning tools. I think that while the Code.org exercises did emphasize the need for loops, I don’t think that it was sufficient to teach for loops. Also, I think that at this point in the semester, the class should address while loops as well as functions in order to have a more robust understanding under the hood.

  22. At the time I found this assignment super difficult, but looking back I realize how far we’ve come now. This was definitely a good exercise in my patience earlier on, which was super important throughout the semester. I was still pretty confused with for loops when doing this assignment, so I feel like maybe having one other easier for loops assignment before this one would’ve been really nice.

  23. While I think For loops are an important component of computer science, the concept is really difficult to learn. I had some trouble understanding how write these loops. I think in the future, maybe dedicating two or three classes to for loops alone would be helpful. I also think white boarding would help students think about each piece of code, it would serve as a great learning tool.

  24. This lesson was one of the hardest ones for me, and I still don’t quite understand what a for-loop is. Why is it called “for loop”? And what are “k” and “i?

  25. I think this class was one of the most useful. For loops were used so often after this and I think the homework did a solid job of reinforcing the concept so it could be easily accessed later.

  26. I found this lesson very challenging. At this stage I still was not completely comfortable with for loops and I had to search online to figure out how to complete the homework. I ultimately got the correct answer but I didn’t really understand it. I think that it would have been worth it to start with some more straight forward loops before this one. Also, the instructions for #2 on the homework were unclear.

  27. For loops are really powerful but, for me, not intuitive. I would have liked more practice. Also, the Codecademy exercises were painful because, in some later units, exercises required methods they hadn’t taught.

  28. Finally I understood for loops. And the animated 24 hours of code also added to that. You might want to make the for loops a little more basic and build on them a little more gradually, since they are such a core concept in programming.

  29. Looking back on what I learned in the class, I would have to say this was one of the most useful exercises. For loops are one of the more complicated and non-intuitive things I learned in the class, and I definitely could not have completed my final project without them. I actually think that we should have spent more time on for loops, booleans, and if statements, because it seemed to me that these were the things that I would get stuck on most often.

  30. I thought it was really cool to learn about for loops – especially in conjunction with Codecademy.
    The homework was tricky, but I think it actually helped me learn for loops better because it introduced a new way to think about for loops beyond just running it through a range/data set.

  31. For loops are very useful! I used this lecture to conduct a few preliminary analyses on my senior thesis data. I found the example that printed both inside and outside the for loop to be particularly informative because I wasn’t sure what would be printed outside of the for loop once the loop was done running.
    for dept in mystrlist:
    print dept
    print dept
    The output of this code was helpful for understanding where in the list we are after/outside the for loop. I also agree with Cherie’s comment about emphasizing the “enumerate” command. At first, I was also a bit confused about what the code “sum+=i,” but it was easy enough to figure out and a helpful shortcut for future coding. I appreciated the tip about going through two lists (for i,j in zip(Months, Days)) because I wouldn’t have thought about doing so otherwise.

  32. I liked this lecture. It started out with the basics of creating a for loop in python and stressed the importance of the new syntax (indents and colons). After, the lecture briefly uses “np.arange()” but then used range() and I wasn’t sure if those were the same or could be interchanged. However, I always find it useful when the lectures show a print statement that uses the .format() so we are reminded of the syntax of how to format a string nicely. Looking back on this lecture, I see that we were shown how to use “for i, mystr in enumerate (mystrlist):” to get the specific indexing. I forgot that we had learned enumerate because it breezed past it so quickly… In the last classwork (class 13?) we needed to use indexing but I was stuck because I forgot how to do it… Maybe this syntax can be stressed a little further because it is useful to remember how to enumerate numbers for later lectures!

Leave a Reply