Hello, my name is Manuel and I run ManyProgramming™. Today I will be introducing lists and Dictionaries to you. Make sure to stay tuned every Friday/Saturday so that you can enjoy the full tutorial.
Lists: Are collections of single values. Each element can be anything; integers, strings, floats or other objects; they need to be separated with a comma. I put some examples below
>>> this_is_a_list=[1,3,'Manuel',6,"Daniel"]
>>> this_is_a_list
[1, 3, 'Manuel', 6, 'Daniel']
>>> this_is_a_list[0]
1
>>> this_is_a_list[2]
'Manuel'
>>> this_is_a_list[4]
'Daniel'
To getout one of the elements, the square brackets [] with the number of the element inside. The counting starts with 0 and ends with the total number minus one. The total number of the elements can be obtained with the function ‘len(listname)’.
Lists can even be cut or sliced. You need to use the square brackets [].The slicing is separated by the double colons; as you can see in the example below.
>>> this_is_a_list
[1, 3, 'Manuel', 6, 'Daniel']
>>> this_is_a_list[0:2]
[1, 3]
>>> newlist=this_is_a_list[1:3]
>>> newlist
[3, 'Manuel']
>>> li=list(range(10))
>>> li
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> li[::2]
[0, 2, 4, 6, 8]
>>> li[::3]
[0, 3, 6, 9]
>>> li[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> li*2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> li+li
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
As you can tell, I have made three lists (‘this_is_a_list’, ‘this_is_another_list’ & ‘li’) and have done calculations with the lists. You can call the lists anything other than the reserve words. If you want to know all of them send me an email (programmingmany@gmail.com) and I will send you how to get the reserve words for your version of Python. Lists can be multiplied (*) and added (+) but not divided (/) or taken away (-). You are also able to see different parts of the list as I have just showen you. You are able to have numbers, letters, or words.
Dictionaries: Are lists with an explanation or a value. In Python it is called a key and a value. Just like a physical dictionary. The key (the word that is being explained) has to be unique. It cannot be the same or it will be overwritten as you will see at the bottom of the examples. However, the value (the explanation or meaning of the key) can be the same.
>>> d={"Auto":"Car","Haus":"House, building","A":3}
>>> d
{'Auto': 'Car', 'Haus': 'House, building', 'A': 3}
>>> d["Auto"]
'Car'
>>> d["Auto"]
'Car'
>>> d["auto"] # This is a misspelling
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'auto'
>>> d.keys()
dict_keys(['Auto', 'Haus', 'A'])
>>> d.values()
dict_values(['Car', 'House, building', 3])
>>> dd={'a':2,'b':4,'a':6} # Duplicated keys 'a'
>>> dd
{'a': 6, 'b': 4}
You can also merge dictionaries. This is how; ‘Name_of_the_Dictionary.update (Name_of_the_dictionary_you_want_to_merge)’ If you do this in Python all the keys & values from the dictionary you put in brackets will be put into the the dictionary you put before the ‘.update’; making only one dictionary with all the entries (an entry consists of keys & values.
Thank You very much for reading my blog. Every view helps me a lot. This blog was done by Manuel & Daniel Kolb. I hope you continue reading my blogs every Friday! If you would like to see my book reviews blog than Click Here!