forked from katenevin/python-crash-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatatypes.py
More file actions
executable file
·241 lines (178 loc) · 6.27 KB
/
datatypes.py
File metadata and controls
executable file
·241 lines (178 loc) · 6.27 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
#!/usr/bin/env python
# vim: set fileencoding=utf-8
from __future__ import unicode_literals
from __future__ import division
# PURPOSE
# #######
# this tutorial is a brief overview of the datatypes
# in Python
# it covers:
list
dict
tuple
set
str
int
long
float
bool
True, False
None
# list
# ####
# lists are the basic Python container type
# they are not required to be homogenous (all elements share the same
# type) but this is usually how they are used
# lists are mutable and ordered
# a simple list of city names
cities = ['new york', 'houston', 'los angeles']
assert len(cities) == 3
assert cities[0] == 'new york'
# add an element to the end
cities.append('chicago')
assert len(cities) == 4
assert cities[3] == cities[-1] == 'chicago'
# assign to an element
cities[3] = 'austin'
# remove an element from the end
assert cities.pop() == 'austin'
# lists can be added
borroughs = ['brooklyn', 'queens', 'bronx', 'manhattan', 'staten island']
assert ['brooklyn', 'queens'] + ['bronx', 'manhattan', 'staten island'] == \
borroughs
# another way to do it:
borroughs = []
borroughs.extend(['brooklyn', 'queens'])
borroughs.extend(['manhattan', 'bronx', 'staten island'])
assert len(borroughs) == 5
# determine if an element belongs to a list with `in`
assert 'brooklyn' in borroughs
# sort a list
borroughs = ['brooklyn', 'queens', 'bronx', 'manhattan', 'staten island']
borroughs.sort()
assert borroughs == ['bronx', 'brooklyn', 'manhattan', 'queens',
'staten island']
# reverse a list
borroughs.reverse()
assert borroughs == ['staten island', 'queens', 'manhattan', 'brooklyn',
'bronx']
# dict
# ####
# dicts are the basic mapping type in Python. They're used all over the place
# and the implementation is very efficient.
# a mapping is a key->value pair which supports lookup key. Mappings are
# called hashes or associative arrays in other languages. A key
# may appear only once, but a value may appear multiple times.
# a mapping of city -> population
borroughs = {'manhattan': 1619090, 'bronx': 1408473, 'brooklyn': 2565635,
'queens': 2272771, 'staten_island': 470728}
numbers = dict(one=1, uno=1, two=2, dos=2, three=3, tres=3)
# you can test for membership with `in`
assert 'manhattan' in borroughs
assert 'jersey' not in borroughs
# you can get an item by its key using [] or .get
assert borroughs['manhattan'] == 1619090
assert borroughs.get('manhattan') == 1619090
# the difference is that if the key does not exist, .get can return a default
# value
assert 'jersey' not in borroughs
assert borroughs.get('jersey', 100) == 100
# you can get the keys as a list with .keys, the values as a list with .values
# and (key, value) tuples with .items
# note that dicts are UNSORTED
assert 'manhattan' in borroughs.keys()
assert 2272771 in borroughs.values()
assert ('bronx', 1408473) in borroughs.items()
# tuple
# #####
# tuples are a container type that are immutable and generally used
# for heterogeneous elements (elements of different type)
# they are immutable in that the elements cannot be changed once
# the tuple has been created
singers = [('Joplin', 'Janis', 'Port Arthur', 1943),
('Gomez', 'Selena', 'Grand Prarie', 1992),
('Nelson', 'Willie', 'Abbot', 1933),
('Knowles', 'Beyoncé', 'Houston', 1981),]
for surname, name, city, year in singers:
print name, surname, 'was born in', city, 'in', year
# set
# ###
# sets are like mathematical sets: they are useful for
# quickly testing for membership and for performing
# intersection, union, and difference operations
# elements cannot repeat
female_singers = {'janis joplin', 'janis joplin', 'selena gomez'}
male_singers = {'willie nelson'}
assert len(female_singers) == 2
assert len(male_singers) == 1
# test for inclusion/membership
assert 'janis joplin' in female_singers
assert 'janis joplin' not in male_singers
# calculate the union
assert 'willie nelson' in female_singers | male_singers
# calculate the intersection
assert not female_singers & male_singers
# calculate the difference
singers = {'janis joplin', 'selena gomez', 'willie nelson', 'beyoncé knowles'}
assert singers - female_singers == {'willie nelson', 'beyoncé knowles'}
# str
# ###
# strings are immutable in Python
watchtower = '''"There must be some kind of way out of here,"
Said the joker to the thief,
"There's too much confusion,
I can't get no relief."
'''
# test for inclusion
assert 'confusion' in watchtower
# helpful methods for changing case
assert 'confusion'.upper() == 'CONFUSION'
assert 'confusion'.lower() == 'confusion'
assert 'too much confusion'.title() == 'Too Much Confusion'
# use .split to split on a delimiter
assert 'too much confusion'.split(' ') == ['too', 'much', 'confusion']
# use .strip to remove whitespace
assert ' confusion '.strip() == 'confusion'
# use ' '.join to concatenate strings on a delimiter
assert ' '.join(['too', 'much', 'confusion']) == 'too much confusion'
# int, long, float
# ################
# Python has a rich numeric tower, so you usually don't have to worry
# about the distinction between ints and longs
# note that the largest int you can represent can be found by sys.maxint
from sys import maxint
assert maxint == 2**(64-1)-1 # on 64-bit systems
# we can convert back and forth
assert float(1) == 1.0
assert int(1.0) == 1
# with our from __future__ import division feature, we
# get automatic promotion to floats on division
# while retaining the ability to do truncating division
assert type(5/2) is float
assert type(5//2) is int
# bool & True, False
# ##################
# Python has a boolean type that takes one of two strict values: True
# and False
assert True
assert not False
# True equivalent values
assert bool(1) is True
assert bool([0]) is True
assert bool('a') is True
# False equivalent values
assert bool(0) is False
assert bool([]) is False
assert bool('') is False
# None
# ####
# Python has no concept of pointers, so there is no NULL pointer analogue
# However, there is a None type, which is often used as a convenience to
# indicates some state in tristate logic: not applicable, missing,
# not found, lack of answer, unitialised
# like True, False, there is only one value for this type
assert None is None
x = None
assert x is None
x = [1,2,3]
assert x is not None