-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjExprVars.py
375 lines (316 loc) · 6.3 KB
/
objExprVars.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#objExprVars.py
'''
see hw1.py for format for 1st assignment (other assignments will have
different format)
'''
#####IDLE #####
'''
two modes:
interactive shell
file editor
Keyboard shortcuts:
CTRL- N: new file
CTRL- S: save file
f5- run module
alt-n, alt p: previous and next statement
'''
##### variables #####
'''
variables are names for obects/values
an assignment:
<variable> = <expression>
1) <expression> is calculated
2) result is stored with name <variable>
variables:
-don't exist until they are assigned to
-can be reused/reassigned
=don't care what type they hold
(but expressions do care)
variable names:
-use a...z,A...Z, 0...9, _
-cant start with digit
-names are cAse SENsitIve
-use relatively meanigful names that aren't too long
-use camelCase
-indicate data type and what's in the variable
-don't use reserved words or core language
-not suggested to use builtins
'''
##### expressions #####
'''
WE can use idle like a calculator
use these operations
(listed in precedence order)
()
**
*,/,//,%
+,-
// and % are friends
>>> #132 minutes in hours, minutes
>>> 132//60
2
>>> 132%60
12
usually use for intergers
floats tend to be approximations:
>>> 123.65 % 1
0.6500000000000057
>>> #functions
>>> abs(-5)
5
>>> max(-2,-3)
-2
>>> #convert 32 degrees farenheight
>>> #to celsius
>>> (32-32)* 5/9
dynamic typing
python doesn't check variable type until it's used in code
can be potentially problematic
>>> #farenheight to celsius
>>> f = 32
>>> c = (f-32) * 5/9
>>> c
0.0
>>>
#the vars "notebook" doesn't remember expressions but remembers what was
#was computed(the results)
>>> c = (f-32) * 5/9
>>> f = 212
>>> c = (f-32) * 5/9
>>> c
100.0
if is part of the core language
max is not. It's a function. Part of a "builtins"
when python looks for a max but max is a variable, it looks in builtins
'''
#####bool(ean) expressions ######
'''
boolean expressions
-math expressions
with results of True or False
boolean operators
==,!= : equal, not equal
<,>,<=, >=,
compount operators
and, or, not
conversions:
from bool to int/float
True-> 1, False-> 0
from int/float to bool
0-> False, everything else=> True
>>> x = 2
>>> x==3
False
>>> x !=3
True
>>> x<2
False
>>> x<=2
True
>>> import random
>>> x = random.randrange(0,100) #0..9
>>>
>>> x%2
0
>>> #is x odd?
>>> x %2 !=0
False
>>> x
48
>>> x = random.randrange(0,100) #0..99
>>> x
27
>>> x %2 1=0
SyntaxError: invalid syntax
>>> x%2 !=0
True
>>> x
27
>>> #check whether a number is divisible by 3
>>> x%3 ==0
True
>>>
>>> x = random.randrange(0,100)
>>> x%3 ==0
False
>>> x
31
>>> #x is divisibe by 2 or 3
>>> x = random.randrange(0,100)
>>> x%2 ==0 or x%3==0
True
>>> x
74
>>> x%2 ==0 and x%3==0
False
>>> x
74
>>>
>>> #negation
>>> x = 5
>>> x<6
True
>>> not x<6
False
>>> #pitfall
>>> #is x one of 2,3, or 4
>>> x = 7
>>> x ==2 or 3 or 4
3
>>> #should have written
>>> x==2 or x==3 or x--4
11
>>> x==2 or x==3 or x==4
False
>>>
'''
#####type/class of an object ######
'''
variabes don't have a type but
>> type(3)
<class 'int'>
>>> type(3.3)
<class 'float'>
>>> x = 9.3
>>> type(x)
<class 'float'>
>>> type(11//3)
<class 'int'>
>>> type ('apple')
<class 'str'>
>>> type([1,2,3])
<class 'list'>
>>> type(x==3)
<class 'bool'>
>>>
'''
#####str(int) #####
'''
str
-0-based indexed sequence of characters
for textual data
delimit strings with '', '' '' , ''' ''',
\n is new line character
functions
len() : length
max, min
operators
in, not in
+ - concatenation
int*str, str*int - multiples
<,>, <= , >= , ==, != (al upper case less than all lower case)
indexing
s[i] - character at index i
i from 0 to len(s)-1
or backwards from -1 to -len(s)
slicing:
s[start: stop] - substring of s starting at start (inclusive) and stopping at
stop (exclusive)
start defaults to 0 stop defualts to end
s[start: stop : step]
s[::-1] - reverse of s
s = 'pear'
>>> type(s)
<class 'str'>
>>> t = "apple"
>>> t
'apple'
>>> u = "Fred's place"
>>> u - 'Fred\'s place'4
SyntaxError: invalid syntax
>>> u - 'Fred\'s place'
Traceback (most recent call last):
File "<pyshell#108>", line 1, in <module>
u - 'Fred\'s place'
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> u = 'Fred\'s place'
>>> u
"Fred's place"
>>> s
'pear'
>>> len(s)
4
>>> #string on multiple lines
>>> sentence - "this is......................."
Traceback (most recent call last):
File "<pyshell#114>", line 1, in <module>
sentence - "this is......................."
NameError: name 'sentence' is not defined
>>> #right way to do it#####
>>> sentence = '''this is a sebtebce that runs onto multiple lines'''
>>> 'this is sentence \n that runs oto multiple lines\n;'
'this is sentence \n that runs oto multiple lines\n;'
>>>
>>> #print(sentence)
>>> print(sentence)
this is a sebtebce that runs onto multiple lines
>>> 'this is sentence\n that runs oto multiple \nlines;'
'this is sentence\n that runs oto multiple \nlines;'
>>>
>>>
>>> hello
Traceback (most recent call last):
File "<pyshell#124>", line 1, in <module>
hello
NameError: name 'hello' is not defined
>>>
>>> print(hello)
Traceback (most recent call last):
File "<pyshell#126>", line 1, in <module>
print(hello)
NameError: name 'hello' is not defined
>>> #in tests for substring
>>> 'app' in "apple"
True
>>> Apl kn 'apple''
SyntaxError: invalid syntax
>>>
>>>
>>> 3*(2s-t)
SyntaxError: invalid syntax
>>> 3*(2* s-t)
Traceback (most recent call last):
File "<pyshell#133>", line 1, in <module>
3*(2* s-t)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>>
>>>
>>>
>>> 'apple' < 'pear'
True
>>> "compares dictionary order!
SyntaxError: EOL while scanning string literal
>>> #dompares dictionary order
>>>
>>>
>>> #indexing
>>> s
'pear'
>>> s[1]
'e'
>>> s[0]
'p'
>>>
>>>
>>> #empty str
>>> s = "" #not empty
>>> #empt string the right way
>>> s = ""
>>> len(s)
0
>>>
>>>
>>> #slicing
>>>
>>> s = 'apple'
>>> s{1:4}
SyntaxError: invalid syntax
>>> s[1:4]
'ppl'
>>>
>>> s[::2]
'ape'
>>>
>>> 'Ap'not in 'apple'
True
'''