-
Notifications
You must be signed in to change notification settings - Fork 2
/
python.py
362 lines (269 loc) · 10.4 KB
/
python.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
# Inspired by Learn Lua in 15 minutes http://tylerneylon.com/a/learn-lua/
# -----------------------------------------------------------------------------
# Python 3 - Documentation: https://docs.python.org/3/
# Python PEP8 - Style Guide: https://www.python.org/dev/peps/pep-0008/
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -- Comments
# -----------------------------------------------------------------------------
# pound character starts a one line comment.
"""
triple quotes start a multi line string.
This also serves as a comment, and a doc string.
"""
# -----------------------------------------------------------------------------
# -- Imports
# -----------------------------------------------------------------------------
# import modules
# this method requires fully typing namespace.
import sys
# this method lets you use the class without typing for namespace.
from UserDict import UserDict
# -----------------------------------------------------------------------------
# -- Variables and Control Flow
# -----------------------------------------------------------------------------
# Everything in Python is an object.
# Python is dynamically typed. (You don't declare type)
# An integer assignment
counter = 100
# A floating point
worth = 999.56
# Python is strongly typed. Variables have to be cast after being set.
assert 'I am worth ${:.0f}'.format(worth) == 'I am worth $1000'
print "worth " + str(worth) #int(), float()
a = 'hello world' # Immutable strings
b = "single or double quotes can be used for string literals."
# slice operator, [start:stop:step]
# slice is used to substring or get ranges for list.
assert a[0:4] == 'hell'
assert a[:4] == 'hell'
#gets even indexes
assert a[1::2] == 'el ol'
c = 1
d = 3
# indentation determines scope for control structures, etc.
# python supports "and", and "or" keywords for boolean logic. also support short circuiting.
if c == 1 and d != 1:
print 'c is one'
print 'this is also printed conditionally.'
elif c != 1:
print 'this will not print'
print 'this will also not print.'
else:
print 'winter is coming'
# while loop
while c <= 3:
print c
c += 1 # no ++ increment operator
# for loop. prints 1 2 3
for e in [1, 2, 3]:
print e
print 'prints range: 0, 1, 2'
for f in range(0, 3):
print f
# ** raises to a power. prints 9
assert 3 ** 2 == 9
# The 'null' value in Python is represented by keyword None
# 'is' keyword is identity operator
h = None
if h is None:
print 'h is None'
# logic operators don't return a boolean.
# they return the values they compare
# this prints: b
print 'a' and 'b'
# 0, '', [], (), {}, and None evaluate to false
print '' and 'b' # print a blank.
if '' and 'b':
print 'this will not print'
#not is the negation operator
if not false
print 'this will print'
# -----------------------------------------------------------------------------
# -- Functions
# -----------------------------------------------------------------------------
# keyword def introduces function definition.
# Python functions can return multiple values
def mult(i, j):
return i * j, '!'
result, exclamation = mult(2, 3)
assert result == 6
assert exclamation == '!'
# Python doesn't have a traditional main method, it just runs the script
# This is the standard way to invoke the main method.
# It uses "magic" methods.
def main():
print 'main method'
if __name__ == '__main__':
main()
# -----------------------------------------------------------------------------
# -- Strings
# -----------------------------------------------------------------------------
my_string = 'hello world'
#sub string is accomplished with range operator
#if a string contains a sub string can be accomplished with in operator.
assert my_string.capitalize() == 'Hello world'
assert my_string.center(20) == ' hello world '
assert my_string.count('l') == 3
assert my_string.endswith('world') is True
assert my_string.find('lo') == 3
assert 'My name is {}'.format('Guido') == 'My name is Guido'
assert 'abc123'.isalnum() is True
assert 'abc'.isalpha() is True
assert '123456'.isdigit() is True
assert 'abc'.islower() is True
assert ' '.isspace() is True
assert 'Hello World'.istitle() is True
assert 'HELLO WORLD'.isupper() is True
assert ' '.join(['hello', 'world']) == 'hello world'
assert my_string.ljust(20) == 'hello world '
assert 'Hello World'.lower() == 'hello world'
assert my_string.lstrip('eh') == 'llo world'
assert my_string.replace('l', 'Z') == 'heZZo worZd'
assert my_string.rfind('o') == 7
assert my_string.rjust(20) == ' hello world'
assert my_string.rstrip('drl') == 'hello wo'
assert my_string.split() == ['hello', 'world']
assert 'hello world'.split() == ['hello', 'world']
assert 'hello<>world<>!'.split('<>') == ['hello', 'world', '!']
assert my_string.startswith('hel') is True
assert my_string.startswith(('ab', 'cd', 'hel')) is True
assert ' test string '.strip() == 'test string'
assert my_string.strip('hled') == 'o wor'
assert 'hElLo WoRlD'.swapcase() == 'HeLlO wOrLd'
assert my_string.title() == 'Hello World'
assert my_string.upper() == 'HELLO WORLD'
assert '2'.zfill(4) == '0002'
# -----------------------------------------------------------------------------
# -- Classes
# -----------------------------------------------------------------------------
# Python is object oriented
class MyClass:
#attributes
i = 1234
#Class instantiation
x = MyClass()
# super class is in parenthesis. allows multiple base classes.
class FileInfo(UserDict):
""" doc string documents the class """
# __init__ is an initializer called after construction
def __init__(self, fileName=None):
""" function doc string """
pass
# The first argument of every class method, including init, is always a reference to the current instance of the class.
# By convention, this argument is always named self.
# -----------------------------------------------------------------------------
# -- Dict
# -----------------------------------------------------------------------------
my_dict = {'key1': 'value1', 'key2': 'value2'}
# return the item of my_dict with key -> 'key1'
assert my_dict['key1'] == 'value1'
# return the value for key if key is in the dictionary, else default
# if default is not given, it defaults to None
assert my_dict.get('key47', 'key not found!') == 'key not found!'
# test 'key1' for membership in my_dict
assert 'key1' in my_dict
# test 'abc' for non-membership in my_dict
assert 'abc' not in my_dict
# number of items in my_dict
assert len(my_dict) == 2
# delete a key/value pair from my_dict, confirm
del my_dict['key1']
assert 'key1' not in my_dict
# -----------------------------------------------------------------------------
# -- Lists
# -----------------------------------------------------------------------------
my_list = ['a', 'b', 'c', 1]
assert list('abc') == ['a', 'b', 'c']
# append to the end of the list
my_list.append('last')
assert my_list == ['a', 'b', 'c', 1, 'last']
# count total occurrences of a value in a list
assert my_list.count('a') == 1
# extend the list
my_list.extend([5, 6, 7, 8])
assert my_list == ['a', 'b', 'c', 1, 'last', 5, 6, 7, 8]
# index of a value in the list
assert my_list.index('c') == 2
# insert a value into the list, insert(index, value)
my_list.insert(4, 'd')
assert my_list == ['a', 'b', 'c', 1, 'd', 'last', 5, 6, 7, 8]
# length of the list
assert len(my_list) == 10
# pop removes the last object by default, can be saved to variable
my_tuple = my_list.pop()
assert my_tuple == 8
assert my_list == ['a', 'b', 'c', 1, 'd', 'last', 5, 6, 7]
# pop can also remove an object by index
assert my_list.pop(5) == 'last'
assert my_list == ['a', 'b', 'c', 1, 'd', 5, 6, 7]
# remove a value from the list
my_list.remove(1)
assert my_list == ['a', 'b', 'c', 'd', 5, 6, 7]
# reverse the list in-place
my_list.reverse()
assert my_list == [7, 6, 5, 'd', 'c', 'b', 'a']
# create a new sorted list
assert sorted(my_list) == [5, 6, 7, 'a', 'b', 'c', 'd']
assert sorted(my_list, reverse=True) == ['d', 'c', 'b', 'a', 7, 6, 5]
assert sorted(['aaa', 'bb', 'c'], key=len) == ['c', 'bb', 'aaa']
# original list remains the same
assert my_list == [7, 6, 5, 'd', 'c', 'b', 'a']
# sort original list in-place
my_list.sort()
assert my_list == [5, 6, 7, 'a', 'b', 'c', 'd']
# -----------------------------------------------------------------------------
# -- Tuples an immutable list
# -----------------------------------------------------------------------------
my_tuple = ('a', 'b', 'c') # parenthesis are optional
my_tuple_2 = 'a', 'b', 'c'
my_tuple_3 = tuple('abc')
# all three variations create an equivalent tuple
assert my_tuple == my_tuple_2 == my_tuple_3
# count the total occurrences of a value in the tuple
assert my_tuple.count('c') == 1
# index of a value in the tuple
assert my_tuple.index('b') == 1
# slicing
assert my_tuple_2[1:] == my_tuple_3[-2:] == ('b', 'c')
# single value tuple
p = 'z',
q = tuple('z')
assert p == q == ('z',)
# -----------------------------------------------------------------------------
# -- Sets
# -----------------------------------------------------------------------------
my_set = {4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1}
assert my_set == {1, 2, 3, 4} == {4, 1, 3, 2}
my_set_2 = {7, 7, 6, 6, 5, 5, 4, 4, 4}
assert my_set_2 == {4, 5, 6, 7} == {6, 5, 7, 4}
# add a value to the set
my_set.add(5)
assert my_set == {1, 2, 3, 4, 5}
# return a new set with a shallow copy of the set
copy_of_set = my_set.copy()
assert copy_of_set == my_set
# clear all contents inside the set
copy_of_set.clear()
assert copy_of_set == set([])
# return a new set with elements that are not in the other set(s)
assert my_set.difference(my_set_2) == {1, 2, 3}
assert my_set - my_set_2 == {1, 2, 3}
# return a new set with elements common to the set and all others
assert my_set.intersection(my_set_2) == {4, 5}
assert my_set & my_set_2 == {4, 5}
# number of elements in the set
assert len(my_set) == 5
# test for membership
assert 2 in my_set
# test for non-membership
assert 47 not in my_set
# remove a value from the set
my_set.remove(1)
assert my_set == {2, 3, 4, 5}
# return a new set with elements in either the set or other but not both
assert my_set.symmetric_difference(my_set_2) == {2, 3, 6, 7}
assert my_set ^ my_set_2 == {2, 3, 6, 7}
# return a new set with elements from the set and all others
assert my_set.union(my_set_2) == {2, 3, 4, 5, 6, 7}
assert my_set | my_set_2 == {2, 3, 4, 5, 6, 7}