-
Notifications
You must be signed in to change notification settings - Fork 2
/
L1-python-basics.py
131 lines (93 loc) · 2.66 KB
/
L1-python-basics.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
# Basics: variables, if statements, for loops, lists, dictionaries
# indentation matters!
number = 5
name = 'somestring'
# print(number)
# if number < 5:
# print('less')
# else:
# print('more')
# python has two main data types: lists and dictionaries
# list: things in order
# fruits = ['apple', [3.0, 10.0], 'banana', 'coconut', 'date', 'eucalyptus']
# loop over items in a list
# for fruit in fruits:
# print(fruit)
# indexing a list
# print(fruits[0])
# length of a list
# print(len(fruits))
# to loop over numbers
# for i in range(len(fruits)):
# print(fruits[i])
# dictionary: key-value pairs
# cost = {'apple': 0.50, 'banana': 0.30}
# indexing a dictionary with a key (string)
# print(cost['apple'])
# defining functions
# def square(x):
# return x**2
# print(square(4))
# What about arrays and matrices?
# creating a numpy array (1D,2D)
# indexing
# zeros and ones functions to initialize
# vector/matrix operations (elementwise arithmetic, addition, sum, mean, etc.)
# loops/lists = bad (speed comparison)
# this is how to import a library (and name it something shorter)
# import numpy as np
# A = np.array([0, 1, 2, 3, 4])
# can we use our function from before?
# print(square(A))
# lists vs. arrays (IMPORTANT)
# a regular python list
# L = [6, 3, 4, 7, 5]
# a numpy array
# A = np.array([6, 3, 7, 4, 5])
# indexing [start:stop:step]
# defaults: start=0, stop=N+1, step=1
# print(A[2:]) # from i to end
# print(A[:2]) # from beginning to i (not inclusive)
# print(A[-1]) # negative indexing is allowed!
# print(A[::2]) # every other element (omit start and stop)
# print(A[::-1]) # reverse the array (step = -1)
# print(A[A < 5]) # logical indexing
# what's the difference?
# 'A' behaves like a matlab array
# what do you expect to happen?
# print(L*3)
# print(A*3)
# a 2d array (matrix)
# A2 = np.array([[6,3,7,4,5], [2,6,3,4,1]])
# indexing [i,j]
# print(A2[0,3])
# get a row (: means "all", just like matlab)
# print(A2[0,:])
# create a matrix of zeros
# M = np.zeros((10,5))
# M[:,:] = 4
# print(M)
# vectorized functions (IMPORTANT)
# numpy has many built-in functions to simplify matrix operations
# N = 10000000
# M = np.ones((N,5))
# use vectorized functions whenever you can,
# e.g. don't do this...
# s = 0
# for i in range(N):
# for j in range(5):
# s += M[i,j]
# print(s)
# instead do this...
# print(M.sum())
# print(np.sum(M, axis=0)) # either syntax is fine
# Matplotlib: how to make plots
# import matplotlib.pyplot as plt
# import numpy as np
# data = np.random.rand(100)
# plt.plot(data, color='red', linewidth=2)
# plt.xlabel('x value')
# plt.ylabel('Random number')
# plt.title('my title')
# plt.show()
# plt.savefig('something.pdf')