import numpy as np
np.random.seed(12345)
np.set_printoptions(precision=4, suppress=True)
import numpy as np
data = [np.random.standard_normal() for i in range(7)]
data
a = [1, 2, 3]
b = a
b
a.append(4)
b
def append_element(some_list, element):
some_list.append(element)
data = [1, 2, 3]
append_element(data, 4)
data
a = 5
type(a)
a = "foo"
type(a)
"5" + 5
a = 4.5
b = 2
# String formatting, to be visited later
print(f"a is {type(a)}, b is {type(b)}")
a / b
a = 5
isinstance(a, int)
a = 5; b = 4.5
isinstance(a, (int, float))
isinstance(b, (int, float))
a = "foo"
getattr(a, "split")
def isiterable(obj):
try:
iter(obj)
return True
except TypeError: # not iterable
return False
isiterable("a string")
isiterable([1, 2, 3])
isiterable(5)
5 - 7
12 + 21.5
5 <= 2
a = [1, 2, 3]
b = a
c = list(a)
a is b
a is not c
a == c
a = None
a is None
a_list = ["foo", 2, [4, 5]]
a_list[2] = (3, 4)
a_list
a_tuple = (3, 5, (4, 5))
a_tuple[1] = "four"
ival = 17239871
ival ** 6
fval = 7.243
fval2 = 6.78e-5
3 / 2
3 // 2
c = """
This is a longer string that
spans multiple lines
"""
c.count("\n")
a = "this is a string"
a[10] = "f"
b = a.replace("string", "longer string")
b
a
a = 5.6
s = str(a)
print(s)
s = "python"
list(s)
s[:3]
s = "12\\34"
print(s)
s = r"this\has\no\special\characters"
s
a = "this is the first half "
b = "and this is the second half"
a + b
template = "{0:.2f} {1:s} are worth US${2:d}"
template.format(88.46, "Argentine Pesos", 1)
amount = 10
rate = 88.46
currency = "Pesos"
result = f"{amount} {currency} is worth US${amount / rate}"
f"{amount} {currency} is worth US${amount / rate:.2f}"
val = "espaƱol"
val
val_utf8 = val.encode("utf-8")
val_utf8
type(val_utf8)
val_utf8.decode("utf-8")
val.encode("latin1")
val.encode("utf-16")
val.encode("utf-16le")
True and True
False or True
int(False)
int(True)
a = True
b = False
not a
not b
s = "3.14159"
fval = float(s)
type(fval)
int(fval)
bool(fval)
bool(0)
a = None
a is None
b = 5
b is not None
from datetime import datetime, date, time
dt = datetime(2011, 10, 29, 20, 30, 21)
dt.day
dt.minute
dt.date()
dt.time()
dt.strftime("%Y-%m-%d %H:%M")
datetime.strptime("20091031", "%Y%m%d")
dt_hour = dt.replace(minute=0, second=0)
dt_hour
dt
dt2 = datetime(2011, 11, 15, 22, 30)
delta = dt2 - dt
delta
type(delta)
dt
dt + delta
a = 5; b = 7
c = 8; d = 4
if a < b or c > d:
print("Made it")
4 > 3 > 2 > 1
for i in range(4):
for j in range(4):
if j > i:
break
print((i, j))
range(10)
list(range(10))
list(range(0, 20, 2))
list(range(5, 0, -1))
seq = [1, 2, 3, 4]
for i in range(len(seq)):
print(f"element {i}: {seq[i]}")
total = 0
for i in range(100_000):
# % is the modulo operator
if i % 3 == 0 or i % 5 == 0:
total += i
print(total)