-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path2x-basicPython.py
More file actions
266 lines (214 loc) · 9.22 KB
/
2x-basicPython.py
File metadata and controls
266 lines (214 loc) · 9.22 KB
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
# python is an interpreted language
# we use version 3: python3
# The character "#" indicates a comment,
# up to the end of the line
# First, we import an external module
# which has functionality written
# by someone else ...
# The module name is numpy
# we use it here with the local name np
# It is used for various mathematical operations
import numpy as np
#####################################
# you can type the instructions directly in the python shell
# where you see the output.
# make sure you type only in the line with the leading ">>>"
#####################################
#define and print a variable
# integer number
a = 3
print("Variable a is: ",a,", type is ",type(a))
# A float number:
# A float (rationale Zahl) can have digits after
# the decimal point while an integer doesn't (ganze zahl)
aa = 3.14159
print("Variable aa is: ",aa,", type is ",type(aa))
# text (string)
b = "zkm"
print("Variable b is: ",b,", type is ",type(b))
# a list (vector, array) of numbers
# a list of elements enclosed in [ ]
c = [4,3,2,1]
print("Variable c is: ",c,", type is ",type(c))
# individual elements of a list
# can be address with an index
# all indices start from 0, by default
print("First element of variable c is: ",c[0],", type is ",type(c[0]))
print("Second element of variable c is: ",c[1],", type is ",type(c[1]))
# we can use another variable as index
d = 3
print("Element ",d," of variable c is: ",c[d],", type is ",type(c[d]))
# if we use an invalid index, python will be unhappy
#print("Element 5 of variable c is: ",c[5])
# it's better to comment this code out by placing a "#"
# at the start of the line
# we can iterate of all elements in a list in different ways
# with a for loop:
for i in range(0,4):
print("Element ",i,": ",c[i])
# Please note three things here:
# 1) the range 0,4 seems to covers 5 elements,
# but the last one is NOT included
# 2) the line with the condition (control statement) ends with ":"
# 3) the following line is indented
# This holds in general for all control structures in python
# Alternatively, we can use the "in" operator
for cc in c:
print("Element is : ",cc)
# Again, we have the ":" and the indentation
# This time, we don't have an explicit index
# but we don't need to check the index range any more
# less errors!
######################################
# input options
# let's see how can we enter the data into the program
print("Example demonstating keyboard input\n")
a1 = input("Raw input, text or number\n")
print("type: ",type(a1),", value: ",a1)
a2 = eval(input("Evaluated input, text, number, list or dict. Text enclosed in \"\"\n"))
print("type: ",type(a2),", value: ",a2)
a3,b1 = eval(input("Evaluated input, 2 entries as before, separated by ,\n"))
print("A - type: ",type(a3),", value: ",a3)
print("B - type: ",type(b1),", value: ",b1)
a4 = eval(input("Evaluated input, 2 entries as before, separated by ,\n"))
if "tuple" in str(type(a4)) or "list" in str(type(a4)):
for aa in a4:
print("type: ",type(aa),", value: ",aa)
else:
print("type: ",type(a4),", value: ",a4)
################################################
# by now, we have used some variables of different and a couple of functions
# Functions are pieces of code written elsewhere, which do something usefull
# like printing values or waiting for input
# The functions we've used so for are print(), input(), type(), str() and eval()
# all functions have ()-brackets and we put the values or objects between
# the brakcets, the function sould work upon. We often call these things
# in brackets "function parameters"
# Many function don't only take parameters as inputs, they also return
# values, very obviously seen with the "input()" function
# Many functions are written by other people and are available
# in libraries or modules (naming depends on language)
# we can define out own functions too, like so:
# def <name>(<params>):
def printStr(s="",n=1):
""" A comment string with triple \" as the first line of a function definition
is called docstring. It is useed to comment on the function's function.
The docstring can be read by the member __doc__, in this case: printStr.__doc__
Our function will accept 2 paramters c and n and will print the string s for n times
We initialize the paramters with and empty sting \"\" and 1, so if a parameter
is left out, the default value will be used
"""
# we have to indent all instructions following the function definition
# we use a new kind of loop, the while loop
# which is executed while the condition is true
while n > 0:
print(s,end="") # normally, print terminates with a newline. here we don't
n -= 1
# that's it
print("\n\nAbout functions ...")
# we print the docstring:
print("\ndocstring: ",printStr.__doc__)
# let's call out function
s,n = eval(input("Which string and how often"))
printStr(s,n)
print("\n")
################################################
# back to operation ...
print("Let's go back to the data ....\n")
# Now, let's take the list and convert it into a numpy array
# All functions imported from the numpy module
# have to be prefixed with np.
# So here we use the function array()
# with c as the parameter
print("When working with larger amounts of data \
we need additional functions, e.g. for operations \
on multiple items at the same time.\n\
Numpy (numerical python) is a library which \
provides a lot of such functions.\n\
However, we need to get the numbers into numpy types\n\
For example, we can turn a list into an numpy array")
nc = np.array(c)
print("Variable nc (numpy) is: ",nc)
# This looks quite the same so far
# but we can also change the data type, to float, for example
nf = np.array(c,"float")
print("Variable nf (numpy) is: ",nf)
# there are a few more differences to lists, e.g on multiplication
print("Multiply the list by 2 give a list with twice as many items: \n",2*c)
print("Multiply the array by 2 multiplies the elements: ",2*nc)
print("With numpy arrays we have to use the append function to get longer arrays")
print(np.append(nc,nc))
print("We can also repeat each item in an array with np.repeat")
print(np.repeat(nc,2))
# we can also turn the text into a numpy array
print("We can also convert strings into numeric arrays")
# fromstring is simple but has some issues on unicode strings
# because some characters use more bytes than others
nt = np.fromstring(b,"uint8")
# a more correct way is to convert to a byte array, then to a buffer
nt = np.frombuffer(bytearray(b,"utf-8"),"uint8")
print("Variable nt from ",b," as unisgned integer array (uint) is: ",nt)
###########################################
# so far, we've dealt with individual items and lists and their np eqivalent, arrays
# a single list turns into an array with a single dimentsion
# we can also have more dimensions, for example two
# a 2D array is widley know as a table
# lets create an 2 d array with 3 rows and 4 columns, with type integer
n2d = np.empty((3,4),dtype=np.int16)
print("The array has 3 rows and 4 columns, but empty() \ndoes not set any data, so the values are random\n",n2d)
# we can extract specific cells with 2 indices and/or ranges
print("item 2 and 3 of the second row can be accessed like so:\n, n2d[1,1:3]\n",n2d[1,1:3])
print("Remember, the last index value is not included")
# the dimensionality of an array are called a "shape"
print("shape of n2d:", n2d.shape)
# a simple example for of a table is a weekly schedule like the following
wd = np.zeros((4,7),dtype=np.uint8) # this time, we use a 0 initialized 2d array
# columns go from Monday to Sunday
# rows are daily activities like sleep, eat, work, fun
# we put some numbers ....
wd[0,:] = [8,8,8,8,8,8,8] # 8 hours of sleep (sigh)
wd[1,:] = [2,2,2,2,2,3,3] #
wd[2,:] = [8,8,8,8,8,0,0] # so much work ...
wd[3,:] = [6,6,6,6,6,15,15] # so much fun
print("Our week schedule looks like so:\n", wd)
###########################################
# to show is graphically, we use the standard plotting library
# matplotlib with the local name plt
import matplotlib.pyplot as plt
# create a plotting figure with 2 areas, vertically spaced
# this is the first one
axs = plt.subplot(2,1,1)
collabel=("MO","TU","WE","TH","FR","SA","SU") # this is a list but with round bracket
# we can access values like with [] but we cannot change them. This is called a tuple
rowlabel=("Sleep","Eat","Work","Fun")
axs.axis('tight')
axs.axis('off')
rcols = ["white"] * len(wd)
cecols = [["gray"] * len(wd[0])]*len(wd)
clcols = ["green"] * len(wd[0])
tbl = axs.table(cellText=wd,rowLabels=rowlabel,\
colLabels=collabel,loc='center',\
cellColours=cecols,\
colColours=clcols,rowColours=rcols)
tbl.auto_set_font_size(False)
tbl.set_fontsize(14)
tbl.scale(1.2, 1.4)
l, b, w, h = axs.get_position().bounds
axs.set_position([l + .1*w, b, w*.8, h])
axs.set_title("Weekly schedule")
# this is the second one
tplot = plt.subplot(2,1,2)
tplot.axis('off')
tplot.clear()
tplot.axis('off')
txt = """
the meaning of the table is very clear.
however, if we remove the row and columns labels
the context is completely lost and we have no idea
what these number should tell us ...
"""
tplot.annotate(txt,\
xy=(1, 1), xytext=(.96,.94), xycoords="data", \
textcoords="axes fraction",ha="right", va="top", size=14)
# show the figure
plt.show()