COURSE 6 – PYTHON FOR DATA SCIENCE, AI & DEVELOPMENT

Module 2: Python Data Structures 

IBM AI DEVELOPER PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

Last updated:

INTRODUCTION – Python Data Structures 

This module embarks on a journey into Python data structures, starting with an explanation of lists and tuples and their ability to store collections of data within a single variable. Following this, you will learn about dictionaries and their functionality in storing data as pairs of keys and values.

The module concludes with an exploration of Python sets, highlighting how this type of collection can appear in any order and ensures that all elements are unique.

Learning Objectives

  • Describe and manipulate tuple combinations and list data structures.
  • Execute basic tuple operations in Python.
  • Perform list operations in Python.
  • Write structures with correct keys and values to demonstrate understanding of dictionaries.
  • Work with and perform operations on dictionaries in Python.
  • Create sets to demonstrate understanding of the differences between sets, tuples, and lists.
  • Work with sets in Python, including operations and logic operations.

PRACTICE QUIZ

1. What are the keys of the following dictionary: {“a”:1,”b”:2}

  • “a”,”b” (CORRECT)
  • 1,2

Correct, the key is the first element  separated from its value by a colon.

2. Consider the following Python Dictionary:

Dict={“A”:1,”B”:”2″,”C”:[3,3,3],”D”:(4,4,4),’E’:5,’F’:6}

What is the result of the following operation: Dict[“D”]

  • 1
  • (4, 4, 4) (CORRECT)
  • [3,3,3]

Correct, this corresponds to the key ‘D’ or Dict[‘D’]

3. Consider the following tuple:

say_what=(‘say’,’ what’, ‘you’, ‘will’)

what is the result of the following say_what[-1]

  • ‘say’
  • ‘ what’
  • ‘you’
  • ‘will’ (CORRECT)

Correct. An index of -1  corresponds to the last index of the tuple, in this case, the string ‘will’.

4. Consider the following tuple A=(1,2,3,4,5), what is the result of the following:A[1:4]:

  • (2, 3, 4) (CORRECT)
  • (3, 4,5)
  • (2, 3, 4,5)

Correct. These indexes correspond to elements 1,2 and 3 of the tuple.

5. Consider the following tuple A=(1,2,3,4,5), what is the result of the following: len(A)

  • 4
  • 5 (CORRECT)
  • 6

Correct. The function len returns the number of items of a tuple.

6. Consider the following list B=[1,2,[3,’a’],[4,’b’]], what is the result of the following:B[3][1]

  •  [4,”b”]
  • “b” (CORRECT)
  • “c”

7. What is the result of the following operation

[1,2,3]+[1,1,1]

  • [2,3,4]
  • [1, 2, 3, 1, 1, 1] (CORRECT)
  • TypeError     

Correct. The addition operator corresponds to concatenating  a list.

8. What is the length of the list A = [1] after the following operation: A.append([2,3,4,5])

  • 6
  • 2 (CORRECT)

Correct. Append only adds one element to the list.

9. What is the result of the following: “Hello

  • Mike”.split()
  • [“H”]
  • [“Hello”,”Mike”] (CORRECT)
  •  [“HelloMike”]

Correct. The method split separates a string into a list based on the argument. If there is no argument as in this case the string is split using spaces.

PRACTICE QUIZ

1. Consider the following set: {“A”,”A”}, what will the result be when the set is created? 

  • {“A”,”A”}
  • {“A”}  (CORRECT)

correct, there are no duplicate values in a set 

2. What is the result of the following: type(set([1,2,3]))

  • set (CORRECT)
  • list

correct, the function set casts the list to a set before we apply the type function 

3. What method do you use to add an element to a set?

  • extend
  • append
  • add (CORRECT)

4. What is the result of the following operation : {‘a’,’b’} &{‘a’}

  • {‘a’} (CORRECT)
  • {‘a’,’b’}

Correct, the intersection operation finds the elements that are in both sets.

MODULE 2 GRADED QUIZ

1. Consider the tuple A=((11,12),[21,22]), that contains a tuple and list. What is the result of the following operation A[1] ?

  •  (11,12)
  •  [21,22] (CORRECT)
  •  ((11,12),[21,22])

correct, the index 1 corresponds to the second element in the tuple, which contains another list.

2. Consider the tuple A=((11,12),[21,22]), that contains a tuple and list. What is the result of the following operation A[0][1]?

  • 21
  • 11
  • 12 (CORRECT)

correct, A[0] corresponds to the first nested tuple; we then access the second element of the tuple using the index 1 i.e A[0][1].

3. True or false: after applying the following method, L.append([‘a’,’b’]), the following list will only be one element longer.

  • True (CORRECT)
  • False

Correct, append only adds one element to a list

4. Consider the following list : A=[“hard rock”,10,1.2]

What will list A contain after the following command is run: del(A[0]) ?

  •  [10,1.2] (CORRECT)
  • [“hard rock”,10,1.2]
  • [“hard rock”,10]

correct, we will delete element zero 

5. If A is a list what does the following syntax do: B=A[:] ?

  • assigns list A to list B
  • variable B references a new copy or clone of the original list A (CORRECT)

6. What is the result of the following: len((“disco”,10,1.2, “hard rock”,10)) ?

  • 5 (CORRECT)
  • 6
  • 0

correct, there are 5 elements in the tuple so the function len returns 5

7. Consider the following dictionary:

{ “The Bodyguard”:”1992″, “Saturday Night Fever”:”1977″}

select  the values

  • “1977” (CORRECT)
  • “1992” (CORRECT)
  • “The Bodyguard”
  •  “Saturday Night Fever”

correct this is a value

8. The variable  release_year_dict is a Python Dictionary, what is the result of applying the following method: release_year_dict.values() ?

  • retrieve the keys of the dictionary
  • retrieves, the values of the dictionary (CORRECT)

correct, this method returns the values

9. Consider the Set: V={‘A’,’B’}, what is the result of V.add(‘C’)?

  • error 
  • {‘A’,’B’}
  • {‘A’,’B’,’C’} (CORRECT)

10. What is the result of the following: ‘A’ in {‘A’,’B’} ?

  • False
  • True (CORRECT)

11. Consider the tuple A=((1),[2,3],[4]), that contains a tuple and list. What is the result of the following operation A[2] ?

  • [4] (CORRECT)
  • [2,3]
  • 1

correct, the index 2 corresponds to the third element in the tuple, which contains another list.

12. Consider the following list : A=[“hard rock”,10,1.2]

What will list A contain affter the following command is run: del(A[1]) ?

  •  [10,1.2]
  • [“hard rock”,1.2] (CORRECT)
  • [“hard rock”,10]

correct , we will delete element 1

13. What is the result of the following: len((“disco”,10)) ?

  • 2 (CORRECT)
  • 6
  • 5

correct, there are 2 elements in the tuple so the function len returns 2

14. Consider the following dictionary:

{ “The Bodyguard”:”1992″, “Saturday Night Fever”:”1977″} 

select  the keys 

  • “1992”
  • “1977”
  • “Saturday Night Fever” (CORRECT)
  • “The Bodyguard” (CORRECT)

correct, this is one of the keys

15. Consider the Set: V={‘1′,’2’}, what is the result of V.add(‘3’)?

  • {‘1′,’2’}
  • {1,2,3}
  • {‘1′,’2′,’3’} (CORRECT)

16. What is the result of the following: ‘1’ in {‘1′,’2’} ?

  • False
  • True (CORRECT)

17. What is the syntax to clone the list A and assign the result to list B ?

  • B=A
  • B=A[:] (CORRECT)

18. Consider the tuple  A=((1),[2,3],[4]), that contains a tuple and list. What is the result of the following operation A[2][0]?

  • 4 (CORRECT)
  •  [4]
  • 1

correct, A[2] corresponds to the third nested list; we then access the only element of the list using the index 0 i.e. A[2][0].

19. The method append does the following:

  • adds one element to a list (CORRECT)
  • merges two lists or insert multiple elements to a list

correct, append-only adds one element.

20. The variable  release_year_dict is a Python Dictionary, what is the result of applying the following method: release_year_dict.keys() ?

  • retrieve the keys of the dictionary (CORRECT)
  • retrieves, the values of the dictionary

correct, the method returns the keys

CONCLUSION – Python Data Structures 

In conclusion, this module provides a thorough understanding of Python data structures. You will begin by mastering lists and tuples, which allow for the storage of data collections in single variables. You will then explore dictionaries, learning how to effectively use key-value pairs for data storage.

Finally, you will delve into sets, understanding their unique property of containing only distinct elements in any order. By the end of this module, you will be well-equipped with the knowledge to utilize these fundamental data structures in Python programming.