-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython101.py
More file actions
executable file
·127 lines (97 loc) · 2.79 KB
/
Copy pathpython101.py
File metadata and controls
executable file
·127 lines (97 loc) · 2.79 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
#!/usr/bin/env python3
## Lists ##
# create a list
a = [17, 15, 43, 73]
# read from a list
print(a[2]) # => 43
# update a list
a[0] = 42
# push to a list
a.append(17)
# pop from a list
x = a.pop()
# iterate over each list element
for x in a:
print("-", x)
# iterate over each list index
for i in range(0, len(a)):
print(i, "-", a[i])
# lists can contain mixed types!
a = [13, 'fun', 8.9, 'apple']
# create an empty list
a = [ ]
# and then push things to the list
a.append(7)
a.append(3)
a.append(4)
print(a) # => [7, 3, 4]
# lits can also contain other lists!
points = [[6, 3], [1, 2], [4, 5]]
print(points[1]) # => [1, 2]
print(points[0][1]) # => 3
# update works just the same
points[2] = [7, 5]
points[2][0] = 4
# create empty arrays and matrices
a = [0] * 5
print(a) # => [0, 0, 0, 0, 0]
m = [[0 for x in range(3)] for x in range(4)]
print(m)
# => [[0, 0, 0],
# [0, 0, 0],
# [0, 0, 0],
# [0, 0, 0]]
## Dictionaries
# a simple dictionary
tel = {'jack': 4098, 'sape': 4139}
# add a key-value pair to the dictionary
tel['guido'] = 4127
# note: the order of keys is arbitrary
print(tel) # => {'jack': 4098, 'guido': 4127, 'sape': 4139}
# modify the value of a key in a dictionary
tel['jack'] = 1234
print(tel) # => {'jack': 1234, 'guido': 4127, 'sape': 4139}
# remove a key from a dictionary
del tel['sape']
print(tel) # => {'jack': 1234, 'guido': 4127}
# test if a key is in the dictionary
if 'jack' in tel:
print("jack is in the phone book")
else:
print("jack is not in the phone book")
# note: you get an error if you try to delete a non-existent key
# so, if you don't know if the key is present, you might need to check first
if 'wiki' in tel:
del tel['wiki']
# create an empty dictionary
d = { }
# and then add stuff to it, types can be mixed
d['a'] = 123
d['b'] = 'apple'
d[17] = 24
d[7.9] = 'thing'
# again note keys are in arbitrary order
print(d) # => {'a': 123, 17: 24, 'b': 'apple', 7.9: 'thing'}
# iterate over dictionary keys
for k in tel:
print(k, "->")
# iterate over key-value dictionary pairs
for k, v in tel.items():
print(k, "->", v)
# iterate over dictionary values
for v in tel.values():
print("->", v)
# list and dictionaries can be mixed freely, for example to describe a graph
# using adjacency lists
graph = { 'a': ['b', 'c'], 'b': ['w'], 'w': ['c', 'a'] }
# or an adjacency graph with costs
graph = { 'a': {'b': 5, 'c': 7}, 'b': {'w': 1}, 'w': {'c': 7, 'a': 3} }
print("the cost from {0} to {1} is {2}".format('b', 'w', graph['b']['w']))
# often a list of "names" and a "cost" matrix are much easier!
name = ['a', 'b', 'c']
cost = [[None, 2, 3],
[ 2, None, 1],
[ 3, 1, None]]
print("the cost from {0} to {1} is {2}".format(name[0], name[2], cost[0][2]))
# Read a lot more at:
# http://docs.python.org/3/tutorial/datastructures.html