#!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np np.random.seed(12345) np.set_printoptions(precision=4, suppress=True) # In[2]: import numpy as np data = [np.random.standard_normal() for i in range(7)] data # In[3]: a = [1, 2, 3] # In[4]: b = a b # In[5]: a.append(4) b # In[6]: def append_element(some_list, element): some_list.append(element) # In[7]: data = [1, 2, 3] append_element(data, 4) data # In[8]: a = 5 type(a) a = "foo" type(a) # In[9]: "5" + 5 # In[10]: a = 4.5 b = 2 # String formatting, to be visited later print(f"a is {type(a)}, b is {type(b)}") a / b # In[11]: a = 5 isinstance(a, int) # In[12]: a = 5; b = 4.5 isinstance(a, (int, float)) isinstance(b, (int, float)) # In[13]: a = "foo" # In[14]: getattr(a, "split") # In[15]: def isiterable(obj): try: iter(obj) return True except TypeError: # not iterable return False # In[16]: isiterable("a string") isiterable([1, 2, 3]) isiterable(5) # In[17]: 5 - 7 12 + 21.5 5 <= 2 # In[18]: a = [1, 2, 3] b = a c = list(a) a is b a is not c # In[19]: a == c # In[20]: a = None a is None # In[21]: a_list = ["foo", 2, [4, 5]] a_list[2] = (3, 4) a_list # In[22]: a_tuple = (3, 5, (4, 5)) a_tuple[1] = "four" # In[23]: ival = 17239871 ival ** 6 # In[24]: fval = 7.243 fval2 = 6.78e-5 # In[25]: 3 / 2 # In[26]: 3 // 2 # In[27]: c = """ This is a longer string that spans multiple lines """ # In[28]: c.count("\n") # In[29]: a = "this is a string" a[10] = "f" # In[30]: b = a.replace("string", "longer string") b # In[31]: a # In[32]: a = 5.6 s = str(a) print(s) # In[33]: s = "python" list(s) s[:3] # In[34]: s = "12\\34" print(s) # In[35]: s = r"this\has\no\special\characters" s # In[36]: a = "this is the first half " b = "and this is the second half" a + b # In[37]: template = "{0:.2f} {1:s} are worth US${2:d}" # In[38]: template.format(88.46, "Argentine Pesos", 1) # In[39]: amount = 10 rate = 88.46 currency = "Pesos" result = f"{amount} {currency} is worth US${amount / rate}" # In[40]: f"{amount} {currency} is worth US${amount / rate:.2f}" # In[41]: val = "espaƱol" val # In[42]: val_utf8 = val.encode("utf-8") val_utf8 type(val_utf8) # In[43]: val_utf8.decode("utf-8") # In[44]: val.encode("latin1") val.encode("utf-16") val.encode("utf-16le") # In[45]: True and True False or True # In[46]: int(False) int(True) # In[47]: a = True b = False not a not b # In[48]: s = "3.14159" fval = float(s) type(fval) int(fval) bool(fval) bool(0) # In[49]: a = None a is None b = 5 b is not None # In[50]: from datetime import datetime, date, time dt = datetime(2011, 10, 29, 20, 30, 21) dt.day dt.minute # In[51]: dt.date() dt.time() # In[52]: dt.strftime("%Y-%m-%d %H:%M") # In[53]: datetime.strptime("20091031", "%Y%m%d") # In[54]: dt_hour = dt.replace(minute=0, second=0) dt_hour # In[55]: dt # In[56]: dt2 = datetime(2011, 11, 15, 22, 30) delta = dt2 - dt delta type(delta) # In[57]: dt dt + delta # In[58]: a = 5; b = 7 c = 8; d = 4 if a < b or c > d: print("Made it") # In[59]: 4 > 3 > 2 > 1 # In[60]: for i in range(4): for j in range(4): if j > i: break print((i, j)) # In[61]: range(10) list(range(10)) # In[62]: list(range(0, 20, 2)) list(range(5, 0, -1)) # In[63]: seq = [1, 2, 3, 4] for i in range(len(seq)): print(f"element {i}: {seq[i]}") # In[64]: total = 0 for i in range(100_000): # % is the modulo operator if i % 3 == 0 or i % 5 == 0: total += i print(total) # In[65]: