-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_programming.py
244 lines (163 loc) · 6.78 KB
/
basic_programming.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
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
"""
Programming is essentially a list of commands that are executed in order to produce some output
To keep track of the data, and to make it human-readable, we give names to the outputs at various points. These named
data are called variables.
There are lots of different kinds of variables types that you will encounter. We will cover some simple ones.
A variable is something you declare, just a name you give to some value/computation so that you can read your code
"""
"""
Assigning variables
- we can assign values to names that we choose, and these are called variables
"""
a = 5
b = 10
print(b)
"We can define new variables using existing ones"
c = a * b
print(c)
a = b = 5
a, b = 5, 10
a, b = b, a
print(a)
"""try printing the sum of two variables"""
"""coding is more than just integers"""
print(type(5))
print(type(5.6))
print(type('text'))
"""
Data types
- basic types (integers, floats, strings)
- data types behave differently and do different things
https://docs.python.org/3/library/stdtypes.html -> visit this website, use it as a resource
"""
my_first_name = 'Stephen'
my_last_name = 'Lenzi'
my_full_name = my_first_name + ' ' + my_last_name
print(my_first_name)
print(type(my_first_name))
print(my_full_name)
"""what happens if you try to add two different types together?
TRY THIS: add my_full_name and a number together
"""
"""some data types allow you to deal with many things (e.g. lists, tuples)"""
my_empty_list = []
my_randomly_organised_stuff = [1, 55, 218, 'cheese', 14, my_full_name, 55, 1]
my_number_list = [1, 55, 218, 555, 222, 2123, 55, 1]
"""indexing - i.e. getting stuff out of lists/arrays/etc"""
my_randomly_organised_stuff[0]
"""TRY THIS: get the 3rd element of the list"""
my_randomly_organised_stuff[-1]
"""TRY THIS: get the 3rd element from the end of the list by indexing"""
"""adding things to a list"""
my_empty_list.append('cheese')
"""tuples"""
my_randomly_organised_tuple = (1, 55, 218, 'cheese', 14, my_full_name, 55, 1)
"""TRY THIS: get the cheese from the tuple"""
"""
Mutability: sometimes you want to change the data
- lists are good for this, it's what we call mutable
- other data types don't allow it, they're what we call immutable
"""
my_randomly_organised_stuff[3] = 'gouda'
"""
TRY THIS: print the list, see that is has changed
TRY THIS: try to assign a new value to my_randomly_organised_tuple?
- does it work the same as for the list? if not, why not?
"""
my_randomly_organised_tuple[3] = 'gouda'
""""
Unpacking
- there are some neat ways of assigning variables
"""
my_tuple = (1, 5, 6, 60, 200, 20)
a, b, c, d, e, f = my_tuple
a, *b, c = my_tuple
"""TRY THIS: Determine types of a, b and c?"""
"""TRY THIS: by unpacking variables, split the tuple such that the first three elements go into a single variable
and the rest into different variables"""
"""Dictionaries, Arrays and Sets"""
my_set_of_randomly_organised_stuff = set(my_randomly_organised_stuff) # casting from a list to a set
my_number_set = set(my_number_list)
my_set_of_randomly_organised_stuff.intersection(my_number_set)
my_dictionary_of_stuff = {'stringy_things': ['cheese', my_full_name],
'numerical_things': [1, 55, 218, 14],
'the number one': 1}
my_dictionary_of_stuff.keys()
my_dictionary_of_stuff.values()
my_dictionary_of_stuff.items()
"""Indexing:
TRY THIS: can you access cheese from the dictionary?"""
""" TRY THIS: Make a dictionary of your own that uses numbers as keys and strings as values"""
""" Loops
- sometimes you want to run snippets of code many times
- e.g. what if we have 100 numbers to add together, it would be tiresome to write 100 lines of code
- loop through each element in a list
- for each vs for i in
"""
for i in range(10):
print(i)
for number in my_number_list:
print(number)
for x in my_number_list:
print(number)
"""maybe this does not work as expected. what result do you get? can you figure out what is happening?"""
"""getting (and using) the loop iteration"""
for item in my_dictionary_of_stuff.items():
print(item)
for i, item in enumerate(my_dictionary_of_stuff.items()):
print(i, item)
""""TRY THIS: Write a loop that iterates through my_number_list and adds the values to a variable called running_total
at each iteration, print the iteration number and the current running total"""
"""
Conditional statements
- Sometimes you want code to execute only if certain criteria are met
- One way of achieving this is using conditional statements (if)
What if we only want to add numbers that fulfil some specific criteria?
"""
running_total = 0
for x in my_number_list:
if x > 200:
print(x)
running_total += x
elif x < 200:
print('this number is too small to print')
elif x == 200:
print('this number is exactly 200, what are the chances of that?!')
else:
print('If you are seeing this something has gone wrong')
"""
What if instead of my_number_list you do this with my_randomly_organised_stuff
TRY THIS: run the same code but replace my_number_list with my_randomly_organised_stuff
- does it work the same way? If not, why not?
- TRY THIS: can you modify this code so that it runs to completion without triggering any errors?
- TRY THIS: can you modify the code such that 50 is subtracted from the total if x is a string?
"""
"""Installing packages with pip
install numpy, matplotlib and pandas
add some lines to the script that use numpy as matplotlib
run the script."""
import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(np.zeros(500))
y = np.random.normal(np.zeros(500))
plt.figure()
plt.scatter(x, y, color='k')
plt.show()
"""TRY THIS: Using what we have learned today about loops, and conditional statements, replot each quadrant in a different color
Bonus: try to achieve the same using np.logical_and()
"""
"""Python can be executed in different ways. Integrated Development Environments (IDEs) allow you do to this in a user
friendly way (think Pycharm, or MATLAB). However, the simplest thing we can do is run python ourselves from the
command line.
make a script (called my_first_script.py)
the data is a track (x,y positions) of a mouse running around a box
- load using numpy or pandas
- plot the data
- cut the data down to the first 1000 frames and plot that on top in a different color
- find the points where the mouse crosses the 200 line in x
- and the corresponding points in y(hint: try using numpy.where())
- plot a red dot at these x, y locations (use plt.scatter)
- print the estimated x_speed at these locations to the console (hint the x speed is in the csv)
- save the figure
- try to do this again for the last 500 frames, with a crossing value of 400 (is it easy for you to change?)
"""