Repository navigation
Expand file tree
/
Copy path9302014.notes
More file actions
312 lines (181 loc) · 6.76 KB
/
Copy path9302014.notes
File metadata and controls
312 lines (181 loc) · 6.76 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
Class Notes - Day 1
====================
Lightning Talk
5 minutes - must relate to Python somehow, doesn't matter
Somebody wrote a poem
Python used for
CS education
Application scripting
Sysadmin
Web apps (Django/Flask)
Scientific Computing
Automated software testing/distributed version control
Research (natural language graph, theory, distributed computing
Used by:
Beginners
Professional software developers
Professionals OTHER THAN computer scpialists
Gets lots of things right:
Looks nices, makes sense
No ideology about best way to program
No platform preference
Easy to connect to other languages
Large standard library
Larger network of external packages
What is python?
Dynamic language
Byte compiled
Interpreted
Not like compiled langs.
No type declarations (strings, ints)
Less code means fewer bugs!
Dynamic typing
Type checking and dispatch happen at run-time
x = a + b
What is a?
What is b
What does it mean to ad them
a and b can change at any time before this process
(crunch number programs run slower in Python because it has to check this stuff_
Strong typing!
A + 1 won't work because int(1) and str(A)
" If an object behaves as expected at run-time, it’s the right type. "
Python 2/3 are relatively similar
Python "Wall of Shame" (a few key packages still not supported)
Most code in the wild is still 2.x
There are ways to write compatibile code (Python Docs)
Three elements to your environment when working with Python:
{
Command line
Your interpreter
Your editor
}
help()
Exit with q
dir() = return names in current scope
Ipython <---use it
Look these up in Vim Docs!
{
Code linting? - A system that will examine your code and tell you things you've done wrong with it
Jump to function definition
}
pip
Python = batteries included - almost everything we need is in the standard library
Python Package Manager
Ipython notes:
%paste - from the system clipboard
Tab complete
Ipython saves the variables from a script run with 'run script.py' from within Ipython
A file with python code in it is a 'module' or 'script'
(More on that distinction later on)
Both named *.py
All programmin is about manipulating values
dir(42) all value/stuff built into int(42)
type() find out the type!
Basic Value Types
Numbers (Floats/Ints/Complex numbers (e.g. 42j )
Text (" or ')
Boolean - spelled with capital (also: None object)
An expression is something that evaluates and results in a value (output)
A statement "does something" but doesn't result in an output
This works
print "the value is", x
Python automatically adds a newline, you can suppress it with a comment (does add a space)
Blocks of code are delimited by a colon and indentdation
Python uses indentation to delineate structure
This means that in Python, whitespace is significant
Standard is to use five spaces to indent your code
Tab character and an spaces mean different things to Python
Tab = 8 spaces
NEVER INDENT WITH TABS
Set Vim to map <tab> = /s/s/s/s/s
An expression is made up of values and operators
Produces a new value
Can be used as a calculator
Int and float arithmetic is different
If ONE of the two values is a float, you get a floating point answer
"Floor division" - I want the integer value of division, //
Python3 does float division by default
Type Conversion
Python will try to convert types
Type errors checked at runtime only
Will get type errors mixing Unicode/ASCII
Symbols are how we give names to these various values
Symbols must contain __ or letter or letters
Can't start with a number
Symbols don't have a type; values do
-This is why Python is dynamic
A symbol is bound by to a value with an =
A value can have many names or none
a = 1
b = a
b == 1
Assignment is a statement, it returns no value
Evaluating the name will return the value to which it is bound
In most languages, symbols are known as "variables"
But many of you defined a "variable" as something like: "a place in memory that can store values"
That is NOT what a name is
A name is bound to a value, but has nothing to do with location in memory
In-place assignment operator = x = 3, x += 1, x == 4
Works with all mathematical operators
Multiple assignment: i, j = 4, 5; i = 4, j = 5
Swap with i, j, = j, i; i = 5, j = 4
You can't actually delete anything in Python
the del command only unbinds an object
Python keeps track until something is no longer referred to
Identity:
Every value in Python is an object
id(x)
Unique number that identifies
Every object is unique and can be tested with id()
"is" operator will tell us if two objects are the same
Most useful in debugging
== checks to see if two things have the same value
=! checks to se if things were not equal
Operator Precedence:
4 + 3 * 5 != (4 + 3) * 5
Follows PEMDAS
Use () to do stuff out of order
This is what the Prof uses frequently
Python Operator Precedence
Look up online
Keywords: some have a special meaning and you can't use them for variable assignments
Python also has a number of builtins: dir(__builtins__) -- these can be rebound?
Be careful that you didn't redefine something needed in the builtins
This is a bad idea. You might do it accidentally
Exceptions are how Python tells you something is wrong
NameError - Python doesn't know about a name
TypeError - You're trying to do something to a type that doesn't work, e.g. int + str
SyntaxError - you probably mistyped something
AttributeError - indicates that you're trying to access a method like s.fred() instead of s.upper()
What is a function?
A self contained chunk of code
A way to repeat a piece of code
DRY - don't repeat yourself
Sometimes it's clean to just put everything by itself
Functions take and return information
You define a function with:
'def function()'
pass #do nothing
def is a statement
Creates a function object
Creates a local name
Run a function with function()
Arguments go in the ()
without parens just gives <function __main__.function> <--exception?
If you don't tell a function to return something, it returns the None object
Functions can call functions and other functions and other functions
If you want to return something, put 'return' and then what you want returned
Only one return statement will ever run
Functions can return more than one result
x,y,z = function() -> Will set all three
Sometimes you want to pass arguments (parameters)
def function(x,y,z)
print x + y + z
Parameters are what a function takes, arguments are what you give it
If you want to do anything interesting, you need an "if" statement
========================
Talk about Python for 5 minutes next week
Work on Python exercises at CodingBat
Get all the warmups done
Get homework from website