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 [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 [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 [12]:
a = 5; b = 4.5 isinstance(a, (int, float)) isinstance(b, (int, float))
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 [18]:
a = [1, 2, 3] b = a c = list(a) a is b a is not c
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 [27]:
c = """ This is a longer string that spans multiple lines """
In [29]:
a = "this is a string" a[10] = "f"
In [30]:
b = a.replace("string", "longer string") b
In [32]:
a = 5.6 s = str(a) print(s)
In [33]:
s = "python" list(s) s[:3]
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 [42]:
val_utf8 = val.encode("utf-8") val_utf8 type(val_utf8)
In [44]:
val.encode("latin1") val.encode("utf-16") val.encode("utf-16le")
In [45]:
True and True False or 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 [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 [56]:
dt2 = datetime(2011, 11, 15, 22, 30) delta = dt2 - dt delta type(delta)
In [58]:
a = 5; b = 7 c = 8; d = 4 if a < b or c > d: print("Made it")
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)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4