Week 1 Notes

Markdown

To get a header, use the hashtag #. One hashtag for level 1 header, 2 for level 2, etc.

for i in range(3): Code can be put between two ticks (key above tab).

To make a bullet list, use the star *, plus +, or minus -.

  • item 1
  • item 2
    • item 2.1
    • item 2.2

For more markdown such as formatting tables, check out the markdown cheatsheet.

LaTeX

To render math in LaTeX, use the dollar sign $

3x+47=7 vs. $3x+4y=7$

There are two ways to render an equation:

  • in-line equations using one dollar sign $ 3x+4y=7 $
  • displayed equations using two dollar sign $$ 3x+4y=7 $$

Fractions using \frac{numerator}{denominator}

$$ x = \frac{-b \pm \sqrt{b^2-4ac}}{2a} $$

Square root using \sqrt{}

For more, check out the LaTeX symbols list. If you are having a hard time figuring out how to do something with LaTeX, Google it (it's what I do) or ask me.

Python Code

Basic arithmetic

In [1]:
3+4
Out[1]:
7
In [2]:
3*4
Out[2]:
12
In [5]:
3*(4+7)
Out[5]:
33
In [6]:
3**2
Out[6]:
9
In [7]:
12/3
Out[7]:
4.0

There are two types of numerical data in Python, integers int and floating point number float. Different data types are treated differently by the code.

In [8]:
type(4.0)
Out[8]:
float
In [9]:
type(4)
Out[9]:
int

You can convert between data types using specialized functions.

In [10]:
int(4.0)
Out[10]:
4
In [11]:
float(4)
Out[11]:
4.0

When it comes to division, it really matters if you are working with integers or floats.

In [12]:
12//3
Out[12]:
4
In [16]:
17/4
Out[16]:
4.25
In [17]:
17//4
Out[17]:
4
In [18]:
17%4
Out[18]:
1

Check if a number is even by calculating the remainder when you divide by 2 (modulo 2)

In [20]:
20%2
Out[20]:
0
In [21]:
19%2
Out[21]:
1

If the remainder is 0 then the number is even. This can be generalized to check if a number $ n $ is divisible by a number $ d $ by checking if n%d is zero.

Variables

Data can be stored as a variable.

In [1]:
x = 3
In [2]:
2*x**2 - 4*x - 8
Out[2]:
-2

Three rules for naming variables

  1. names must begin with a letter or underscore _
  2. names cannot have special characters other than underscores (this includes spaces)
  3. if the name turns green, don't use it (it is already being used by the system)

Not a rule but advice

  1. names should be short and descriptive
In [3]:
growth_rate = 0.045
In [ ]:
# Examples of words that can't be used for variable names
# don't try to run this cell
sum
list
int

Loops

Loops tell Python to run the same code over and over again based on a changing variable.

In [22]:
for i in range(3):
    print(i)
0
1
2
In [23]:
total = 0
for i in range(10):
    total = total + i
    print(total)
0
1
3
6
10
15
21
28
36
45
In [26]:
total = 0
for i in range(1001):
    total = total + i
print(total)
500500

Sum of even numbers only

In [4]:
total = 0
for i in range(0,1001,2):
    total = total + i
print(total)
250500

Sum of odd numbers only

In [5]:
total = 0
for i in range(1,1001,2):
    total = total + i
print(total)
250000

Lists

In [28]:
list1 = [1,3,7,18,4.92]
In [29]:
list1[3]
Out[29]:
18
In [30]:
list1[0]
Out[30]:
1
In [31]:
len(list1)
Out[31]:
5
In [32]:
list1[1:3]
Out[32]:
[3, 7]
In [33]:
list1[:3]
Out[33]:
[1, 3, 7]
In [34]:
list1[3:]
Out[34]:
[18, 4.92]
In [35]:
list1[-1]
Out[35]:
4.92
In [36]:
list1[-2]
Out[36]:
18
In [37]:
list2 = [5,7,9]
In [38]:
list1+list2
Out[38]:
[1, 3, 7, 18, 4.92, 5, 7, 9]
In [41]:
list2*3
Out[41]:
[5, 7, 9, 5, 7, 9, 5, 7, 9]
In [42]:
list1.append(86)
print(list1)
[1, 3, 7, 18, 4.92, 86]
In [43]:
list1.insert(0,38)
In [44]:
print(list1)
[38, 1, 3, 7, 18, 4.92, 86]
In [45]:
list1.pop(0)
Out[45]:
38
In [46]:
print(list1)
[1, 3, 7, 18, 4.92, 86]

Tuples

In [47]:
tup = (5,7,3,38)
In [48]:
tup[0]
Out[48]:
5
In [49]:
tup[:2]
Out[49]:
(5, 7)
In [50]:
tup[-1]
Out[50]:
38
In [51]:
tup.append(4)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13112/3077097648.py in <module>
----> 1 tup.append(4)

AttributeError: 'tuple' object has no attribute 'append'
In [52]:
tup = ()

Conditional Statements

In [3]:
4 == 4.0
Out[3]:
True
In [5]:
x=568
if x%2 == 0:
    print('It is even')
It is even
In [7]:
x < 500
Out[7]:
False
In [9]:
x >= 568
Out[9]:
True
In [13]:
x%2 == 1 and x < 500
Out[13]:
False
In [15]:
x%2 == 1 or x < 600
Out[15]:
True
In [16]:
not x < 600
Out[16]:
False
In [19]:
x=668
if x%2 == 0 and x < 600:
    print('It is an even number less than 600')
else:
    print('It is either odd or larger than 600')
It is either odd or larger than 600
In [23]:
x=667
if x%2 == 0:
    print('The number is even')
elif x < 600:
    print('The number is less than 600')
else:
    print('It is either odd and larger than 600')
It is either odd and larger than 600
In [24]:
total = 0
for i in range(0,1001,7):
    total = total + i
print(total)
71071
In [26]:
total = 0
for i in range(0,1001):
    if i%7 == 0:
        total = total + i
print(total)
71071
In [ ]:
 

Exercise Write a conditional statement that indicates True if the number 567 is divisible by 21 and False if it is not.

Exercise Modify your code so that is checks the number stored in the variable num (you may want to store something in num first).

Exercise Write code that creates a list of all of the divisors of 567.

In [27]:
def Divisors(num):
    list_of_divisors = []
    for div in range(1,num+1):
        if num%div == 0:
            list_of_divisors.append(div)
    return list_of_divisors
In [28]:
Divisors(567)
Out[28]:
[1, 3, 7, 9, 21, 27, 63, 81, 189, 567]

Exercise Write a function that returns True if a given number is prime and False if it is not.

Exercises

What is the sum of the first 100 square integers?

In [1]:
result = 0
for i in range(101):
    result += i**2
result
Out[1]:
338350

Write a loop that creates a list of 100 odd integers.

In [2]:
nums = []
for n in range(1,201,2):
    nums.append(n)
print(nums)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199]
In [3]:
len(nums)
Out[3]:
100
In [4]:
nums = []
for n in range(201):
    if n%2 == 1:
        nums.append(n)
