Installation Program_Execution Data_Structures Data_Input Data_Handling Functions Data_Manipulation

Goal

Get an overview of the most important data structures in Python.

Additional Information

Here you can find additional information on ...


Get the background

  • Read "Chapter 2.2.1: Python Datatypes" of my book Hands-on Signal Analysis with Python
  • It is important that you know the difference between list, tuple, and np.array. In addition, you will frequently have to work with the types dictionary and pd.DataFrame.

Tuples, Lists, and Arrays

Tuples

Tuples are collections of different things. Once created, tuples cannot be modified. They are mainly used for returning multiple items from a function. One convenient feature: you can assign multiple values simultaneously using tuples.

  • Generate a tuple my_tuple = ('high', 5)
  • Can you change 5 to 7?
  • Explain the result.

Lists and Arrays

  • Generate two lists, with the values [1, 2, 3] and [5, 7, 11]. In addition, generate two NumPy arrays with the same values.
  • Add the two lists together; and then add the two arrays together.
  • Explain your results.

Dictionaries and Pandas DataFrames

Dictionaries

  • Create a dictionary, which contains the translations of the words "trump", "is", "an", "idiot" into your native language.
  • Add a (key/value), containing the string 'pi' as key, and the corresponding float-value as the value.
  • Now create a second dictionary called data. Assign the key t to the numbers from zero to 6*pi, in steps of 0.1. Then assign the key sine to the corresponding sine-values.
  • Convert data into the corresponding DataFrame with df = pd.DataFrame(data), and plot the DataFrame with df.plot().
  • Inspect df.

Solution