forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_DataTypes.py
More file actions
379 lines (334 loc) · 9.82 KB
/
01_DataTypes.py
File metadata and controls
379 lines (334 loc) · 9.82 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
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
376
377
378
############# chap-1 #################
# comment line in python
#https://dabeaz-course.github.io/practical-python/Notes/01_Introduction/04_Strings.html
'''
#Python: It’s All About the Indentation
indentation is used to define compound statements or blocks. In a Python program, contiguous statements that are indented to the same level are considered to be part of the
same block.
https://realpython.com/python-conditional-statements/#python-its-all-about-the-indentation
if <expr> :
<stmt>
<stmt> -- block ends
else
<stmt> -- block ends
while <expr> :
<block in same indentation>
'''
print("test")
## below code doesn ot work
'''
import urllib.request
webUrl = urllib.request.urlopen('https://www.javatpoint.com/python-tutorial')
u = urllib.request.urlopen ('http://ctabustracker.com/bustime/map/getStopPredictions.jsp?stop=14791&route=22','dfd', 11)
u1 = urllib.request.urlopen('https://www.javatpoint.com/python-tutorial')
from xml.etree.ElementTree import parse
doc = parse(u)
for pt in doc.findall('.//pt'):
print(pt.text)
'''
'''
In interactive mode, The underscore _ holds the last result. Nevre use in a program
'''
# simple excercise
bill_thickness = 0.11 #meters
tower_height = 99999 #meters
day = 1
num_bills =1
while num_bills*bill_thickness < tower_height :
print(day,num_bills,num_bills*bill_thickness)
day +=1
num_bills *=2
print('number of days',day)
print('number of bills', num_bills)
print('final height', num_bills*bill_thickness)
'''
learn few thigns from above example
types variables comments indentation code block loop case sensitive-python is case sensitive language conditional statement (if else)
In a print, extra line can be supressed
The extra newline can be suppressed:
print('Hello', end=' ')
print('My name is', 'Jake')
##user input
name = input('enter name')
print('your name is',name)
##pass statement
Sometimes you need to specify an empty code block. The keyword pass is used for it.
if a > b:
pass
else:
print('Computer says false')
## numbers test
float
int
booleans
## operators
x + y Add
x - y Subtract
x * y Multiply
x / y Divide (produces a float)
x // y Floor Divide (produces an integer)
x % y Modulo (remainder)
x ** y Power
x << n Bit shift left
x >> n Bit shift right
x & y Bit-wise AND
x | y Bit-wise OR
x ^ y Bit-wise XOR
~x Bit-wise NOT
abs(x) Absolute value
## comparison
x < y Less than
x <= y Less than or equal
x > y Greater than
x >= y Greater than or equal
x == y Equal to
x != y Not equal to
## Converting Numbers
a = int(x) # Convert x to integer
b = float(x) # Convert x to float
>> solve dave's mortagage
'''
principal = 500000.0
rate = 0.05
overperiod = 30
payment = 2684.11
total_paid = 0.0
interest = 0.0
month = 1
extraPaid = 1000
forMonths = 12
while principal > 0:
if forMonths > 1:
principal = principal - extraPaid
forMonths -=1
principal = principal * (1 +rate/12 ) - payment
total_paid = total_paid + payment
print ('month - principal', month, principal)
month +=1
print('total paid', total_paid)
print('hello')
'''######### 1.4 Strings ##########
# strings are array of chars
# Strings are “immutable” or read-only. Once created, the value can’t be changed.
# but string variable can be reassigned. (something like const pointer and point to constant in C)
# 'in' is a operator in python can be applied in strings
# strings can be read forward and backward (-2)
'''
## raw strings
rs = r'c:\newdata\test'
rs1 = 'c:\newdata\test'
print(rs)
print(rs1)
'reverse string'
name = "prakash"
count = len(name)
print(count/2)
i =0
name1 = ''
while i < round(len(name)):
name1 = name1 + name[count-i-1]
i +=1
print(name)
print(name1)
name = 'zyxmo'
name1 =''
for x in reversed(name):
name1 +=x
print(name1)
## byte string, unicode etc.
bstr = b'hello'
len(bstr)
bstr1 = 'hello'
len(bstr1)
money = 1001.212
## formatted string printing
print(f'{name:10s} {money:5.2f}')
## excercises
symbols = 'AAPL,IBM,MSFT,YHOO,SCO'
symbols[0]
symbols += ',GOOG'
print(symbols)
# using in operator
'IBM' in symbols
'BM' in symbols
## using string methods
'''
dir() produces a list of all operations that can appear after the (.).
Use the help() command to get more information about a specific operation:
'''
dir(symbols)
'''
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__',
'__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs',
'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier',
'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
'''
help(name.upper)
symbols.lower()
symbols.find('IB')
symbols[5:15]
## format string f-strings
'''
Sometimes you want to create a string and embed the values of variables into it.
'''
#examples
tshare ='xyx corp'
price=999.903
sym = 'xy'
f'{tshare} with range{sym} amount {price:0.2f}'
print("hello" f'{tshare}')
## Exercise 1.18: Regular Expressions
'''
use pythong re module for regular expressions
import re
more info: https://docs.python.org/library/re.html
'''
text = 'Today is 08/06/2023. Tomorrow is 08/07/2023.'
# Find all occurrences of a date
import re
re.findall(r'\d+/\d+/\d+', text)
['3/27/2018', '3/28/2018']
# Replace all occurrences of a date with replacement text
re.sub(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text)
text
'''######### 1.5 Lists [ ] ##########
Python’s primary type for holding an ordered collection of values.
Use square brackets [ ] to define a list literal:
Index stats from 0 and Negative indices count from the end.
You can change any item in a list.
# list can have heterogenous itmes
'''
names = ['tim', 'tom','bib', 'bob']
dir(names)
## Sometimes lists are created by other methods. For example, a string can be split into a list using the split() method
line = 'jab tab kab kab'
listline = line.split()
listline
## list operations
names.append('alice')
names.insert(2,'malroy')
miscNames = ['jack', 'jill']
names = names + miscNames
names[4] = 'Mills'
len(names)
names.remove('tom')
'jack' in names # using in operator
'bob' not in names # using not in operator
# replication;
it = [1,2,3,4]
it = it *3 #here list is replicted three times
# list iteration
for name in names:
print(name)
# removing from list using remove('item') or del names[1]
# Lists can be sorted “in-place”.
names.sort()
#Use sorted() if you’d like to make a new list instead:
t = sorted(names)
## taking list of string and join them into a string, with seperatation char
ts = ','.join(names)
ts
type(ts)
type(names)
tx1 = ' '.join(names)
tp1 = list(ts.split(','))
## list of list and
tx = ['test1',names,it]
tx[1][2]
'''######### 1.6 File Management ##########
open file should be closed
'''
## ##1>> Open a file and read data and write to bar.txt
import os
import pathlib
tp = os.getcwd()
# tp2 = pathlib.Path(__file__).parent.resolve() ## __file__ it does not work in interactive mode; so run in file mode
f = open( tp+r"\Work\Data\foo.txt",'rt')
g = open(tp+r'\work\data\bar.txt', 'w+')
data = f.read() #read everything into list
type(data)
print(data)
xdata = data.split()
type(xdata)
print(xdata)
print(';'.join(xdata))
g.write(data)
g.write('------------end -----------')
g.seek(0)
line = g.readline()
print(line)
while line:
print(line)
line = g.readline()
f.close()
g.close()
## ##2>> another way read a line and write a line
f = open( tp+r"\Work\Data\foo.txt",'rt')
g = open(tp+r'\work\data\bar.txt', 'w+')
for dat in f:
print(dat)
g.write(dat)
g.write('------------end2 -----------')
g.seek(0)
for d in g:
print(d)
f.close()
g.close()
## ##3>> another way to open and close file without using close() method
## like C++ idiom --
with open( tp+r"\Work\Data\foo.txt",'rt') as f, open(tp+r'\work\data\bar.txt', 'w+') as g:
for line in f:
g.write(line)
g.seek(0)
for line in g:
print(line)
print("-- done file operation---")
## Redirect the print function to out
with open( tp+r"\Work\Data\foo.txt",'rt') as f, open(tp+r'\work\data\bar.txt', 'w+') as out:
for line in f:
print(line,file=out)
## skip one line in between
next(f)
print("$$$$ output to console is set to file", file=out)
out.seek(0)
for line in out:
print(line)
'''######### 1.7 Functions ##########
Use functions for code you want to reuse. Here is a function definition:
'''
# sum count of n numbers
def sumcount(n):
total =0
while n>0:
total +=n
n -=1
return total
sum = sumcount(10)
print(sum)
## python specific standard libraries
import math
import urllib.request
u = urllib.request.urlopen('https://google.com')
data = u.read()
## Errors and Exceptions
## catching and handling exceptions
## try -except (line try catch block)
## raising excpetion using
raise RuntimeError('What a bluff')
import os
xpath = os.getcwd()
xpath += r'\work\data\foo.txt'
with open(xpath,'rt') as f:
try:
for line in f:
print(line)
except ValueError:
print('Error in reading line')
## Exercise 1.29: Defining a function
## Exercise 1.30: modify pocst.py into a function