Types

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as st
2 + 2
4
s = "hello, world"
x = 2
type(x)
int
type(s)
str
y = 2.2
type(y)
float
b = True
type(b)
bool
type(None)
NoneType
c = 'c'
type(c)
str
s = 2
type(s)
int

Data Structures

l = [1, 2.2, "edward", True] # list
l
[1, 2.2, 'edward', True]
l.append(5)
l
[1, 2.2, 'edward', True, 5]
l[4]
5
d = {"key": 1, 1: "value", (1, 2): "wow"} # dictionary or dict
d[(1,2)]
'wow'
d["key"]
1
d["math314"] = "is cool"
d
{'key': 1, 1: 'value', (1, 2): 'wow', 'math314': 'is cool'}
t = (1, 2, 3.4)
t
(1, 2, 3.4)

Control Flow

if True:
    print("yay")
else:
    print("nay")
yay
if l:
    print("what")
what
if ():
    print("ah I get it")
for i in range(len(l)):
    print(i, l[i])
0 1
1 2.2
2 edward
3 True
4 5
d.keys()
dict_keys(['key', 1, (1, 2), 'math314'])
d.values()
dict_values([1, 'value', 'wow', 'is cool'])
d.items()
dict_items([('key', 1), (1, 'value'), ((1, 2), 'wow'), ('math314', 'is cool')])
for k, v in d.items():
    print(k, v)
key 1
1 value
(1, 2) wow
math314 is cool
c = 0
while True:
    print(c)
    if c > 10:
        break
    c += 1
0
1
2
3
4
5
6
7
8
9
10
11
c *= 2
c /= 1.5
c **= 3
c
3154.9629629629626
x = 2
x **= 2
x
4
if False:
    print("a")
elif True:
    print("b")
else:
    print("c")
b

Functions

def f(h, j = 5, k = 6):
    return h + j
f(1, j = 8)
9
f(1)
6
f(1, k = 2, j = 3)
4
f(2)
7

Class

class Table():
    def __init__(self, num_legs = 4):
        self.legs = num_legs

    def num_legs(self):
        return self.legs
t = Table()
x = t.num_legs()
s = Table(num_legs = 7)
s.num_legs()
7

Numpy

np.exp(((np.zeros(3) + 3) * 2 ) / 6)
array([2.71828183, 2.71828183, 2.71828183])
rng = np.random.default_rng(seed = 789427834294)
x = rng.normal(size = 10)
np.mean(x)
0.05685974974670359
np.std(x)
1.0161486486393296
X = rng.normal(size = (3, 5))
np.mean(X, axis = 1)
array([ 0.10255819, -0.85520091, -0.56674674])
np.std(X, axis = 1)
array([0.41042252, 0.72211726, 0.57281818])