In [1]:
some_tuple = (10,20,30)
print(some_tuple[1])
In [2]:
some_tuple = (10,20,30)
print(some_tuple[:])
In [3]:
some_tuple = (10,20,30)
some_tuple[1] = 50
In [4]:
some_tuple = (10,20,30)
some_tuple.append(10)
Geeky note: tuples are useful because they are "hashable" and can be used as keys for dictionaries, while lists are not hashable and can't be dictionary keys.
In [9]:
a_string = "This is a string"
print(a_string[0])
print(a_string[5:])
print(a_string[:-1])
In [6]:
a_string = "This is a string"
a_string[0] = "a"
In [15]:
a_string = "This is a string"
print(a_string)
#Convert the string to a list
a_list = list(a_string)
print(a_list)
# Set the first element in the list to "a"
a_list[0] = "a"
# Convert the list back to a string
a_string = ",".join(a_list)
print(a_string)
In [16]:
some_dict = {"a":10,
"b":20,
"c":30}
print(some_dict["a"])
print(some_dict["b"])
In [21]:
another_dict = {1:10,
"test":print,
1.5:-10,
(1,2,3):"cow"}
print(another_dict["test"])
print(another_dict[(1,2,3)])
In [27]:
some_dict = {"a":10,
"a":20,
"c":10}
some_dict["a"] = [1,2,3]
In [28]:
some_dict = {"a":10,"b":20,"c":30}
for k in some_dict.keys():
print(k,some_dict[k])
In [29]:
some_dict = {"a":10,"b":20,"c":30}
for v in some_dict.values():
print(v)
In [30]:
another_dict = dict([(some_dict[k],k) for k in some_dict.keys()])
another_dict[10]
Out[30]:
iterators
(like range
) that can be looped through with for
.
In [31]:
some_dict = {"a":10,"b":20,"c":30}
some_dict["c"] = 50
print(some_dict)
In [32]:
some_dict = {"a":10,"b":20,"c":30}
some_dict["d"] = 50
print(some_dict)
SOME_DICTIONARY[KEY] = VALUE
In [33]:
some_dict = {"a":10,"b":20,"c":30}
x = some_dict.pop("a")
print(x)
print(some_dict)
In [ ]:
VALUE = SOME_DICT.pop(KEY)
In [ ]:
look_up = {"brown":"cow",
"pink":"pig",
"salmon":"silver",
"it decides":"squid"}