-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSurvival_Guide_Example_Script.py
369 lines (305 loc) · 10.2 KB
/
Survival_Guide_Example_Script.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 9 15:39:07 2022
Examples and Exercises for the JACS Python Intro Session
@author: Soheb Mandhai
"""
"Importing libraries"
import numpy as np #Used for numerical calculations
import matplotlib.pyplot as plt #Useful for creating visualisations with data
import pandas as pd #Useful for processing tables and data files
# =============================================================================
# DATA Types
# =============================================================================
# =============================================================================
# NUMERICAL
# =============================================================================
"These exercises will help you learn how data types in pythons work"
"Working with Integers"
a = 100
b = 2
c = a*b
print(c)
"Working with floats"
af = 100.
bf = 3.
cf = af/bf
print(cf)
"Working with complex numbers"
ac = 100 + 2j
bc = 2 + 1.2j
cc = ac+bc
print(cc)
# =============================================================================
# Strings
# =============================================================================
"Printing out a string"
print("Hello World")
"Assigning a string"
word = 'Manchester City '
print(word)
"Extracting character by index"
print(word[4])
"You can also modify strings in Python!"
print(word.upper()) #All upper case
print(word.lower()) #All lower case
print(word.lower().capitalize()) #Capitalize the first letter
print(word.replace('M','F')) #Replace a letter
print(word.strip()) #Remove white spaces from the front or end of a word
print(word.split('e')) #Splits the word around the letter e
# =============================================================================
# Sequences
# =============================================================================
"Defining a list"
alist = [1,2,3]
print(alist)
"Remember, indexing in Python starts from 0 for the first element"
print(alist[2])
"Replacing a value in a list"
alist[2] = 4
print(alist)
"Defining a tuple"
atuple = (4,5,6)
print(atuple)
print(atuple[0])
"Defining a dictionary"
adict = {"snack":"chocolate", "amount":3, "healthy": False}
print(adict)
print(adict['snack'])
"Defining a set"
aset = {"banana","apple","oranges"}
print(aset)
# =============================================================================
# Booleans
# =============================================================================
"Assigning Booleans"
abool = False
print(abool)
"Checking if the condition holds"
if abool == False:
print("I'm not abool, you are!")
a = 1
b = 3
if a<b:
print("a is lower than b")
# =============================================================================
# End of Chapter Exercises
# =============================================================================
fruits = {'banana':3,'strawberries':4,'pineapple':1,'lemon':2}
type(4*(2+6j))
print(fruits['banana'])
#print(fruits[3]) #This will return a KeyError
food = ("toast","cereal","croissant")
# food[1] = "banana" #Will return a TypeError
# =============================================================================
# Conditions and Loops
# =============================================================================
# =============================================================================
# If, Elif, and Else statements
# =============================================================================
JACS = True #Set a boolean variable
"Boolean example of using conditions"
if JACS: #If JACS == True
#do something
print("Python JACS Session 2022...\n")
else:
print("Something else...\n")
"Numerical example of using conditions"
people = 9
if people > 10:
print("There are over 10 people...\n")
elif (people <= 10) & (people>0):
print("There are less than or equal to 10 people...\n")
else:
print("No one is here...\n")
# =============================================================================
# For Loops
# =============================================================================
"""For loops iterate over the block of code indented following the colon.
The iteration number is determined by the limiting condition.
"""
#Iterate over values 0-9 and run the code, setting i to the number
for i in range(10):
#do something
print(i)
# =============================================================================
# While
# =============================================================================
cut_off = 10 #Value to terminate at
count = 0 #Initialise a counter
#While conditions requires termination conditions
print('While loop')
while (count < 10):
print(count)
count+=1
# =============================================================================
# End of Chapter Exercise: Looping
# =============================================================================
#%%
#Q1
magic_beans = 3
if magic_beans > 3:
print("Jack got a good bargain for the cow... maybe")
elif (magic_beans==3):
print("Woah... three magic beans???")
else:
print("""
Jack's Mom: WHERE'S THE COW?
Jack: I sold it for beans and then ate some
because I got hungry.""")
#Q2
cutoff = 10
i = 0
while i < cutoff:
print(i)
i+=1
# =============================================================================
# Advanced Operations
# =============================================================================
# =============================================================================
# Functions
# =============================================================================
"A function is a block of code that can be reused without retyping lines"
#Basic designation for a function
def my_function(arg="I'm an argument"):
#You can use assignements to initialise default arguments
#Coding block here
print(arg)
"Once a function has been defined like the above, we can call it with"
my_function() #without an argument, uses default value of arg
my_function("I like chocolate")
"Simple Pythagoras' Theorem"
def pythag(a=1,b=1):
return (a**2+b**2)**(1./2)
c = pythag(3,4)
print(c) # Works
# =============================================================================
# Lambda
# =============================================================================
"Mini functions that carry out a single operation"
lambda_func = lambda x : x+10
print(lambda_func(5))
# =============================================================================
# Classes
# =============================================================================
"Classes makes use of Object-Oriented programming aspect of Python"
"These are blueprints for creating objects"
"Example"
class Animal:
def __init__(self,name,species):
self.name = name
self.species = species
cat = Animal("Floofington","Feline")
dog = Animal ("Doglas", "Canine")
print(cat.name, cat.species)
print(dog.name,dog.species)
# =============================================================================
# Generators
# =============================================================================
#%%
"Generators can create controlled iterables"
"This generator will keep increasing n by one when it is iterated over"
def my_gen():
n = 0 #Initialise counter
while n > -1:
if n%2 == 0:
print(n, " is even")
else:
print(n, " is odd")
n+=1
if n == 10:
raise StopIteration
yield n
#Assign generator to a variable
agen = my_gen()
next(agen)
"Uncomment to see how StopIteration Works"
# for item in agen:
# print(item)
# =============================================================================
# Decorators
# =============================================================================
"Decorators are a neat approach to handle nested functions"
def my_decorator(func):
"Define a wrapper to conduct operations on the passed function"
def wrapper():
f = func() #Call function
f_upper = f.upper() #Capitalises all letters in text
return f_upper #returns result to outer scope
return wrapper #returns wrapper results to global scope
"Our test function simply returns text"
def test_func(text='Hello There'):
return text
print(test_func()) #Original Text
present = my_decorator(test_func) #Decorated Function
print(present()) #Print
"Alternatively... we can apply the decorator using the following convention."
"This is the equivalent to the assignments from above"
@my_decorator
def test_func_2(text='General Kenobi!'):
return text
print(test_func_2())
# =============================================================================
# End of Chapter Exercises
# =============================================================================
#Q2
smol_func = lambda x,y: x**2 + y**2
print(smol_func(3,2))
# =============================================================================
# Scope
# =============================================================================
#%%
"Global variables are available to all functions"
x= 1
def scope():
print(x)
scope()
print(x)
"You can define the var. within a local scope with no effect on the global scope"
x= 1
def scope():
x=2
print(x)
scope()
print(x)
"Finally, you can overwrite the global var. by invoking the global keyword"
x= 1
def scope():
global x
x = 2
print(x)
scope()
print(x)
# =============================================================================
# Docstring Template
# =============================================================================
def foo(a, b, c=False): #Credit: Josh Hayes
"""
a short one-line description of the function
Parameters
----------
a : float
description of a
b : str
description of b
c : bool, optional
description of c. Default is False
Returns
-------
out : dtype of out
dexcription
Notes
-----
Anything else you'd like to say about odd behavious
"""
# do stuff
return
#Use ?foo to find out more about this function
# =============================================================================
# Excessive for loops
# =============================================================================
"Excessive looping can be avoided in most cases"
for i in range(10):
for j in range(4):
for k in range(3):
print(i*j*k)