Complex Data Types

After investigating the most basic data types for variables, you can now proceed with a little bit more advanced data types.

The more advanced types are:

  • Tuple
  • List
  • Set
  • Dictionary


Let’s start with tuples. They are the simplest composed data type in Python. By “composed” I mean that they can contain different values at the same time while without mixing them together. They are described within parentheses “( )” around the values, separated by commas. Tuples can contain any kind of data types, even other tuples, lists or dictionaries and you can mix the data types used within a tuple.

rotation = (0.123, 0.426, 0.783)
greetings = ("Hello", "Good Morning")
employee = ("Steve", "Johnson", 42, ("glasses", "brown hair", "green eyes"), True) 

If you want to access the individual elements of a tuple, you need to write the name of the tuple followed by square brackets “[ ]”. Inside the brackets, you give the index (i.e. position inside the tuple) with an integer starting with zero as the first element.

pitch = rotation[0]
appearance = employee[3]
eye_colour = employee[3][2]

Good to know about tuples is, that their individual elements can’t be modified. Once created, they remain the same. If you want to change an element, you need to create a new tuple. Read more about tuples on the W3Schools website.


Next, let’s talk about lists. A list is made to list things, similar to tuples. The main difference is, a list can be modified. They are described within square brackets around the values, separated by commas. Also here, the data types inside a list can be of any type including other lists.

names = ["John", "Jane"]
position = [1.2, 3.0, 0.5]
matrix_2x2 = [[0, 1],[0, 1]]

If you have a list in a list, it might be interesting to write it like a table. (The same actually applies for tuples.)

matrix_3x3 = [[0, 1, 1],
              [0, 1, 0], 
              [1, 0, 1]]

A list can be modified which means that you can access each element of the list and change it without the need to make a complete new list. Again, the indexes are given in square brackets and start with zero.

matrix_3x3[0][0] = 0
names[1] = "Sarah"
position[2] = position[2] + 1

If you want to replace an element, the above method is fine ,but what about extending the list by adding an element at the bottom? There is a function built into the list. You can use it with:


The element that you want to add to the list needs to be specified in the parentheses at the end.


Lists have even more functions that ca be accessed like this such as “remove” or “insert”. Read more about lists on the W3Schools website.


A set is similar to a list. The main difference is, that a set is not having any order. The are described within curly brackets “{ }” around the values, separated by commas.

books = {"Sherlock Holmes", "Tom Sawyer", "Lord of the Rings"}
interesting_pages = {12, 23, 54, 62, 102}

In a list, the elements are neatly stored one after the other and their position remains the same. In a set, this is not a case. This means that the following sets are actually the same.

books1 = {"Sherlock Holmes", "Tom Sawyer", "Lord of the Rings"}
books2 = {"Sherlock Holmes", "Lord of the Rings", "Tom Sawyer"}
books3 = {"Tom Sawyer", "Sherlock Holmes", "Lord of the Rings"}

As you can’t access the individual elements of a set, you can’t change them either. Though, you can add elements to the set.


The lack of a numbered structure also means that there is no way you can use an index to access the elements. What is the use of this then? Until now, you have not seen the necessary tools to use sets, but you can use sets to see if a value can be found within a set or you can apply an algorithm to each element of a set. Read more about sets on the W3Schools website.


Dictionaries are comparable to sets. They also have no order so you can’t use an index to retrieve the elements. However, dictionaries have “keys” with which the elements can be accessed by. The are described in curly brackets starting with the name of the key in quotes followed by a colon “:” and then the value. If the value is a string, it is also inside quotes.

vehicle = {
    "type": "car",
    "brand": "Fiat",
    "model": "Punto",
    "year": 2015,
    "last_repair":{"what":"oil change", "when":2019}

Like the other complex data types, dictionaries can contain all other data types as well. You can access and change the value of an element by referring to its key. The same way you can add new keys to the dictionary.

last_time_in_garage = vehicle["last_repair"]["when"]
vehicle["model"] = "Picanto"
vehicle["color"] = "red"

Dictionaries can be a good choice for storing data of a physical object, depending on the needs of the program. Read more about sets on the W3Schools website.


Sometimes you need to create a variable such as a list or a dictionary before you will know what you store inside it. In these cases, you can create an empty variable and fill it later. This can be done by leaving the respective brackets empty. Note that there is no use of creating empty tuples. This is also why it is not possible.

list_entries = []
dictionary_entries = {}

Another thing that is good to know is that you can get the number of entries in a tuple, list, set or dictionary.

length_of_tuple = len(tuple_entries)
length_of_list = len(list_entries)
length_of_set = len(set_entries)
length_of_dictionary = len(dictionary_entries)

This is everything about the standard data types that you need to know to have a solid foundation before you start with the actual programming part. The next step is to get to know the different types of operators to start calculating in Python.

Continue learning about arithmetic expressions or go back to revisit the basic data types.