Module 2: Python Data Structures 

Spread the love

Introduction to Detection and Incident Response

The module covers the use of data structures in Python, beginning with lists and tuples. These structures allow for the storing of groups of data in one variable and facilitate the handling and operations with data included in them.

Next, dictionaries are being discussed in this module, which present the possibility of storing data as key-value pairs, which allows the searching and manipulating of that data in a more efficient and speedy manner.

Finally, the last one would be sets. In this case, there would be explorations made even to the uniqueness of sets: being an unordered collections of equalities.

Learning Objectives

  • Understand tuple combinations and list structures and manipulate these structures.
  • Perform elementary tuple operations in Python.
  • Execute different list operations in Python.
  • Build dictionaries in a proper way with the appropriate keys and values.
  • Demonstrate the ability to manipulate dictionaries in Python.
  • Create sets in order to understand how they differ from tuples and lists.
  • Work with sets in Python, including logical and operational functions.

PRACTICE QUIZ

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

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

Definitely, the key is actually the first item separated by a colon from its value attached to it.

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]

Right, it 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’
  • ‘say’
  • ‘ what’
  • ‘you’
  • ‘will’ (CORRECT)

Correct. In this situation, the index of -1 refers to the last element in the tuple, which for this case is 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. What these indexes correspond to are tuple elements, namely, 1, 2, and 3.

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. Returns the number of items present in a tuple, the function len.

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 is used to concatenate two lists.

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

  • 6
  • 2 (CORRECT)

Correct. The addition operator is used to concatenate two lists.

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

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

Correct. The split method splits a string with respect to the specified argument into a list. For instance in this case there is no given argument, thus splits without an argument wherein spaces are used for splitting the text.

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

True, the set function transforms the list into a set before applying 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’}

Indeed, the intersection operation refers to obtaining the elements that belong to both the 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])

You’re right, index 1 signifies the second element of the tuple, which itself contains yet 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, That’s right. A[0] refers to the first nested tuple. To get the second element of that tuple, you will have to use 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

True, now that you have a 5 element tuple, the len function will return 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

Yes, there are 2 elements in the tuple. Thus len function will return 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, Indeed, A[2] refers to the third inner array. In order to retrieve that array’s solitary element, simply use index 0: 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

That’s accurate; the method retrieves the keys from the dictionary.

CONCLUSION – Python Data Structures

Finally, this module has a full understanding of the data structures in Python. Initially, you will learn all about lists and tuples, which define how to store collections of data within a single variable. Dictionaries, hence, will be learned with an understanding of how to effectively use their key-value pairs for data storage purposes.

Eventually, sets which contain only unique elements irrespective of order will then be tackled. By the end of this module, you will be equipped with requisite knowledge as well as skills in the application of these prime data structures during Python programming.

Leave a Comment