Class 5 -For Loops

Class Overview

Today we will work on two things.

  1.  We are going to solve the equation of a line and plot it (pdf=equationofline).  You will work together in your breakout room to do it by hand.  Then each person will implement it themselves.  Here is my answer. (pdf=equationofline-answer)
  2. Then we are going to work on For loops (pdf=for-loops).  We will first just talk about looping in class and then work through a notebook.  Here is some reading on for loops.  http://www.tutorialspoint.com/python/python_for_loop.htm

VIDEO

Quiz is on courseworks for the video!

Homework 4 Due Monday February 5, 2024

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.

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 that you used in class.  Remember that we used this data when we were learning about lists and learning how to make plots (here is the link list today and plot it).   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 I have 0 Classes
On Monday I have 2 Classes
On Tuesday I have 0 Classes
On Wednesday I have 2 Classes
On Thursday I have 0 Classes
On Friday I have 0 Classes
On Saturday I have 0 Classes

In total I have 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

 

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

28 thoughts on “Class 5 -For Loops

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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