Week 2

Quiz

  1. Finish this sentence "Project report section titles should be ..."

  2. What is the difference between 12/3 and 12//3?

  3. What is wrong with the following scenario. " Student A is working on project 1. Their roommate took 337 last semester, so Student A asked for help with the project. Their roommate showed Student A their project report and explained the code. Student A then wrote their own project report and turned it in (without copying their roommate's code)."

  4. Identify and correct the error.

In [1]:
x=37
if x%2 == 1
    print("It is odd")
  File "C:\Users\rahollis\AppData\Local\Temp/ipykernel_3564/3125587736.py", line 2
    if x%2 == 1
               ^
SyntaxError: invalid syntax

Answers

  1. Project report section titles should be short and descriptive.

  2. 12/3 results in the float 4.0 while 12//3 results in the integer 4.

  3. Student A was getting outside help with the project.

  4. Missing colon at the end of line 2.

Notes

While loops

In [8]:
x = 51
q = 0
while x >= 3:
    x -= 3         # This is the same as x = x-3
    q += 1
    print(x)
print(q)
48
45
42
39
36
33
30
27
24
21
18
15
12
9
6
3
0
17
In [7]:
16*3
Out[7]:
48
In [3]:
56//3
Out[3]:
18
In [9]:
x = [2,3,5,7,11,13]
for i in x:
    print(i)
2
3
5
7
11
13
In [10]:
for i in range(len(x)):
    print(x[i])
2
3
5
7
11
13

IsPrime

In [1]:
def IsPrime(num):
    is_prime = True
    for div in range(2,num):
        if num%div == 0:
            is_prime = False
            break
    return is_prime
In [3]:
IsPrime(7)
Out[3]:
True
In [ ]:
def IsPrime(num):
    for div in range(2,num**0.5 +1):
        if num%div == 0:
            return False
    return True

Importing libraries

In [4]:
import math
In [5]:
math.sqrt(5)
Out[5]:
2.23606797749979
In [7]:
math.sin(math.pi)
Out[7]:
1.2246467991473532e-16
In [8]:
import numpy as np
In [9]:
np.linspace(1,10)
Out[9]:
array([ 1.        ,  1.18367347,  1.36734694,  1.55102041,  1.73469388,
        1.91836735,  2.10204082,  2.28571429,  2.46938776,  2.65306122,
        2.83673469,  3.02040816,  3.20408163,  3.3877551 ,  3.57142857,
        3.75510204,  3.93877551,  4.12244898,  4.30612245,  4.48979592,
        4.67346939,  4.85714286,  5.04081633,  5.2244898 ,  5.40816327,
        5.59183673,  5.7755102 ,  5.95918367,  6.14285714,  6.32653061,
        6.51020408,  6.69387755,  6.87755102,  7.06122449,  7.24489796,
        7.42857143,  7.6122449 ,  7.79591837,  7.97959184,  8.16326531,
        8.34693878,  8.53061224,  8.71428571,  8.89795918,  9.08163265,
        9.26530612,  9.44897959,  9.63265306,  9.81632653, 10.        ])
In [10]:
from math import cos
In [11]:
cos(0)
Out[11]:
1.0
In [14]:
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
In [15]:
IsPrime(5682437)
Out[15]:
False

Congruence in LaTeX

$ m \equiv n (\mod k) $

In [ ]: