xxxxxxxxxx
# list: mutable, can be indexed and sliced, repetitions allowed
l = [0, 1, 2.3, "hello"]
l[0] >>> 0
l[-1] >>> "hello"
l[1:2] >>> [1, 2.3]
# sets: as list, but repetions are rejected
s = {0, 2, 63, 5, 5} # the second 5 will be discarded
# tuple: can be indexed, immutable
t = (5, 6, 8, 3)
t[0] >>> 5
t[2] >>> 8
# dictionary: pairs key - value.
# Types of keys and values must (or, for your sanity, should be) uniform type
# keys are unique
d1 = {"key1" : "value1", "key2" : "value2"}
d2 = {1 : "value1", 2 : "value2"}
d3 = {"key1" : 1, "key2" : 2}
print(d1["key1"]) >>> "value1"
print(d2[1]) >>> "value1"
print(d3["key1"]) >>> 1
xxxxxxxxxx
myset = {"apple", "banana", "cherry"}
mylist = ["dog", "cat", "humanoid"]
mydict = {'planet': 'blue', 'edges': 4, 'perimeter':15}
mytuple = ('earth', 'mars', 'mercury')
xxxxxxxxxx
# Python3 program for explaining
# use of list, tuple, set and
# dictionary
# Lists
l = []
# Adding Element into list
l.append(5)
l.append(10)
print("Adding 5 and 10 in list", l)
# Popping Elements from list
l.pop()
print("Popped one element from list", l)
print()
# Set
s = set()
# Adding element into set
s.add(5)
s.add(10)
print("Adding 5 and 10 in set", s)
# Removing element from set
s.remove(5)
print("Removing 5 from set", s)
print()
# Tuple
t = tuple(l)
# Tuples are immutable
print("Tuple", t)
print()
# Dictionary
d = {}
# Adding the key value pair
d[5] = "Five"
d[10] = "Ten"
print("Dictionary", d)
# Removing key-value pair
del d[10]
print("Dictionary", d)
xxxxxxxxxx
# Python3 program for explaining
# use of list, tuple, set and
# dictionary
# Lists
l = []
# Adding Element into list
l.append(5)
l.append(10)
print("Adding 5 and 10 in list", l)
# Popping Elements from list
l.pop()
print("Popped one element from list", l)
print()
# Set
s = set()
# Adding element into set
s.add(5)
s.add(10)
print("Adding 5 and 10 in set", s)
# Removing element from set
s.remove(5)
print("Removing 5 from set", s)
print()
# Tuple
t = tuple(l)
# Tuples are immutable
print("Tuple", t)
print()
# Dictionary
d = {}
# Adding the key value pair
d[5] = "Five"
d[10] = "Ten"
print("Dictionary", d)
# Removing key-value pair
del d[10]
print("Dictionary", d)