Python Fundamental Review - HTNM
Python Fundamental Review - HTNM
August 9, 2021
Contents
0 INTRODUCTION TO PROGRAMMING REVIEW 4
0.1 PREFACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
0.2 CODES FOR ALL EXERCISES AND SOURCES . . . . . . . . . . . . . . . . . . . 5
1 CHAPTER 1: INTRODUCTION 5
1.1 CONSTANTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 VARIABLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 NUMERIC OPERATORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 SCIENTIFIC NOTATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 INT AND FLOAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 (INPUT) TYPE DIFFERENCES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8 (INPUT) FILE NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 CHAPTER 3: FUNCTIONS 8
3.1 FUNCTION EXAMPLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 COMMON BUILT-IN PYTHON FUNCTIONS . . . . . . . . . . . . . . . . . . . . . 9
3.3 PASS-BY-OBJECT-REFERENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.4 SCOPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.5 FUNCTIONS AS ARGUMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.6 DEFAULT PARAMETER VALUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.7 LAMBDA FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.7.1 LAMBDA EXAMPLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.7.2 TEST: WITHOUT LAMBDA . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.7.3 LAMBDA EXAMPLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.8 RECURSION EXAMPLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.8.1 FACTORIAL RECURSION IN SHORT . . . . . . . . . . . . . . . . . . . . . 12
3.8.2 FIBONACCI RECURSION IN SHORT . . . . . . . . . . . . . . . . . . . . . 12
3.8.3 (INPUT) (WARNING: WALL-OF-CODE) TOWER OF HANOI: TRADI-
TIONAL RECURSION PROBLEM . . . . . . . . . . . . . . . . . . . . . . . 13
4 CHAPTER 4: STRINGS 17
1
4.1 STRING EXAMPLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.2 len() OF A STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.3 LOOP OVER A STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.4 SLICING A STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.5 STRING FORMATTING OPERATOR % . . . . . . . . . . . . . . . . . . . . . . . . 18
4.5.1 STRING FORMATTING OPERATOR % EXAMPLE . . . . . . . . . . . . . 18
4.6 in OPERATOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.7 STRING COMPARISON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.8 THE DIRECTORY FUNCTION dir() . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.9 IMPORTANT FUNCTIONS AND METHODS . . . . . . . . . . . . . . . . . . . . . 19
4.9.1 capitalize(), center() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.9.2 endswith(), startswith() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.9.3 find() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.9.4 lstrip(), rstrip(), strip() . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.9.5 join() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.9.6 replace() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.9.7 lower(), upper() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2
5.4.1 TUPLE EXAMPLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4.2 TUPLES ARE IMMUTABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4.3 TUPLE DIRECTORIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4.4 TUPLES AND ASSIGNMENT . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4.5 TUPLE AS RETURN VALUES . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4.6 LISTS AND TUPLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.4.7 DICTIONARIES AND TUPLES . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.4.8 TUPLE COMPARISON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.4.9 SORTING EXAMPLE: SORT BY VALUE, NOT KEY . . . . . . . . . . . . 34
6 CHAPTER 6: MODULES 34
6.1 import EXAMPLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.2 (preparation) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.2.1 (preparation) mount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.2.2 (preparation) append path . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.3 MODULES AS SCRIPTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.4 __name__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.5 sys.argv[] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.6 USEFUL MODULES: BASIC EXAMPLES . . . . . . . . . . . . . . . . . . . . . . . 36
6.7 pickle MODULE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.7.1 IMPORT pickle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.7.2 dump() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.7.3 load() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7 CHAPTER 7: FILES 37
7.1 OPENING A FILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.2 LOOP OVER A FILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.3 READ WHOLE FILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.4 with BLOCK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.5 WRITING FILES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.6 CURRENT POSITION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.7 CHANGE POSITION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3
10 APPENDIX: SOME IMPORTANT MODULES FOR MATHEMATICS, DATA
SCIENCE AND MACHINE LEARNING 45
10.1 random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
10.1.1 INTEGERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
10.1.2 SEQUENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
10.1.3 REAL NUMBERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
10.2 time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
10.3 math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
10.3.1 CONSTANTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
10.3.2 NUMBER-THEORETIC AND REPRESENTATION FUNCTIONS . . . . . 48
10.3.3 POWER AND LOGARITHMIC FUNCTIONS . . . . . . . . . . . . . . . . . 49
10.3.4 TRIGONOMETRIC FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . 50
10.3.5 ANGULAR CONVERSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
10.4 numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
10.4.1 ARRAY CREATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
10.4.2 INDEXING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
10.4.3 BROADCASTING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
10.4.4 STRUCTURED ARRAYS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
10.5 MOST USED MODULES FOR MATHEMATICS, DATA SCIENCE AND MA-
CHINE LEARNING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4
0 INTRODUCTION TO PROGRAMMING REVIEW
Written by Hoang Tran Nhat Minh,
Hanoi University of Science and Technology,
Data Science and Artificial Intelligence - K65.
Guided by Dr. Dinh Viet Sang.
0.1 PREFACE
Thank you, my teacher, Dr. Dinh Viet Sang, I cannot learn programming in Python
that much without you. This notebook uses a huge part your lectures as a guidance.
Dear Data Science & AI - K65,
This is my last notebook in order to prepare for the final exam on Introduction to Programming.
If you find this notebook helpful, thanks for reading it. Some might find this one not that helpful,
I highly appreciate every line of text you read, including this one.
If you find any mistake, feedback and I will try to fix it as soon as possible.
Thank y’all for supporting me all the time, good luck on all of your exams.
Best wishes,
Hoang Tran Nhat Minh.
1 CHAPTER 1: INTRODUCTION
1.1 CONSTANTS
[1]: # CONSTANTS
print('Hello World')
print(12.3)
Hello World
12.3
5
1.2 VARIABLES
[2]: # VARIABLES
x = 13.4
y = 4.3
x = 'Hello'
print(x)
print(y)
Hello
4.3
279.5084971874737
3.8
3
4
123.4
123.4
0.001234
6.3
1.0
8
6
1.6 TYPES
[6]: # TYPES
print(3 + 6)
print('hello' + ' there')
print(type(3.5))
print(type('?'))
print(float(3))
print(str(4) + str(5))
print(int('8'))
9
hello there
<class 'float'>
<class 'str'>
3.0
45
8
a = 8
88888
40
Name = /content/sample_data/README.md
This directory includes a few sample datasets to get you started.
7
* `mnist_*.csv` is a small sample of the
[MNIST database](https://en.wikipedia.org/wiki/MNIST_database), which is
described at: http://yann.lecun.com/exdb/mnist/
False
True
True
False
True
False
True
8
print('larger')
elif a == b:
print('equal')
else:
print('less')
a = 3
b = 5
less
3 CHAPTER 3: FUNCTIONS
3.1 FUNCTION EXAMPLE
[12]: # FUNCTION EXAMPLE
# name parameter(s)
def two_time(number):
# docstring
'''
2 times a number
'''
# body
doubled = 2 * number
# returns (if not, it is a void function)
return doubled
# function call, pass argument(s) to parameter(s)
print(two_time(8))
print(two_time.__doc__)
16
2 times a number
8
3
9
3.3 PASS-BY-OBJECT-REFERENCE
[14]: # PASS-BY-OBJECT-REFERENCE
# Immutable objects: int, float, complex, string, tuple, frozen set, bytes
# Mutable objects: list, dict, set, byte array
def change_num(num):
num += 1
a = 4
print(a)
change_num(a)
print(a)
def change_lst(lst):
lst.append('changed')
l = ['original']
print(l)
change_lst(l)
print(l)
4
4
['original']
['original', 'changed']
3.4 SCOPE
[15]: # SCOPE
def f(x):
x += 1
print('In f: x =',x)
x = 0
print('First: x =', x)
f(x)
print('After f: x =', x)
First: x = 0
In f: x = 1
After f: x = 0
10
return f(x) + g(y)
79
79
print(tong(1))
# 1 + 2 + 3
print(tong(1, 4))
# 1 + 4 + 3
print(tong(0, 3, 6))
# 0 + 3 + 6
6
8
9
8
18
double = multiply_by(2)
# multiply_by(2) -> lambda x: x * 2
# -> a doubling function
triple = multiply_by(3)
print(double(16))
print(triple(4))
11
32
12
double = multiply_by(2)
# multiply_by(2) -> function: multi, where multi return the doubled argument
# -> a doubling function
triple = multiply_by(3)
print(double(16))
print(triple(4))
32
12
12
def factorial(n):
if n == 0:
print('0! = 1')
return 1
pr(n)
return factorial(n - 1) * n
print(factorial(7))
12
7! = 6! * 7
6! = 5! * 6
5! = 4! * 5
4! = 3! * 4
3! = 2! * 3
2! = 1! * 2
1! = 0! * 1
0! = 1
5040
5040
move(start, end)
print_board()
else:
transfer(n - 1, start, mid, end)
transfer(1, start, end, mid)
transfer(n - 1, mid, end, start)
count = 0
transfer(n, 0, 2, 1)
return count
13
'''
a =
0 1 2 3
0 4 3 2 1
1 0 0 0 0
2 0 0 0 0
printed :
A B C
1 . .
2 . .
3 . .
4 . .
'''
def print_board():
'''print current board'''
print('| A B C |')
a[row_start][col_lastnonenull(row_start)] = 0
# MAIN:
14
# input n
n = int(input('n = '))
0 4 3 2 1
1 0 0 0 0
2 0 0 0 0
'''
a = [[n - i for i in range(n)]]
for i in range(2):
a.append([0 for j in range(n)])
# run, print the board every move and return the result
print('\nNumber of transfers: %i' % hanoi_tower(n))
n = 4
| A B C |
| 1 . . |
| 2 . . |
| 3 . . |
| 4 . . |
Step 1: A -> B
| A B C |
| . . . |
| 2 . . |
| 3 . . |
| 4 1 . |
Step 2: A -> C
| A B C |
| . . . |
| . . . |
| 3 . . |
| 4 1 2 |
Step 3: B -> C
| A B C |
| . . . |
| . . . |
15
| 3 . 1 |
| 4 . 2 |
Step 4: A -> B
| A B C |
| . . . |
| . . . |
| . . 1 |
| 4 3 2 |
Step 5: C -> A
| A B C |
| . . . |
| . . . |
| 1 . . |
| 4 3 2 |
Step 6: C -> B
| A B C |
| . . . |
| . . . |
| 1 2 . |
| 4 3 . |
Step 7: A -> B
| A B C |
| . . . |
| . 1 . |
| . 2 . |
| 4 3 . |
Step 8: A -> C
| A B C |
| . . . |
| . 1 . |
| . 2 . |
| . 3 4 |
Step 9: B -> C
| A B C |
| . . . |
| . . . |
| . 2 1 |
| . 3 4 |
Step 10: B -> A
| A B C |
| . . . |
| . . . |
| . . 1 |
| 2 3 4 |
Step 11: C -> A
| A B C |
| . . . |
| . . . |
16
| 1 . . |
| 2 3 4 |
Step 12: B -> C
| A B C |
| . . . |
| . . . |
| 1 . 3 |
| 2 . 4 |
Step 13: A -> B
| A B C |
| . . . |
| . . . |
| . . 3 |
| 2 1 4 |
Step 14: A -> C
| A B C |
| . . . |
| . . 2 |
| . . 3 |
| . 1 4 |
Step 15: B -> C
| A B C |
| . . 1 |
| . . 2 |
| . . 3 |
| . . 4 |
Number of transfers: 15
4 CHAPTER 4: STRINGS
4.1 STRING EXAMPLES
[26]: # STRING EXAMPLES
print('abc')
abc
16
17
4.3 LOOP OVER A STRING
[28]: # LOOP OVER A STRING
for c in 'hi there?':
print(c)
h
i
t
h
e
r
e
?
c
npq
hello
color = 'yellow'
num = 7
print( '%s is a color of %d main colors in a rainbow!' % (color, num) )
gold is a metal
yellow is a color of 7 main colors in a rainbow!
3.90
18
4.6 in OPERATOR
[32]: # in OPERATOR
print('e' in 'hello')
True
False
False
True
Hello
hello
*******hello********
19
print('hello'.startswith('h'))
print('hello'.startswith('e', 1, 4))
# 01234
# search from 1 to 3 = 'ell'
True
False
True
True
4.9.3 find()
[37]: # find()
print('hello'.find('l'))
print('hello'.find('l', 3, 5))
# 01234
2
3
"hello "
"__ hello __***** "
" hello"
"***__ hello __***** "
"hello"
"__ hello __***** "
20
4.9.5 join()
[39]: # join()
wrdlst = ['hello', 'from', 'the', 'other', 'side']
print('_'.join(wrdlst))
hello_from_the_other_side
side-other-the-from-hello
4.9.6 replace()
[40]: # replace()
print('hello, long time no see'.replace('l', '*'))
print('hello, long time no see'.replace('l', '*', 2))
# only the 2 first ones are replaced
hello
HELLO
21
a
8
bc
d
[15, 6]
5
5
0 1 2 3 4 5 6 7
3 4 5 6 7
3 6
8 6 4
22
5.1.7 LIST SLICING
[48]: # LIST SLICING
print(lst)
print(lst[:3])
print(lst[-2])
__subclasshook__
append
clear
copy
count
extend
index
insert
pop
remove
reverse
sort
append()
[50]: # append()
new_lst = lst[:]
new_lst.append('appended string')
print(new_lst)
in OPERATOR
[51]: # in OPERATOR
print(8 in lst)
print(9 in lst)
print('^_^' in lst)
print('^_^' not in lst)
True
False
True
False
23
max(), min(), sum()
[52]: # max(), min(), sum()
new_lst = [6, 69, 9]
print(max(new_lst))
print(min(new_lst))
print(sum(new_lst))
69
6
84
sort(), sorted()
[53]: # sort(), sorted()
new_lst = [6, 69, 9]
new_lst = sorted(new_lst)
print(new_lst)
new_lst.sort(reverse = True, key = lambda x: x % 8)
'''
6 % 8 = 6
69 % 8 = 5
9 % 8 = 1
'''
print(new_lst)
[6, 9, 69]
[6, 69, 9]
del new_lst[4]
print(new_lst)
new_lst.pop()
print(new_lst)
new_lst.remove('bc')
print(new_lst)
reverse()
24
[55]: # reverse()
new_lst = lst[:]
print(new_lst)
new_lst.reverse()
print(new_lst)
['t', 'h', 'i', 's', ' ', 'o', 'n', 'e', '!', ' ', 'i', 's', ' ', 'a', ' ', 's',
't', 'r', 'i', 'n', 'g']
['this', 'one!', 'is', 'a', 'string']
['this one', ' is a string']
hi-there-?
5.1.10 ALIASES
[57]: # ALIASES
lst_a = [1, 3, 4]
lst_b = lst_a
lst_b.append('?')
print(lst_a)
[1, 3, 4, '?']
25
[1, 4]
def wrong_delete_last(a_lst):
a_lst = a_lst[:-1]
print('List in wrong one:', a_lst)
# this one doesn't, a_lst now becomes a local variable
new_lst = [1, 3, 6, 9]
delete_last(new_lst)
print(new_lst)
new_lst = [1, 3, 6, 9]
wrong_delete_last(new_lst)
print(new_lst)
[1, 3, 6]
List in wrong one: [1, 3, 6]
[1, 3, 6, 9]
reduce()
[61]: # reduce()
from functools import reduce
new_lst = [1, 3, 6, 1]
print(reduce(lambda x, y: x + y, new_lst))
print(reduce(lambda x, y: x + y, new_lst, 3000))
# initializer
11
3011
26
text = 'The word "deep" in "deep learning" refers to the number of layers␣
,→through which the data is transformed. More precisely, deep learning systems␣
,→have a substantial credit assignment path (CAP) depth. The CAP is the chain␣
,→connections between input and output. For a feedforward neural network, the␣
,→depth of the CAPs is that of the network and is the number of hidden layers␣
,→plus one (as the output layer is also parameterized). For recurrent neural␣
,→networks, in which a signal may propagate through a layer more than once,␣
,→threshold of depth divides shallow learning from deep learning, but most␣
,→researchers agree that deep learning involves CAP depth higher than 2. CAP␣
,→of depth 2 has been shown to be a universal approximator in the sense that␣
,→it can emulate any function.[15] Beyond that, more layers do not add to the␣
,→function approximator ability of the network. Deep models (CAP > 2) are able␣
,→to extract better features than shallow models and hence, extra layers help␣
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0]
15
LIST COMPREHENSION
[63]: # LIST COMPREHENSION
print([c for c in 'hello there'], '\n')
['h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e']
['The', 'the', 'the', 'The', 'the', 'the', 'the', 'the', 'the', 'the', 'the',
'the', 'the', 'the', 'the']
15
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0]
5.2 SETS
5.2.1 SET EXAMPLES
[64]: # SET EXAMPLES
print({3, 4, 8})
print(set('hello'))
{8, 3, 4}
{'l', 'o', 'h', 'e'}
{0, 1}
{0, 1, 2, 3, 4, 5}
{2, 3}
{0, 1, 4, 5}
5.3 DICTIONARIES
5.3.1 DICTIONARY EXAMPLES
[67]: # DICTIONARY EXAMPLES
d = {8: 'b', '?': 'a'}
print(d)
28
print(d[8])
print(d['?'])
d['!?'] = 'mnpq'
print(d)
5.3.2 in OPERATOR
[68]: # in OPERATOR
d = {8: 'b', '?': 'a'}
print(d)
print('?' in d)
print('b' in d)
29
1, 'deep': 3, 'learning': 4, 'systems': 1, 'have': 1, 'a': 5, 'substantial': 1,
'credit': 1, 'assignment': 1, 'path': 1, '(CAP)': 1, 'depth.': 1, 'CAP': 4,
'chain': 1, 'transformations': 1, 'from': 2, 'input': 2, 'output.': 2, 'CAPs':
2, 'describe': 1, 'potentially': 2, 'causal': 1, 'connections': 1, 'between': 1,
'and': 3, 'For': 2, 'feedforward': 1, 'neural': 2, 'network,': 1, 'depth': 5,
'that': 3, 'network': 1, 'hidden': 1, 'plus': 1, 'one': 1, '(as': 1, 'output':
1, 'layer': 2, 'also': 1, 'parameterized).': 1, 'recurrent': 1, 'networks,': 1,
'signal': 1, 'may': 1, 'propagate': 1, 'more': 2, 'than': 3, 'once,': 1,
'unlimited.[2]': 1, 'No': 1, 'universally': 1, 'agreed-upon': 1, 'threshold': 1,
'divides': 1, 'shallow': 2, 'learning,': 1, 'but': 1, 'most': 1, 'researchers':
1, 'agree': 1, 'involves': 1, 'higher': 1, '2.': 1, '2': 1, 'has': 1, 'been': 1,
'shown': 1, 'be': 1, 'universal': 1, 'approximator': 2, 'sense': 1, 'it': 1,
'can': 1, 'emulate': 1, 'any': 1, 'function.[15]': 1, 'Beyond': 1, 'that,': 1,
'do': 1, 'not': 1, 'add': 1, 'function': 1, 'ability': 1, 'network.': 1, 'Deep':
1, 'models': 2, '(CAP': 1, '>': 1, '2)': 1, 'are': 1, 'able': 1, 'extract': 1,
'better': 1, 'features': 2, 'hence,': 1, 'extra': 1, 'help': 1, 'effectively.':
1}
8 b
? a
! b
10 15
8 b
? a
30
! b
10 15
377
res = {0: 1}
def lis_including(i):
if i in res:
return res[i]
max_lis = 1
for j in range(i):
if num_list[j] < num_list[i]:
max_lis = max(max_lis, 1 + lis_including(j))
res[i] = max_lis
return max_lis
i num_list[i] lis_including(i)
0 96 1
1 9 1
2 69 2
3 50 2
31
4 23 2
5 89 3
6 31 3
7 64 4
8 11 2
9 99 5
10 79 5
11 98 6
12 18 3
13 35 4
14 55 5
Result = 6
5.4 TUPLES
5.4.1 TUPLE EXAMPLES
[77]: # TUPLE EXAMPLES
print((3, 5, 7))
print((1, ))
# Warning: Tuple of 1 element must have a comma
print((1))
(3, 5, 7)
(1,)
1
(3, 4, 7)
32
print('x = %i\ny = %i' % (x, y))
a = 3
b = 5
x = 6
y = 9
def max_and_index(lst):
index = 0
for i in range(1, 15):
if lst[i] > lst[index]:
index = i
return lst[index], index
(3, 4)
[46, 23, 33, 7, 14, 96, 42, 48, 37, 31, 85, 1, 21, 28, 32]
(96, 5)
enumerate()
33
[83]: # enumerate()
for ind, c in enumerate('cdef'):
print(ind, c)
0 c
1 d
2 e
3 f
True
True
False
True
False
34
Sort by key: ('h', 4) ('m', 7) ('n', 6) ('t', 5)
Sort by value, #1 way: ('h', 4) ('t', 5) ('n', 6) ('m', 7)
Sort by value, #2 way: ('h', 4) ('t', 5) ('n', 6) ('m', 7)
6 CHAPTER 6: MODULES
6.1 import EXAMPLE
[87]: # import EXAMPLE
import math
print(math.sqrt(8))
from math import sqrt
print(sqrt(8))
from random import *
print(randint(3,6))
2.8284271247461903
2.8284271247461903
5
6.2 (preparation)
6.2.1 (preparation) mount
Mounted at /content/gdrive
'/content/gdrive/MyDrive/Colab Notebooks/hello.py'
if __name__ == '__main__':
print('Hello?')
print('Script in "hello.py":\n---------------------------------')
f = open(hello_path, 'r')
35
print(f.read(), '\n---------------------------------')
f.close()
Script in "hello.py":
---------------------------------
if __name__ == '__main__':
print('Hello?')
---------------------------------
Hello?
6.4 __name__
[91]: # __name__
print('before import')
import hello
print('after import')
print(hello.__name__)
before import
after import
hello
6.5 sys.argv[]
[92]: # sys.argv[]
sum_path = '/content/gdrive/MyDrive/Colab Notebooks/sumof2module.py'
print('Script in "sumof2module.py":\n---------------------------------')
f = open(sum_path, 'r')
print(f.read(), '\n---------------------------------')
f.close()
! python '/content/gdrive/MyDrive/Colab Notebooks/sumof2module.py' 8 9
Script in "sumof2module.py":
---------------------------------
if __name__ == '__main__':
import sys
print(int(sys.argv[1]) + int(sys.argv[2]))
---------------------------------
17
36
https://docs.python.org/3/library/datetime.html
https://docs.python.org/3/library/math.html
https://numpy.org/doc/
'''
import random
import datetime
import math
import numpy
print(datetime.datetime.now())
print(math.factorial(8))
2021-08-09 05:48:01.231481
40320
[1.79077237 1.05060928 1.15447232 2.275137 1.86980938 1.18032189]
[1.79077237 2.275137 1.86980938]
6.7.2 dump()
[95]: # dump()
sample_pkl_path = '/content/gdrive/MyDrive/Colab Notebooks/sample_pkl.pkl'
lst = [3, 5, 0, 8]
with open(sample_pkl_path, 'wb') as f:
# 'wb' = write byte, see more below, in Chapter 7: FILES
pkl.dump(lst, f)
6.7.3 load()
[96]: # load()
with open(sample_pkl_path, 'rb') as f:
# 'rb' = read byte, see more below, in Chapter 7: FILES
loaded_content = pkl.load(f)
print(loaded_content)
[3, 5, 0, 8]
37
7 CHAPTER 7: FILES
7.1 OPENING A FILE
[97]: # OPENING A FILE
f = open(hello_path, 'r')
# 'r' reading | 'w' writing | 'a' appending | 'r+' both reading and writing
if __name__ == '__main__':
print('Hello?')
if __name__ == '__main__':
print('Hello?')
if __name__ == '__main__':
print('Hello?')
True
38
7.6 CURRENT POSITION
[102]: # CURRENT POSITION
with open(write_path, 'r') as f:
print(f.tell())
print(f.read())
print(f.tell())
0
Write this line to the file
27
0
13
ne to the file
27
39
def __str__(self):
return 'Complex number (%s + %si)' % (self.real, self.imaginary)
40
8.1.1 USING THE OBJECT EXAMPLES
[105]: # USING THE OBJECT EXAMPLES
z1 = Complex_number(6, 9)
print(z1.real, z1.imaginary)
print(z1.modulus())
print(z1)
6 9
10.816653826391969
Complex number (6 + 9i)
2.8 9.2
9.616652224137045
Complex number (2.8 + 9.2i)
False
True
8.2 SUBCLASS
[109]: # SUBCLASS
# You can re-define any method of Complex_number, or
# add new methods, new attributes in Imaginary_number
class Imaginary_number(Complex_number):
def __init__(self, *args):
if len(args) == 1:
self.removed_real_part = None
imaginary_part = args[0]
else: # len(args) == 2
41
self.removed_real_part = args[0]
imaginary_part = args[1]
# 2 ways to initialize:
# super().__init__(0, imaginary_part)
Complex_number.__init__(self, 0, imaginary_part)
def __str__(self):
return 'Imaginary number (%si)' % (self.imaginary)
9.2 EXCEPTIONS
9.2.1 COMMON BUILT-IN EXCEPTIONS
[112]: # COMMON BUILT-IN EXCEPTIONS
lst = [0, 1, 2]
print(lst[4])
IndexError: list index out of range
42
print(int(lst))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
print(an_undefined_a_variable)
NameError: name 'an_undefined_a_variable' is not defined
print(a
print(a
^
SyntaxError: unexpected EOF while parsing
print(lst.mnpq)
AttributeError: 'list' object has no attribute 'mnpq'
print(6 / 0)
ZeroDivisionError: division by zero
print(int('?'))
ValueError: invalid literal for int() with base 10: '?'
f = open('????')
FileNotFoundError: [Errno 2] No such file or directory: '????'
else: # Only execute if the try clause does not raise any exception
print(res)
a = 4
43
b = 8
0.5
Done
a = sfgopsgspg
Unable to convert to number
Done
a = 4
b = 0
Unable to divide by 0
Done
a = 4
b = 8
0.5
Done
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-116-d1990690242c> in <module>()
----> 1 raise ValueError('This is a value exception')
def num_args_check(date):
dmy_lst = date.split('-')
if len(dmy_lst) == 3:
return True
return False
def numeric_check(date):
dmy_lst = date.split('-')
for arg in dmy_lst:
if not arg.isnumeric():
44
return False
return True
def args_len_check(date):
dmy_lst = date.split('-')
len_lst = [len(dmy_lst[i]) for i in range(3)]
if tuple(len_lst) == (2, 2, 4):
return True
return False
if not num_args_check(date):
raise DateFormatError(
'There must be 3 arguments for day-month-year, respectively')
if not numeric_check(date):
raise DateFormatError(
'At least one of 3 arguments is not numeric')
if not args_len_check(date):
raise DateFormatError(
'len() of 3 arguments must be 2, 2, 4, respectively')
Date = 66-99-6969
The date is: 66-99-6969
Date = 41-14-6161-3
DateFormatError: There must be 3 arguments for day-month-year, respectively
Date = 1-12-4251
DateFormatError: len() of 3 arguments must be 2, 2, 4, respectively
Date = 30-12-200x
DateFormatError: At least one of 3 arguments is not numeric
Date = 66-99-6969
The date is: 66-99-6969
45
10 APPENDIX: SOME IMPORTANT MODULES FOR MATH-
EMATICS, DATA SCIENCE AND MACHINE LEARNING
10.1 random
[115]: # random
# Documentation: https://docs.python.org/3/library/random.html
import random
10.1.1 INTEGERS
[116]: # INTEGERS
print(random.randrange(10))
# a random number in range(10)
print(random.randrange(22, 29, 2))
# a random number in range(22, 29, 2), which are 22, 24, 26 and 28
1
22
10.1.2 SEQUENCE
[117]: # SEQUENCE
seq = [3, 5, 0, 8]
print(random.choice(seq))
# choose a random element in a sequence
print(random.choices(seq, k = 2))
# choose a random element twice in a sequence
5
[8, 5]
[118]: # SEQUENCE
print(random.sample(seq, 2))
# choose two random elements in a sequence
print(random.sample(seq, len(seq)))
# alias of random.shuffle, since it is...
# Deprecated since version 3.9, will be removed in version 3.11
[5, 0]
[3, 0, 8, 5]
46
# the end-point might or might not be included,
# read more in documentation
0.15902495606438138
3.504131258751076
10.2 time
[120]: # time
# Documentation: https://docs.python.org/3/library/time.html
import time
[121]: # time
print('before')
time.sleep(0.5)
# suspend execution for the given number of seconds
print('after')
before
after
[122]: # time
print(time.time())
# Return the time in seconds since the epoch as a floating point number
1628488096.9642045
[123]: # time
# measure execution time
time_before = time.time()
print(time_before)
for i in range(5):
print(i, end = ' ')
time_after = time.time()
print('\n%s' % time_after)
print(time_after - time_before)
1628488096.9813864
0 1 2 3 4
1628488096.9855316
0.004145145416259766
47
10.3 math
[124]: # math
# Documentation: https://docs.python.org/3/library/math.html
import math
10.3.1 CONSTANTS
[125]: # CONSTANTS
print(math.pi)
print(math.e)
print(math.inf)
print(math.nan)
3.141592653589793
2.718281828459045
inf
nan
4
8
0.30000000000000004
False
[128]: True
48
print()
print(math.isinf(999999999999999))
print(math.isinf(math.inf))
print(math.isinf(math.nan))
# nan is not infinite
print()
print(math.isnan(999999999999999))
print(math.isnan(math.inf))
print(math.isnan(math.nan))
True
False
False
False
True
False
False
False
True
3
3
20.085536923187668
20.085536923187664
print(math.log(5, 2))
# logarithm of 5 to the base 2
49
print(math.log2(5))
# more accurate
print()
print(math.log10(101))
1.0986122886681098
2.321928094887362
2.321928094887362
2.0043213737826426
print(math.sqrt(5.6))
print(5.6 ** 0.5)
# more accurate
40.84101000000001
40.84101000000001
2.3664319132398464
2.3664319132398464
print(math.acos(1.0))
# arc cosine
print(math.asin(1.0))
print(math.atan(1.0))
1.0
6.123233995736766e-17
1.633123935319537e+16
0.0
1.5707963267948966
50
0.7853981633974483
90.0
1.5707963267948966
10.4 numpy
[136]: # numpy
# Documentation: https://numpy.org/doc/
import numpy as np
[1 4 6 9]
[[1 3]
[6 9]]
[[3.2 2.1]
[6. 1.3]]
[6. 9. 3. 0.]
[[3 2]
[6 1]]
51
# arrays with a specified number of elements,
# and spaced equally between the specified beginning and end values
[0 1 2 3 4 5 6 7 8 9]
[21.2 18.7 16.2 13.7 11.2 8.7]
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[[3 0 0]
[0 4 0]
[0 0 5]]
[[0. 0. 0.]
[0. 0. 0.]]
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
10.4.2 INDEXING
[141]: # INDEXING
np_arr = np.arange(3.3, 12, 1.1)
print(np_arr)
print(np_arr[-2])
print(np_arr[-2:])
# slicing np array is exactly the same as slicing python list
52
[ 9.9 11. ]
[142]: # INDEXING
print(np_arr.shape)
np_arr.shape = (2, 4)
print(np_arr.shape)
print(np_arr)
(8,)
(2, 4)
[[ 3.3 4.4 5.5 6.6]
[ 7.7 8.8 9.9 11. ]]
[143]: # INDEXING
print(np_arr[1])
print(np_arr[1, 2])
[144]: # INDEXING
print(np.arange(4))
np_arr_2 = np.arange(1, 8, 2)
print(np_arr_2)
print()
[0 1 2 3]
[1 3 5 7]
[ 1 -1 3]
[3 7 7]
[[1 0]
[3 2]]
[[3 1]
[7 5]]
53
[145]: # INDEXING
np_arr_3 = np.arange(9)
print(np_arr_3)
index_arr_3 = np_arr_3 % 2 == 0
print(index_arr_3)
print(np_arr_3[index_arr_3])
# in data science, filtering data is very important,
# so this is a must-have tool
[0 1 2 3 4 5 6 7 8]
[ True False True False True False True False True]
[0 2 4 6 8]
10.4.3 BROADCASTING
[146]: # BROADCASTING
# the term broadcasting describes how np treats arrays
# with different shapes during arithmetic operations
np_arr_a = np.array([6, 8, 9])
print(np_arr_a)
np_arr_b = np.ones(3, dtype = np.int8)
print(np_arr_b)
print(np_arr_a + np_arr_b)
[6 8 9]
[1 1 1]
[ 7 9 10]
[147]: # BROADCASTING
print(np_arr_a + 1)
# read more about general broadcasting rules for
# more-dimensional-arrays in the documentation
[ 7 9 10]
54
10.5 MOST USED MODULES FOR MATHEMATICS, DATA SCIENCE AND
MACHINE LEARNING
• TensorFlow: https://www.tensorflow.org/
• SciPy: https://www.scipy.org/
• Matplotlib: https://matplotlib.org/
• Pandas: https://pandas.pydata.org/
• Keras: https://keras.io/
• scikit-learn: https://scikit-learn.org/
• statsmodels: https://www.statsmodels.org/
• Plotly: https://plotly.com/
• Seaborn: https://seaborn.pydata.org/
55