print(nums)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199]
In [5]:
nums = []
for n in range(100):
    nums.append(2*n+1)
print(nums)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199]

Write a loop that creates a list of 100 numbers that are congruent to 2 modulo 5.

In [6]:
nums = []
for n in range(2,501,5):
    nums.append(n)
print(nums)
[2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77, 82, 87, 92, 97, 102, 107, 112, 117, 122, 127, 132, 137, 142, 147, 152, 157, 162, 167, 172, 177, 182, 187, 192, 197, 202, 207, 212, 217, 222, 227, 232, 237, 242, 247, 252, 257, 262, 267, 272, 277, 282, 287, 292, 297, 302, 307, 312, 317, 322, 327, 332, 337, 342, 347, 352, 357, 362, 367, 372, 377, 382, 387, 392, 397, 402, 407, 412, 417, 422, 427, 432, 437, 442, 447, 452, 457, 462, 467, 472, 477, 482, 487, 492, 497]
In [7]:
len(nums)
Out[7]:
100
In [8]:
nums = []
for n in range(501):
    if n%5 == 2:
        nums.append(n)
print(nums)
[2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77, 82, 87, 92, 97, 102, 107, 112, 117, 122, 127, 132, 137, 142, 147, 152, 157, 162, 167, 172, 177, 182, 187, 192, 197, 202, 207, 212, 217, 222, 227, 232, 237, 242, 247, 252, 257, 262, 267, 272, 277, 282, 287, 292, 297, 302, 307, 312, 317, 322, 327, 332, 337, 342, 347, 352, 357, 362, 367, 372, 377, 382, 387, 392, 397, 402, 407, 412, 417, 422, 427, 432, 437, 442, 447, 452, 457, 462, 467, 472, 477, 482, 487, 492, 497]

Functions

In [9]:
def sum_of_squares(n):
    result = 0
    for i in range(n+1):
        result += i**2
    return result
In [10]:
sum_of_squares(100)
Out[10]:
338350
In [12]:
sum_of_squares(357)
Out[12]:
15230215

While loops

In [13]:
x = 5280
q = 0
while x >= 13:
    x -= 13
    q +=1
q
Out[13]:
406
In [14]:
406*13
Out[14]:
5278
In [15]:
5280//13
Out[15]:
406

Write a function that takes an integer $ n $ as input and returns $ \sum_{k=0}^n \frac{1}{k} $ as output.

In [ ]:
 

Write a function that takes an integer $ n $ and input and returns a list of its divisors as output.

In [ ]:
 

Write a function that determines if an input integer $ n $ is a prime number, returning True if it is and False if its not.

In [ ]:
 

Write a function that take an integer $ k $ as input and returns a list of all the prime numbers less than or equal to $ k $ as output.

In [ ]:
 

Write a function that takes an integer $ n $ as input and returns a list containing it's prime factorization as output.

In [ ]:
 

Write a function that checks if two integers $ m $ and $ n $ are coprime, returning True if they are and False if they aren't.

In [ ]: