PythonIntro.py (Source)

  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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
"""
Created on Sun Mar 12 15:39:22 2017

@author: Vincent Gregoire

Prepared by [Vincent Grégoire](http://www.vincentgregoire.com), 
Department of Finance, The University of Melbourne. 

This is a sample code to illustrate some basic features of the Python language.
This notebook was created as supplemental material to a Python for 
financial research bootcamp for finance honours and PhD students at 
the University of Melbourne in March of 2017.

Last update: March 24, 2017.

**Contact**: <vincent.gregoire@unimelb.edu.au>

Latest version: <http://www.vincentgregoire.com/python-bootcamp/>
"""


# This is a comment!

"""
This is a longer comment
that can span multiple
lines.
Actually this is a string which is not
assigned to anything, so it is code,
but it does nothing.
"""


# %%
# It is usual to always begin by saying Hello World!
print "Hello World!"


# %% This is a neat comment trick that split blocks of code in Spyder
# To execute the code in the block, just type shift-enter.


# I prefer to use parenthesis, but they are not required for `print`.
print('Hello World!')

# Python is case sensitive, so the statement below is different from
# the one above, and would not work as PRINT() is not defined.
# PRINT('HELLO WORLD!')

# %% Number and operators

print(2 + 2)

print(4 - 1)

print(5 * 2)

print(5.5 * 2)

print(11.0 / 2)

print(11 / 2)

# Python 3 converts automatically to floating point if required
# Not no in Python 2, but we can activate this feature for compatibility.
from __future__ import division

print(11 / 2)

print(2**8)

print(round(22.34))

