Python Data Structure Exercises

Let’s check out some exercises that will help you understand structured data types in Python better, also usually referred to as composite data types as well.

Exercise 5-a

Let's create an empty list and print its type.


list() is a function that creates an empty list. You don’t need to put anything inside list() function to achieve this. The same thing can be achieved by typing brackets: []

type() function will tell you the class type of your variable.

gift_list = []
answer_1 = type(gift_list)

Exercise 5-b

Now, we will create an empty dictionary and print its type.


dict() is a function that creates an empty list. You don’t need to put anything inside dict() function to achieve this.

The same thing can be achieved by typing curly brackets: {}

type() function will tell you the class type of your variable.

grocery_items = {}
answer_2 = type(grocery_items)

Exercise 5-c

Then we will create an empty tuple and print its type.


tuple() is a function that creates an empty list. You don’t need to put anything inside tuple() function to achieve this.

type() function will tell you the class type of your variable.

bucket_list = ()
answer_2 = type(bucket_list)

Now let’s see some more exercises about converting data types.

Exercise 5-d

Let's create a list with values in it.


list format is [] with values in it which can be separated by commas.

gift_list = ['socks', '4K drone', 'wine', 'jam', 'pajamas']

Exercise 5-e

Now let's create a dictionary with values in it.


dictionary format is {} with keys and values in it. This is usually referred as a key-value pair as well. Each key will have a colon after it which is followed by the value of the key.

grocery_list = {'banana': 4, 'milk': 2, 'bread': 1}

Exercise 5-f

Finally, let's create a tuple with values in it.


tuple format is (). It’s very similar to a list with the exception of being immutable, unlike lists.

bucket_list = ('learn Python',  'go to space', 'scuba diving', 'climb Everest')