-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-python-refresher.py
More file actions
148 lines (69 loc) · 1.5 KB
/
01-python-refresher.py
File metadata and controls
148 lines (69 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# coding: utf-8
# # Python Refresher
# In[6]:
x = 5
# In[7]:
def good_enough(x, guess):
return abs((guess * guess) - x) < 0.00000000001
def improve_guess(x, guess):
return (guess + x/guess)/2
def sqrt(x, guess=0.0001):
if good_enough(x, guess):
return guess
else:
return sqrt(x, improve_guess(x, guess))
# In[8]:
sqrt(guess=0.1, x=36)
# In[ ]:
x = 16
if x < 5:
print("x is less than 5")
elif x < 7:
print("x is less than 7")
else:
print("x is greater than 7")
# In[ ]:
p = 0
while p < 10:
print(p)
p += 1
# In[ ]:
# In[ ]:
for i in range(10000000000000000000000000):
print(i)
# In[16]:
l = [1, 2, 4, 2, 2, 2]
l.append(4)
print(l.remove(2))
print(l)
# In[17]:
for i in l:
print("Square root of " + str(i) + ": ", end="")
print(sqrt(i))
# In[32]:
d = { 1: "one",
2: "two" }
print(d[1])
print(list(d.items()))
# In[33]:
tuple(d.items()) # immutable
# In[37]:
for k, v in d.items():
print(k, "=", v)
# In[43]:
list(d.keys())
# In[54]:
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 0, 3, 5, 5, ]
merged = list(zip(a, b))
# In[55]:
merged
# In[59]:
# Real world usecase of ZIP: combining column headers with row values
# much more, along with usecases, here: https://stackoverflow.com/questions/2429692
fields = ["id", "name", "location"]
values = ["13", "bill", "redmond"]
print(list(zip(fields, values)))
print(dict(zip(fields, values)))
# In[ ]: