Module 2: Python Data Structures

Looking for ‘python for data science ai & development module 2 answers‘?

In this post, I provide accurate answers and detailed explanations for Module 2: Python Data Structures of Course 4: Python for Data Science, AI & Development IBM Generative AI Engineering Professional Certificate

Whether you’re preparing for quizzes or brushing up on your knowledge, these insights will help you master the concepts effectively. Let’s dive into the correct answers and detailed explanations for each question.

Practice Quiz: Lists and Tuples

Practice Assignment

1. Consider the following tuple:

say_what=('say',' what', 'you', 'will')

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

  • ‘you’
  • ‘ what’
  • ‘will’ ✅
  • ‘say’

Example:

  • Negative indexing in Python starts from the end of the tuple.
  • say_what[-1] refers to the last element of the tuple, which is 'will'.

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

  • (2, 3, 4,5)
  • (1, 2, 3, 4)
  • (2, 3, 4) ✅
  • (3, 4,5)

Explanation:

  • Slicing a tuple [start:end] extracts elements from index start to end - 1.
  • For A[1:4], it extracts elements from index 1 to 3 (values 2, 3, 4).

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

  • 4
  • 5 ✅
  • 6

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

  • “a”
  • “2”
  • “b” ✅
  • [4,”b”]

Explanation:

  • B[3] accesses the fourth element of the list, which is [4, 'b'].
  • [4, 'b'][1] retrieves the second element of this sublist, which is 'b'.

5. What is the result of the following operation?

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

  • [1, 2, 3; 1, 1, 1]
  • [1, 2, 3, 1, 1, 1] ✅
  • TypeError
  • [2,3,4]

Explanation:

  • The + operator concatenates two lists in Python.
  • [1, 2, 3] + [1, 1, 1] produces a single list with all the elements combined: [1, 2, 3, 1, 1, 1].

6. After operating A.append([2,3,4,5]), what will be the length of the list A = [1]?

  • 5
  • 10
  • 2 ✅
  • 6

Explanation:

  • The .append() method adds the argument as a single element (a sublist) to the end of the list.
  • The list becomes: [1, [2, 3, 4, 5]].
  • The length of the list is now 2 (the first element is 1, and the second is [2, 3, 4, 5]).

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

  • 2 ✅
  • 6
  • 5

8. What is the result of the following: "Hello Mike".split()

  • [“HelloMike”]
  • [“Hello”,”Mike”] ✅
  • [“H”]

Practice Quiz: Dictionaries

Practice Assignment

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

  • a, b
  • 1,2
  • “a”,”b” ✅
  • {“a”,”b”}

Explanation:

  • The keys in the dictionary {"a": 1, "b": 2} are 'a' and 'b'.
  • So, the keys are 'a', 'b'.

10. 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"]

  • [3,3,3]
  • (4, 4, 4) ✅
  • ‘4, 4, 4’
  • 1

Explanation:

  • The dictionary contains the key "D" which has the value (4, 4, 4) (a tuple).
  • Accessing Dict["D"] retrieves the tuple (4, 4, 4).

11. Which of the following is the correct syntax to extract the keys of a dictionary as a list?

  • list(keys(dict))
  • dict.keys().list()
  • keys(dict.list())
  • list(dict.keys()) ✅

Explanation:

  • The correct syntax to extract the keys of a dictionary as a list is list(dict.keys()).
  • This method returns the keys in a list format.

Practice Quiz: Sets

Practice Assignment

12. Consider the following set: {"A","A"}, what will the result be when the set is created?

  • {}
  • {“A”} ✅
  • {“A”, “A”}
  • {“A”, “B”}

Explanation:

  • Sets in Python automatically remove duplicate elements.
  • If you create a set {"A", "A"}, the result will be {"A"} because sets only store unique elements.

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

  • set ✅
  • list

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

  • Append
  • Insert
  • Add ✅
  • Extend

Explanation:

  • To add an element to a set, you use the .add() method.
  • Methods like .append(), .insert(), or .extend() are used for lists, not sets.

