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