-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python Intermediate Concepts.py
344 lines (277 loc) · 7.25 KB
/
Python Intermediate Concepts.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# %% [markdown]
# # Intermediate python
#
# * Conditionals
# * If statements
# * Else-if chains
# * Loops
# * Iterables like range() and list()
# * List comprehension
# * Functions
# * Classes & Methods
# %% [markdown]
# ## Conditionals
# %%
# if statements
if True:
# do something if condition is true
pass
else:
# do something else instead
pass
# %%
# if statement based on a random or other calculation
import random
randBool = random.choice([True, False])
if randBool:
print(f"randBool is true")
pass
else:
print(f"randBool is false")
pass
# %%
# if - else chain
randInt = random.choice([None, 0, 1, 2])
if randInt == 0:
print("randInt is zero")
pass
elif randInt == 1:
print("randInt is one")
pass
elif randInt == 2:
print("randInt is two")
pass
else: #default
print(f"randInt = {randInt}")
pass
# %%
# compound conditionals
import random
randBool = random.choice([True, False])
randInt = random.choice([None, 0, 1])
if (randInt == 0) or (randBool):
print("randInt is zero OR randBool is true")
pass
elif (randInt == 1) and (not randBool):
print("randInt is one AND randBool is false")
pass
else: #default
print(f"randInt = {randInt}, randBool = {randBool}")
pass
# %% [markdown]
# ## Loops
#
# %%
# loops with indexes
print(f"range (12) = {range (12)}")
# example of what range(int) returns
for i in range(12):
print(f"i = {i}")
pass
# %%
import numpy as np
l = np.random.rand(12)
print(f"l = \n{l}")
# %%
# example using range(length(list))
for i in range(len(l)):
print(f"l[{i}] = {l[i]}")
pass
# %%
# loop through items in a iterable
for float_val in l: # comparable to C# foreach loops
print(f"float_val = {float_val}")
# %%
# loop to create a 2D array
arr = []
for i in range(3):
arrJ = []
for j in range (3):
arrJ.append(np.random.rand())
arr.append(arrJ)
print(f"arr = \n{arr}")
# %%
# List comprehension 1D
arrListComp = [i for i in range(3)]
print(f"arrListComp = \n{arrListComp}")
# %%
# List comprehension 2D
arrListComp2D = [[j+i for j in range(3)] for i in range(3)]
print(f"arrListComp = \n{arrListComp2D}")
# %%
# List comprehension 2D with function
arrListComp2D = [[np.random.rand() for j in range(3)] for i in range(3)]
print(f"arrListComp2D = \n{arrListComp2D}")
# %% [markdown]
# ## Functions
# %% [markdown]
# ### Basic functions with one argument
# %%
# define funcName (args)
def sqrt(num):
import math
root = math.pow(num, 1/2)
return root
pass
print(f"sqrt(9) = {sqrt(9)}")
# %% [markdown]
# ### Handling unexpected types
# %%
sqrt("Hello World!") # this will throw an error
# %%
# handle unexpected data types
def sqrt(num):
import math
root = None
try:
root = math.pow(num, 1/2)
except:
print(f"'{num}' is not a real number -- data type = {type(num)}")
pass
return root
pass
print(f"sqrt(9) = {sqrt(9)}")
sqrt("Hello World!") # this will no longer throw an error
# %%
import math
def percentAsString(num):
assert (type(num) == int) or (type(num) == float)
perc = num * 100
perc_string = f"{perc:,.1f}%"
return perc_string
[percentAsString(x) for x in range(1,11)]
# %%
# assert trigering example
[percentAsString(char) for char in "Hello World!"] # will throw an error
# %%
# define a fuction by name (parameters go in prentheses = "default values")
def fncName (parameter1 = "foo", parameter2 = "bar", etc = "baz"):
print(f"parameter1 = {parameter1}")
print(f"parameter2 = {parameter2}")
print(f"etc = {etc}")
fncName(1, 2, 3)
fncName(1, 2)
fncName()
# %%
# example of passing a named parameter
fncName(etc = 3)
# %% [markdown]
# ### *args
# %%
# positional arguments (*args) example
def fncNameWithArgs(*args):
for arg in args:
if type(arg) == str:
print(f"arg = '{arg}'")
pass
elif type(arg) == int:
print(f"arg = {arg}")
pass
else:
print(f"arg of unkown type = {type(arg)}, value = {arg}")
pass
pass
return args
pass
fncNameWithArgs("1", [2, 4, 6], 3)
# %%
# *args does not catch keword arguments
fncNameWithArgs(etc = 5)
# %% [markdown]
# ### **kwargs
# %%
# keword arguments (**kwargs) example
def fncNameWithKwargs(**kwargs):
for key, value in kwargs.items():
if type(value) == str:
print(f"{key} = '{value}'")
pass
elif type(value) == int:
print(f"{key} = {value}")
pass
else:
print(f"kwarg of unkown type: {key} = {value}")
pass
pass
return kwargs
pass
fncNameWithKwargs(kwarg1 = "1", kwarg2 = 2, kwarg3 = [1,3,5])
# %%
# **kwargs does NOT capture positional arguments, however
fncNameWithKwargs(3, kwarg1 = "1", kwarg2 = 2, kwarg3 = [1,3,5])
# %%
# In order to make fully robust functions,
# you have to capture *args AND **kwargs
def fncNameWithArgsAndKwargs(*args, **kwargs):
for arg in args:
if type(arg) == str:
print(f"arg = '{arg}'")
pass
elif type(arg) == int:
print(f"arg = {arg}")
pass
else:
print(f"arg of unkown type = {arg}")
pass
pass
for key, value in kwargs.items():
if type(value) == str:
print(f"{key} = '{value}'")
pass
elif type(value) == int:
print(f"{key} = {value}")
pass
else:
print(f"kwarg of unkown type: {key} = {value}")
pass
pass
return (args, kwargs)
pass
fncNameWithArgsAndKwargs(3, kwarg1 = "1", kwarg2 = 2, kwarg3 = [1,3,5])
# %%
# Floor division (Integer division) review
print(f"1/4 = {1/4}")
print(f"1//4 = {1//4}")
print(f"5/4 = {5/4}")
print(f"5//4 = {5//4}")
print(f"11/4 = {11/4}")
print(f"11//4 = {11//4}")
# %%
def Q1(L): # def defines a function, Q1 is the name of the fnc, () accepts arguments
q1_i = len(L)//4
list_sorted = np.sort(L)
q1 = list_sorted[q1_i]
print(f"list = {L}")
print(f"list_sorted = {list_sorted}")
print(f"Q1 of list = {q1}")
return q1
data = np.random.rand(12)
Q1(data)
# %% [markdown]
# ## Classes & Methods
# %%
# 'class' declares a class, 'FinancialPortfolio' is its name
class FinancialPortfolio:
def __init__(self, name = "", cash = 0, bonds = 0, stocks = 0, alternatives = 0):
print(f"FinancialPortfolio class instance created = {self}")
self.name = name
self.cash = cash
self.bonds = bonds
self.stocks = stocks
self.alts = alternatives
return
def Print(self):
print(f'{self.name}\'s Portfolio :: printing.')
print(f' cash = ${self.cash:,.2f}')
print(f' bonds = ${self.bonds:,.2f}')
print(f' stocks = ${self.stocks:,.2f}')
print(f' alts = ${self.alts:,.2f}')
def PrintNetWorth(self):
net = self.cash + self.bonds + self.stocks + self.alts
print(f'{self.name}\'s Portfolio net worth = ${net:,.2f}')
pass
# create an instance of the class
portfolio = FinancialPortfolio("Anisha", -10, 1000, 3000000, 15)
# call some of its functions
portfolio.Print()
portfolio.PrintNetWorth()