2013-12-29 22:34:42 +00:00
|
|
|
# basic dictionary
|
|
|
|
|
|
|
|
d = {}
|
|
|
|
print(d)
|
|
|
|
d[2] = 123
|
|
|
|
print(d)
|
|
|
|
d = {1:2}
|
|
|
|
d[3] = 3
|
2016-04-15 16:28:33 +01:00
|
|
|
print(len(d), d[1], d[3])
|
2013-12-29 22:34:42 +00:00
|
|
|
d[1] = 0
|
2016-04-15 16:28:33 +01:00
|
|
|
print(len(d), d[1], d[3])
|
|
|
|
print(str(d) == '{1: 0, 3: 3}' or str(d) == '{3: 3, 1: 0}')
|
2013-12-29 22:34:42 +00:00
|
|
|
|
|
|
|
x = 1
|
2016-02-12 23:38:30 +00:00
|
|
|
while x < 100:
|
2013-12-29 22:34:42 +00:00
|
|
|
d[x] = x
|
|
|
|
x += 1
|
2016-02-12 23:38:30 +00:00
|
|
|
print(d[50])
|
2016-10-17 01:43:47 +01:00
|
|
|
|
|
|
|
# equality operator on dicts of different size
|
|
|
|
print({} == {1:1})
|
|
|
|
|
|
|
|
# equality operator on dicts of same size but with different keys
|
|
|
|
print({1:1} == {2:1})
|
|
|
|
|
2020-02-10 11:22:12 +00:00
|
|
|
# 0 replacing False's item
|
|
|
|
d = {}
|
|
|
|
d[False] = 'false'
|
|
|
|
d[0] = 'zero'
|
|
|
|
print(d)
|
|
|
|
|
|
|
|
# False replacing 0's item
|
|
|
|
d = {}
|
|
|
|
d[0] = 'zero'
|
|
|
|
d[False] = 'false'
|
|
|
|
print(d)
|
|
|
|
|
|
|
|
# 1 replacing True's item
|
|
|
|
d = {}
|
|
|
|
d[True] = 'true'
|
|
|
|
d[1] = 'one'
|
|
|
|
print(d)
|
|
|
|
|
|
|
|
# True replacing 1's item
|
|
|
|
d = {}
|
|
|
|
d[1] = 'one'
|
|
|
|
d[True] = 'true'
|
|
|
|
print(d)
|
|
|
|
|
|
|
|
# mixed bools and integers
|
|
|
|
d = {False:10, True:11, 2:12}
|
|
|
|
print(d[0], d[1], d[2])
|
|
|
|
|
2016-10-17 01:43:47 +01:00
|
|
|
# value not found
|
|
|
|
try:
|
|
|
|
{}[0]
|
2016-10-17 02:01:18 +01:00
|
|
|
except KeyError as er:
|
2018-08-17 06:46:04 +01:00
|
|
|
print('KeyError', er, er.args)
|
2016-10-17 01:43:47 +01:00
|
|
|
|
|
|
|
# unsupported unary op
|
|
|
|
try:
|
|
|
|
+{}
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
|
|
|
|
|
|
|
# unsupported binary op
|
|
|
|
try:
|
|
|
|
{} + {}
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|