-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheatSheet.py
157 lines (125 loc) · 2.58 KB
/
cheatSheet.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
## Assignment
# Strings
data = 'hello world'
print(data[0])
print(len(data))
print(data)
# Numbers
value = 123.1
print(value)
value = 10
print(value)
# Boolean
a = True
b = False
print(a, b)
# Multiple Assignment
a, b, c = 1, 2, 3
print(a, b, c)
# No Value
a = None
print(a)
## Flow Control
# If and Else
value = 99
if value >= 99:
print('That is fast')
elif value > 200:
print('That is too fast')
else:
print('That that is safe')
# For-Loop
for i in range(10):
print(i)
# While-Loop
i = 0
while i < 10:
print(i)
i += 1
## Data Structures
# Tuple
a = (1, 2, 3)
print(a)
# List
mylist = [1, 2, 3]
print("Zeroth Value: %d" % mylist[0])
mylist.append(4)
print("List Length: %d" % len(mylist))
for value in mylist:
print(value)
# Dictionary
mydict = {'a': 1, 'b': 2, 'c': 3}
print("A value: %d" % mydict['a'])
mydict['a'] = 11
print("A value: %d" % mydict['a'])
print("Keys: %s" % mydict.keys())
print("Values: %s" % mydict.values())
for key in mydict.keys():
print(mydict[key])
## Functions
def mysum(x, y):
return x + y
# Test Function
print(mysum(1, 3))
## NumPy
# Create Array
import numpy
mylist = [1, 2, 3]
myarray = numpy.array(mylist)
print(myarray)
print(myarray.shape)
# Access Data
import numpy
mylist = [[1, 2, 3], [3, 4, 5]]
myarray = numpy.array(mylist)
print(myarray)
print(myarray.shape)
print("First row: %s" % myarray[0])
print("Last row: %s" % myarray[-1])
print("Specific row and col: %s" % myarray[0, 2])
print("Whole col: %s" % myarray[:, 2])
# Arithmetic
import numpy
myarray1 = numpy.array([2, 2, 2])
myarray2 = numpy.array([3, 3, 3])
print("Addition: %s" % (myarray1 + myarray2))
print("Multiplication: %s" % (myarray1 * myarray2))
## Matplotlib
# Basic Line Plot
import matplotlib.pyplot as plt
import numpy
myarray = numpy.array([1, 2, 3])
plt.plot(myarray)
plt.xlabel('test x axis')
plt.ylabel('test y axis')
plt.show()
# Basic Scatter Plot
import matplotlib.pyplot as plt
import numpy
x = numpy.array([1, 2, 3])
y = numpy.array([2, 4, 6])
plt.scatter(x,y)
plt.xlabel('test x axis')
plt.ylabel('test y axis')
plt.show()
## Pandas
# Series
import numpy
import pandas
myarray = numpy.array([1, 2, 3])
rownames = ['a', 'b', 'c']
myseries = pandas.Series(myarray, index=rownames)
print(myseries)
# Access Data
print(myseries[0])
print(myseries['a'])
# Dataframe
import numpy
import pandas
myarray = numpy.array([[1, 2, 3], [4, 5, 6]])
rownames = ['a', 'b']
colnames = ['one', 'two', 'three']
mydataframe = pandas.DataFrame(myarray, index=rownames, columns=colnames)
print(mydataframe)
print("one column: %s" % mydataframe['one'])
print("one column: %s" % mydataframe.one)