print(8//3)  # Integer division
print(8%3)  # Modulo (remainder of integer division)



# %% Variables

a = 2

b = 4

a * b

c = 'Hello'

c

# This doesn't work
# a + c

d = "World"

c + ' ' + d + '!'


# %% Operators

2 == 2

2 < 2

2 <= 2

True | False # OR

True & False # AND

True != False # XOR

not True # NOT

# %% None

# In Python there is a special value for null objects that is called None.
# Note that you first need to define a variable as None before you can
# access it.

a = None
b = 1

a is None

b is None

b is not None

# %% Strings

s = 'Hello World!'

len(s)

s[1]
s[0]
s[-1]
s[-2]

s[2:5]

s.upper()

s.lower()

'   Hello   '.strip()


'2'.zfill(3)
'20'.zfill(3)

s.replace('World', 'Moon')
s.replace('o', 'a')

s.find('d')
s[10]

# s + 2 # Error

s + str(2)

s * 2


s.split(' ')

s.startswith('Hell')

s.count('l')

print('Don\'t forget to escape the quotes.')

#%% Input

x = raw_input('Type something: ')


#%% Input exercise

# Exercise: write a program that asks the user's name and responds with
# a personalised greating.

x = raw_input('What is you name? ')

print('Good day to you, ' + x + '.')


# %% Sequences


# List

l = [2, 4, 'blabla', 4]
l[0]
l[2:4]

l[0] = 3

# Delete one element
del l[1]

l.append(3)

l.reverse()

l.sort()
l.pop()

l + l

# Tuple (immutable list)

t1 = 2, 3
t2 = (2, 3)

34 not in t1

3 in t1


# Set (unordered list of unique elements)
{3, 4, 4, 5, 4}

# Dictionnary
d = {1: 2, 'a': 'Hello'}
d['a']

d['b'] = 'World!'


# Special note: copying lists (and most other objects)
# This does not create a new list, both l and l2 refer to the
# same list.

l2 = l

l2.reverse()

l

# To create a new copy, you need to deepcopy
import copy

l3 = copy.deepcopy(l)


# %% Identity

# For base types (numbers, strings), 'is' and == behave the same way.
2 is 2
2 == 2
'aa' is 'aa'
'aa' == 'aa'

# For other objects (including list), 'is' checks if the two operands refer
# to the same object, while == compares the content.

[1, 2, 3] is [1, 2, 3]
[1, 2, 3] == [1, 2, 3]


# %% Loops and branches (Flow control)

# Notice the indentation! This is how Python identifies
# blocks of codes. PEP8 recommends 4 spaces (as long as a tab,
# but actual spaces.) Spyder will do this automatically.

# Branches
if 1 > 2:
    print('Yes')
elif 2 > 2:
    print('No')
else:
    print('I don\'t know')

# Repeat for each element in the list. (note that 'l' need not be a list, it
# can be anything that is iterable, but for now you can think list!)
for x in l:
    print(x)
    
range(5)

for i in range(5):
    print(i)

for i, x in enumerate(l):
    print( str(i) + ', ' + str(x) )
    
# Interating on dictionnaries is iterating on keys.
for x in d:
    print(str(x) + ': ' + str(d[x]))
    

# Repeat something until a condition is met.
sum = 0

while sum < 10:
    sum = sum + 2
    print(sum)
    
# break and continue
# When iterating, you can stop the loop completely using break,
# or just skip the current iteration using continue.
for i in range(10):
    if i == 3:
        continue
    elif i == 6:
        break
    print(i)


# %% List comprehension

# We saw above how to assign a list statically. Another way is to assign
# actively from another list 

fillings = ['Apple', 'Cherry', 'Sugar', 'Meat']

pies = [x + ' pie' for x in fillings]

# Same idea with dictionnaries

swapped_cases = {x: x.swapcase() for x in fillings}

# Can have conditions
desserts = [x + ' pie' for x in fillings if x != 'Meat']

# Or multiple loops

desserts = [x + ' pie' + y for x in fillings if x != 'Meat' 
            for y in ('', ' with ice cream')]
# You can have as many loops as you want, but best practice is to limit to
# only 2 loops in list comprehensions.

# %% Loading packages

# Python is a pretty powerful language, but most of the benefits come from
# the buil-in functionality is somewhat limited. Most advanced features are
# available through packages, either as part of the Python Standard Library
# or from third-parties.

# To load a package and make it available, you have to import it.
import sys

# You can list the components using the dir() method (or in the console,
# typing os. and then tab to show avaialble options.)
dir(sys)

sys.path
sys.getrecursionlimit()

# It's common to rename packages with the 'as' keyword to make access easier.
import pandas as pd

# If you want to import all the components of a pakcage, you can use *
from datetime import *

# But it's usually best to only import what you need
from datetime import datetime, timedelta

# You can also load only a subpackage if that's all you need.
import statsmodels.formula.api as sm


# %% Functions


def square(x):
    return x * x

square(2)

# Variables defined inside a function are only available
# inside that function


def computeBigSum(a, b, c, d):
        e = a + b
        f = c + d
        print(str(e))  # This works here
        return e, f
    
sum1, sum2 = computeBigSum(1, 2, 4, 5)

# But not here.
# print(str(e))
# You have to use a name defined in this scope.
print(str(sum1))


# You can make some arguments optional by assigning default values.
# Note that all required arguments need to be listed before the
# optional ones
def raise_power(n, power=2):
    return n**power

print(raise_power(4))
print(raise_power(4, 2))
print(raise_power(4, 3))

# %% Recursive function

# A function can call itself, we call this a recursive
# function. Just make sure you have a condition to stop
# the recursion (so that it doesn't run forever).

# A Fibonacci number is part of a sequence were every number is the
# sum of the previous two numbers in the sequence. The first two numbers
# of the sequence are defined to be 0 and 1.
# https://en.wikipedia.org/wiki/Fibonacci_number

def fibonacci_recursive(n):
    if n in [0, 1]:
        return n
    return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

fibonacci_recursive(10)

# %% Iterative function

# However, recursion is not always the best solution. In this case, for
# large numbers (say 50+), even a modern computer will take some time.

# Exercise: fund and code the better way! (Google search allowed!)

# A better way is the iterative way

def fibonacci_iter(n):
    if n in [0, 1]:
        return n
    a = 0
    b = 1
    c = a + b
    for i in range(2, n):
        a = b
        b = c
        c = a + b
    return c

fibonacci_iter(10)

[fibonacci_iter(x) for x in range(10)]
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

# %% Exceptions

# Sometimes things go wrong, and the code crashes. But it doesn't have to.
# You can catch exceptions, and decide what to do.


def inverse(n):
    return 1.0 / n

inverse(0)

def inverse_ex(n):
    out = None
    try:
        out = 1.0 / n
    except ZeroDivisionError as e:
        print(e[0])
        out = 0.0
    finally:
        return out
    

inverse_ex(0)    

# Anywhere in you code, you can raise your own exceptions
raise Exception('I just raised an exception!')


# %% Classes

# Python is an object-oriented programming
# language, which means that most of the datatypes are classes, and the
# actual instances of those datatypes are called objects.

# You can create your classes if you want to (probably won't need to at first)
class Rectangle:
    def __init__(self, length=1, width=1):
        self.length = length
        self.width = width
        
    def __repr__(self):
        return str(self.length) + 'x' + str(self.width) + ' rectangle'

    def getArea(self):
        return self.length * self.width

    def setLength(self, x):
        self.length = x

    def setWidth(self, x):
        self.width = x
    
    def getLength(self):
        return self.length
    
    def getWidth(self):
        return self.width
    
rect = Rectangle(3, 4)

print(rect)

rect.getArea()