15. What is the result of the following operation : {'a','b'} &{'a'}

  • {‘b’}
  • {}
  • {‘a’,’b’}
  • {‘a’} ✅

Explanation:

  • The & operator is used to find the intersection of two sets, i.e., elements that are common in both sets.
  • For {'a', 'b'} & {'a'}, the common element is 'a'.

Module 2 Graded Quiz: Python Data Structures

Graded Assignment

16. 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] ✅
  • [2,3]
  • 1
  • 3

Explanation:
A[2] accesses the third element of the tuple, which is [4].

17. 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 ✅
  • 22

Explanation:

  • A[0] refers to the first element of the tuple, which is (11, 12).
  • Within this nested tuple, [1] accesses the second element, which is 12.

18. If L = [‘c’, ‘d’], then the output of the statement `L.append([‘a’, ‘b’])` is:

  • [[‘a’, ‘b’], ‘c’, ‘d’]
  • [‘c’, ‘d’, [‘a’, ‘b’]] ✅
  • [‘a’, ‘b’, ‘c’, ‘d’]
  • [‘c’, ‘d’, ‘a’, ‘b’]

Explanation:

  • The append() method adds the argument as a single element to the end of the list.
  • The resulting list becomes ['c', 'd', ['a', 'b']].

19. Consider the following list: A=["hard rock",10,1.2]

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

  • [10,1.2]
  • [“hard rock”,1.2] ✅
  • [“hard rock”,10]
  • Syntax error

Explanation:

  • del(A[1]) deletes the element at index 1, which is 10.
  • The resulting list becomes ["hard rock", 1.2].

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

  • List A gets converted to a set and is loaded to B
  • Creates a new reference variable B that points to a copy or clone of the original list A ✅
  • B gets a transposed form of the list A
  • Assigns list A to list B

Explanation:

  • [:] creates a shallow copy of the list A.
  • B becomes a new list that is a clone of A and does not share memory with A.

21. The method append does the following:

  • adds one element to a list ✅
  • merges two lists or insert multiple elements to a list

22. 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] ✅
  • [“hard rock”,10,1.2]
  • [“hard rock”,10]

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

  • B=A
  • B=A[:] ✅

24. What is the result of the following: len(("disco",10,1.2, "hard rock",10)) ?

  • 5 ✅
  • 6
  • 0
  • 7

Explanation:

  • The len() function counts the number of elements in a tuple.
  • The tuple has 5 elements: "disco", 10, 1.2, "hard rock", and 10.

25. Consider the following dictionary:

{ "The Bodyguard":"1992", "Saturday Night Fever":"1977"}

select the values

  • “1977” ✅
  • “1992” ✅
  • “The Bodyguard”
  • “Saturday Night Fever”

Explanation:

  • The values of the dictionary are "1992" and "1977".
  • The keys are "The Bodyguard" and "Saturday Night Fever", but they are not part of the answer.

26. The variable release_year_dict is a Python dictionary, what is the outcome of applying the following method? release_year_dict.keys()

  •  Retrieves the values of the dictionary
  • Retrieves the entire contents of the dictionary
  • Retrieves the keys of the dictionary ✅
  • Changes the dictionary to a list

Explanation:

  • The keys() method retrieves the keys of a dictionary as a view object.
  • For example, given {"A": 1, "B": 2}, the result would be dict_keys(['A', 'B']).

27. 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 ✅

28. Consider the set: V={'1','2'}, what is the result of V.add('3')?

  • Error
  • {‘1′,’2′,’3’} ✅
  • {‘1′,’2’}
  • {1,2,3}

Explanation:

  • The add() method adds an element to the set if it is not already present.
  • After the operation, the set becomes {'1', '2', '3'}.

29. What is the result of the following? 'A' in {'A','B'}

  • True ✅
  • 0
  • 1
  • False

Explanation:

  • The in operator checks if 'A' is a member of the set {'A', 'B'}.
  • Since 'A' is present, the result is True.

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

  • error
  • {‘A’,’B’}
  • {‘A’,’B’,’C’} ✅

31. What is the result of the following: '1' in {'1','2'} ?

  • False
  • True ✅

Related contents:

You might also like:

Leave a Reply