In [ ]:
import math

Week 3

Quiz

  1. What is the difference between a list and a tuple?

  2. Given a list my_list, how can you access the last item in the list?

  3. Why does the while look below go on forever?

In [ ]:
n=0
while n < 20:
    print(n)
  1. Identify and correct the error.
In [8]:
import math
sin(0)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12764/2097557198.py in <module>
      1 import math
----> 2 sin(0)

NameError: name 'sin' is not defined

Answers:

  1. Lists are mutable while tuples are immutable.
In [23]:
my_list = [3,76,2,395,492,49]
In [24]:
my_list[-1]
Out[24]:
49
In [25]:
my_list.reverse()
In [26]:
my_list[0]
Out[26]:
49
In [28]:
my_list.pop(len(my_list)-1)
Out[28]:
3
In [29]:
my_list
Out[29]:
[49, 492, 395, 2, 76]
In [30]:
n=0
while n < 20:
    print(n)
    n += 1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
In [31]:
math.sin(0)
Out[31]:
0.0

Notes

Code comments

Comments can be added to code using the hashtag #

Python ignores any and all characters after the #

Criteria for code comments

  1. describing what the line of code does
  2. be specific
  3. sort and to the point
In [ ]:
def Divisors(num):                       # Define divisors function with positive integer input
    list_of_divisors = []                # Define a list variable to store the divisors
    for div in range(1,num+1):           # loop through all possible divisors
        if num%div == 0:                 # check if div is a divisor
            list_of_divisors.append(div) # if it is, add it to the list of divisors
    return list_of_divisors              # return the list of divisors as output

For long comments or multi-line comments use triple quotes.

In [ ]:
"""This is a long comment that can be
entered onto a new line and continued
for as long as you would like"""
def IsPrime(num):                               # define function IsPrime with integer input num
    for div in range(2,int(math.sqrt(num)) +1): # loop through possible divisors of num, up to the square root
        if num%div == 0:                        # check if the loop variable is a divisor
            return False                        # if it is, then num is not prime and False is returned
    return True                                 # if we get through the loop, then no divisors and num is prime

Project Report

Your target audience is just an average person. You should always be asking yourself, "What am I trying to tell the reader?"

Introduction

  1. Background info and motivations
    • mathematical definitions
    • any other background from other fields
    • Why should the reader care?
    • Are there any applications?
  2. Goals
    • What do you want the reader to take away from this project
  3. Plan
    • How are you going to go about achieving the goals?
    • What are the steps you are going to take to get the reader to the final goal?

Narrative and Code

  1. Results section

Conclusion

  1. Remind reader of the goals
  2. Review the procedure
  3. Summarize the results

References

Everything that did not come out of your own gray matter needs to be cited in the text with a full reference listed here.

Graphing using matplotlib.pyplot

In [32]:
import matplotlib.pyplot as plt
In [34]:
horiz = [n for n in range(11)]
horiz
Out[34]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [37]:
# This is the same as the above
horiz = []
for n in range(11):
    horiz.append(n)
horiz
Out[37]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [35]:
vert = [h**2 - 3*h+7 for h in horiz]
vert
Out[35]:
[7, 5, 5, 7, 11, 17, 25, 35, 47, 61, 77]
In [38]:
vert = []
for h in horiz:
    vert.append(h**2 - 3*h+7)
vert
Out[38]:
[7, 5, 5, 7, 11, 17, 25, 35, 47, 61, 77]
In [50]:
plt.plot(horiz,vert,'r-.')
plt.show()
In [59]:
x = [i for i in range(20)]
y = [i**3 - 7*i**2 + 32 for i in x]
plt.plot(x,y,'go')
plt.title('Graph of $ y= x^3-7x^2+32 $')
plt.xlabel('$x$ axis')
plt.ylabel('$y$ axis')
plt.xlim([-1,16])
plt.ylim([-100,2000])
plt.show()
In [60]:
import math
In [61]:
x_vals = [math.pi*n/25 - 2*math.pi for n in range(101)]
y_vals = [math.cos(x) for x in x_vals]
In [66]:
plt.plot(x_vals,y_vals)
plt.title('Graph of $ y = \cos(x) $ on $ [-2\pi,2\pi] $')
plt.xlabel('$x$ values in radians')
plt.ylabel('$y$ values')
xticklocs = [math.pi*n/2 - 2*math.pi for n in range(9)]
xticklabs = ['$-2\pi$','$\\frac{-3\pi}{2}$','$-\pi$','$\\frac{-\pi}{2}$','0',
             '$\\frac{\pi}{2}$','$\pi$','$\\frac{3\pi}{2}$','$2\pi$']
plt.xticks(xticklocs,xticklabs)
plt.show()
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

Project 2

In [ ]: