Chapter 5 - Core concepts of containers

In the next chapters, we will introduce the most important containers in the Python language, which are lists, sets, tuples, and dictionaries. However, before we can introduce them, it's important that we introduce some things that they all share, which is hence the goal of this chapter.

At the end of this chapter, you will be able to understand the following concepts:

  • positional arguments
  • keyword arguments
  • mutability

If you want to learn more about these topics, you might find the following links useful:

If you have questions about this chapter, please refer to the forum on Canvas.

1. Containers

When working with data, we use different python objects (which we summarize as 'containers') to order data in a way that is convenient for the task we are trying to solve. Each of the following container types has different advantages for storing and accessing data (which you will learn about in the following chapters):

  • lists
  • tuples
  • sets
  • dictionaries

Each of the container types can be manipulated using different methods and functions, for instance allowing us to add, access or remove data. In order to use those functions and methods efficiently, it is important to understand some key concepts of how to use them.


In [ ]:
# Some examples (you do not have to remember this now):

a_list = [1,2,3, "let's", "use", "containers"]
a_tuple = (1, 2, 3, "let's", "use", "containers")
a_set = {1, 2, 3, "let's", "use", "containers"}
a_dict = {1:"let's",  2:"use", 3: "containers"}

#print(a_list)
#print(a_tuple)
#print(a_set)
#print(a_dict)

2. Positional arguments (args) and keyword arguments (kwargs)

Args and kwargs are basically used to specify what a function or method is supposed to do. Therefore, a good understanding of the terms args and kwargs is important for the use of functions and methods in Python. Let's look at some string method examples from the last topic:


In [ ]:
a_string = 'hello world'
print('example 1. upper method:', a_string.upper())
print('example 2. count method:', a_string.count('l'))
print('example 3. replace method:', a_string.replace('l', 'b'))
print('example 4. split method:', a_string.split())
print('example 5. split method:', a_string.split(sep='o'))

'l' in example 2 is a positional argument. sep='o' in example 5 is an example of a keyword argument. Let's analyze the examples.

example method positional arguments (args) keyword arguments (kwargs)
1 upper 0 0
2 count 1 0
3 replace 2 0
4 split 0 0
5 split 0 1

This might look a bit confusing, because sometimes methods have positional arguments and/or keyword arguments and sometimes they do not. Luckily Python has a built-in function help, which provides us insight into how to use each method.


In [ ]:
help(str.upper)

we learn that str.upper takes no positional arguments and no keyword arguments (nothing between the parentheses) and returns a string (-> str).


In [ ]:
help(str.count)

we learn that str.count takes one positional argument (sub) and returns an integer (-> int). You can ignore the information between square brackets for now.


In [ ]:
help(str.replace)

we learn that str.replace takes two positional arguments (old and new) and no keyword arguments. It returns a string (-> str).


In [ ]:
help(str.split)

Now it becomes interesting. str.split has no positional arguments and two keyword arguments (sep and maxsplit). The method returns a list of strings.

3. Difference between positional arguments (args) and keyword arguments (kwargs)

  • Positional arguments (args) are compulsory in order to call a method.
  • Keyword arguments (kwargs) are optional. They can be optional since they usually have a default value. By using the keyword argument, you simply change the default value to another value.

For example, if we call a method that needs a positional argument without any, we get an error:


In [ ]:
a_string = 'hello world'
a_string.count()

However, if we do not provide a value for keyword arguments, we do not get an error:


In [ ]:
a_string = 'hello world'
a_string.split()

4. Mutability

Hopefully, it will become clear in the following chapters what we mean by mutability. For now, you can think of it in terms of 'can I change the data?'. Please remember the following categories for the subsequent chapters:

immutable mutable
integer list
string set
dictionary

Exercises

Please find some exercises about core concepts of python containers below.

Exercise 1:

Use the help function to figure out what the string methods below are doing. Then analyze how many positional and keyword arguments are used in the following examples:


In [ ]:
print(a_string.lower())
print(a_string.strip())
print(a_string.strip('an'))
print(a_string.partition('and'))

Exercise 2:

Please illustrate the difference between ARGS and KWARGS using the example of string methds. Feel free to use dir(str) and the help function for inspiration


In [ ]:
# your examples here