Lists, Dictionaries, Iterations & Loops

Lists; are as they sound, an ordered list of variables. The order matters!

>>>my_list = [ ] >>>my_list = [‘Clara’, ‘Stark’, ‘Ben’] >>>my_list.append(4) # adds 4 to the end of the list >>>2 in my_list # asking wether the number 2 somewhere in my list? Indexing


>>>my_list = [‘1’, ‘2’, ‘3’]


In the list above, number 1 has the index number 0, number 2 has the index number 1 and so on. Thus all lists start at index 0! This makes it convenient to check each position of the list with ease, considering lists can get very long.

You can get a chunk of the list if you know you don’t want the whole thing. >>>my_list[ : 4] # get a list of the first up to the fifth indexed item in my_list (index 0-4) >>>my_list[3 : ] # get a list of the fourth through the last item in my_list (index 3 onward) >>>my_list[5 : 7] # get a list of the sixth up to eighth item in my_list (index 5-7) >>>my_list[-5 : -1] # get a list of the fifth to last up to the last items in my_list You can also assign variables to items in a list;

>>> number = my_list[2] # variable number now holds value at index 2 >>>my_list.del[5] # removes the indexed item >>>my_list[6] = 1 # replaces whatever was in index 6 with the number 1 You can also make lists of lists, also referred to as 2D list (2 dimensions). It is a way to i.g store groups of variables separate in categories such as odd numbers in one and even in the other. It is possible to get the sum of an entire list; >>>sum(my_list)

The length of a list is also possible to obtain, but it is crucial to remember that length>last index; >>>len(my_list) # remember that length > last index Changing the state from a string to an integer or a float is performed like this;

str(1) is the same thing as “1”, and int(“1”) is the same as 1 Dictionaries


Dictionaries are used to store information linked to specific identifiers such as Name: contact information and Account number: Balance Due. In contrast to lists, the order does not matter in dictionaries. Just like lists, you can also assign variables to a dictionary. >>>my_dictionary = {‘Tony Stark’: ‘Iron man’} >>>my_dictionary = {‘num’: 2}

>>>my_dictionary = {12345: 17.38} >>>‘Tony’ in my_dictionary # is ‘Tony’ in my_dictionary? >>>del my_dictionary[‘Tony’] # removes key and value pairs Strings

Strings are "Characters" with quotation marks, also known as "Chars". >>>string = ‘Hello World!’ >>>string[0] # returns ‘H’ >>>string[-1] # returns ‘!’ >>>string[0 : 2] # returns ‘He' Loops

Repeated execution of a set of statements is known as Iterations.


While Loops - While something is still true, keep doing this task”

When unsure of how many times a loop needs to go on, while loops are used. Adding a "Break" stops the loop once the information required is obtained by the loop. The boolean statement can also be used in this type of loop.

Try out the following on your Sublime;



© 2017 Proudly made by YAS ​

  • LinkedIn Social Icon
  • Facebook Social Icon
  • Twitter Social Icon
  • Instagram
  • YouTube
  • TikTok
  • Pinterest