<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Chapter 6 - Lists
This notebook uses code snippets and explanations from this course.
As we have learned before, strings and integers have one value in them. When we put a new value in them, the old value is overwritten. In this chapter, we will have a first look at containers. A container allows us to put many values in a single box. A container is nice because we can carry lots of values around in one convenient package. It is very straightforward to store a list of items as a container. Not surprisingly, we call this a list.
At the end of this chapter, you will be able to
Step1:  Square brackets surround lists, and commas separate the elements in the list
A list element can be any Python object - even another list
A list can be empty
Step2:  Please note that there are two ways of creating an empty list.
Step3:  2.  How to add items to a list
The most common way of adding an item to a list is by using the append method.
Let's first look at the help message of this method to understand it better
Step4:  We learn that append has one positional parameter object. Remember that you should ignore 'self' (it just means that append is a list method). append returns None. It might be a bit confusing at first that a list method returns None. Please carefully look at the difference between the two following examples. Please predict what will be printed in each code snippet below
Step5:  The reason why the first example is the correct one is that lists are mutable, which means that you can change the contents of a list. You can hence change the items in a list without assigning it to a new variable.
In comparison, when we want to change an immutable object like a string, the method returns a copy of the input
Step6:  3. How to extract/inspect items in a list
Please note that indexing and slicing work the same way as with strings. You have already seen indexing for the characters in a string. Every item in the list has hence its index number. We start counting at 0! The indices for our
list ['John', 'Bob', 'Marry'] are as follows
Step7:  We can hence use this index number to extract items from a list (just as with strings).
Step8:  Obviously, we can also use negative indices
Step9:  And we can extract one part of a list using slicing
Step10:  If you insert an index that is higher than what is present in the list, you will get an IndexError
Step11:  Two additional methods are useful for inspecting lists
Step12:  The count method has one positional parameter value and returns an integer. As the name already indicates, the method returns how often the value occurs in the list.
Step13:  The index method has one positional parameter value, and two keyword parameters, and returns the first index of the value. It is hence similar to the count method, but now the first index is returned of the value instead of the count.
Step14:  We get a ValueError when the value is not in the list.
Step15:  4. Basic List Operations
Python allows at least the following very useful list operations
Step16:  The '*' sign makes it possible to repeat a list
Step17:  Of course, you can use lists in membership boolean expressions
Step18:  And you can use lists in comparison boolean expressions
Step19:  5. Use built-in functions on lists
Python has a range of functions that operate on lists. We can easily get some simple calculations done with these functions
Step20:  6. An overview of list methods
There are many more methods which we can perform on lists. Here is an overview of some of them.
To get used to them, please call the help function on each of them (e.g., help(list.insert)). This will give you the information about the positional parameters, keyword parameters, and what is returned by the method.
Step21:  In order to have a complete overview of all list methods, you can use the dir built-in function
Step22:  Exercises
Exercise 1
Step23:  Exercise 2
Step24:  Exercise 3
Step25:  Exercise 4
Step26:  Exercise 4
<ASSISTANT_TASK:>
Python Code:
friends = ['John', 'Bob', 'Mary']
stuff_to_pack = ['socks','shirt','toothbrush']
print(friends)
print(stuff_to_pack)
Explanation: Chapter 6 - Lists
This notebook uses code snippets and explanations from this course.
As we have learned before, strings and integers have one value in them. When we put a new value in them, the old value is overwritten. In this chapter, we will have a first look at containers. A container allows us to put many values in a single box. A container is nice because we can carry lots of values around in one convenient package. It is very straightforward to store a list of items as a container. Not surprisingly, we call this a list.
At the end of this chapter, you will be able to:
* create a list
* add items to a list
* extract/inspect items in a list
* perform basic list operations
* use built-in functions on lists 
If you want to learn more about these topics, you might find the following links useful:
* Python documentation
* Tutorial on lists
* Another tutorial on lists
If you have questions about this chapter, please us (cltl.python.course@gmail.com).
1. How to create a list
It's straightforward to create a list. Please look at the following examples:
End of explanation
#list of integers
print([1, 24, 76])
#list of strings
print(['red', 'yellow', 'blue'])
#mixed list
print(['red', 24, 98.6])
#list with a list included
print([1, [5, 6], 7])
#empty list
print([])
Explanation: Square brackets surround lists, and commas separate the elements in the list
A list element can be any Python object - even another list
A list can be empty
End of explanation
one_way = []
print(one_way)
another_way = list()
print(another_way)
Explanation: Please note that there are two ways of creating an empty list.
End of explanation
help(list.append)
Explanation: 2.  How to add items to a list
The most common way of adding an item to a list is by using the append method.
Let's first look at the help message of this method to understand it better:
End of explanation
a_list = [1, 3, 4]
a_list.append(5)
print(a_list)
a_list = [1, 3, 4]
a_list = a_list.append(5)
print(a_list)
Explanation: We learn that append has one positional parameter object. Remember that you should ignore 'self' (it just means that append is a list method). append returns None. It might be a bit confusing at first that a list method returns None. Please carefully look at the difference between the two following examples. Please predict what will be printed in each code snippet below:
End of explanation
help(str.replace)
a_string = 'hello'
a_string.replace('l', 'b')
print(a_string) # a_string has not been changed
a_string = 'hello'
a_new_string = a_string.replace('l', 'b')
print(a_string)
print(a_new_string)
Explanation: The reason why the first example is the correct one is that lists are mutable, which means that you can change the contents of a list. You can hence change the items in a list without assigning it to a new variable.
In comparison, when we want to change an immutable object like a string, the method returns a copy of the input:
End of explanation
fruit_basket = ['apple', 'banana', 'cherry']
fruit = 'banana'
print(fruit_basket[1])
print(fruit[1])
Explanation: 3. How to extract/inspect items in a list
Please note that indexing and slicing work the same way as with strings. You have already seen indexing for the characters in a string. Every item in the list has hence its index number. We start counting at 0! The indices for our
list ['John', 'Bob', 'Marry'] are as follows:
John|Bob|Mary
---|---|--|
0|1|2|
-3|-2|-1
You can access elements in a list just as you can access characters in a string by using the index.
End of explanation
friend_list = ['John', 'Bob', 'Marry']
print(friend_list[0])
print(friend_list[1])
print(friend_list[2])
Explanation: We can hence use this index number to extract items from a list (just as with strings).
End of explanation
friend_list = ['John', 'Bob', 'Marry']
print(friend_list[-2])
Explanation: Obviously, we can also use negative indices:
End of explanation
friend_list = ['John', 'Bob', 'Marry']
list_with_fewer_friends = friend_list[:2]
print(list_with_fewer_friends)
Explanation: And we can extract one part of a list using slicing:
End of explanation
print(friend_list[5])
Explanation: If you insert an index that is higher than what is present in the list, you will get an IndexError:
End of explanation
help(list.count)
Explanation: Two additional methods are useful for inspecting lists:
* count
* index
End of explanation
friend_list = ['John', 'Bob', 'John', 'Marry', 'Bob']
number_of_bobs = friend_list.count('Bob')
print(number_of_bobs)
friend_list = ['John', 'Bob', 'John', 'Marry', 'Bob']
number_of_franks = friend_list.count('Frank')
print(number_of_franks)
help(list.index)
Explanation: The count method has one positional parameter value and returns an integer. As the name already indicates, the method returns how often the value occurs in the list.
End of explanation
friend_list = ['John', 'Bob', 'John', 'Marry', 'Bob']
first_index_with_john = friend_list.index('Bob')
print(first_index_with_john)
Explanation: The index method has one positional parameter value, and two keyword parameters, and returns the first index of the value. It is hence similar to the count method, but now the first index is returned of the value instead of the count.
End of explanation
friend_list = ['John', 'Bob', 'John', 'Marry', 'Bob']
friend_list.index('Frank')
Explanation: We get a ValueError when the value is not in the list.
End of explanation
one_list = ['where', 'is']
another_list = ['the', 'rest', '?']
print(one_list + another_list)
Explanation: 4. Basic List Operations
Python allows at least the following very useful list operations:
* concatenation
* repetition
* membership
* comparison
The '+' sign concatenates two lists:
End of explanation
a_list = ['Hello', 'world']
print(a_list * 3)
Explanation: The '*' sign makes it possible to repeat a list:
End of explanation
life = ['a', 'lot', 'of', 'stuff']
print('meaning' in life)
Explanation: Of course, you can use lists in membership boolean expressions
End of explanation
print([3, 2] == [2, 3])
print([2,3] == [2,3])
Explanation: And you can use lists in comparison boolean expressions
End of explanation
nums = [3, 41, 12, 9, 74, 15]
print(len(nums)) # number of items in a list
print(max(nums)) # highest value in a list
print(min(nums)) # lowest value in a list
print(sum(nums)) # sum of all values in a list
Explanation: 5. Use built-in functions on lists
Python has a range of functions that operate on lists. We can easily get some simple calculations done with these functions:
End of explanation
#define some lists and variables
a = [1,2,3]
b = 4
c = [5,6,7]
x = 1
i = 2
#do some operations 
a.append(b)     # Add item b to the end of a
a.extend(c)     # Add the elements of list c at the end of a
a.insert(i,b)   # Insert item b at position i
a.pop(i)        # Remove from a the i'th element and return it. If i is not specified, remove the last element
a.index(x)      # Return the index of the first element of a with value x. Error if it does not exist
a.count(x)      # Return how often value x is found in a
a.remove(x)     # Remove from a the first element with value x. Error if it does not exist
a.sort()        # Sort the elements of list a
a.reverse()     # Reverses list a (no return value!)
print(a)
Explanation: 6. An overview of list methods
There are many more methods which we can perform on lists. Here is an overview of some of them.
To get used to them, please call the help function on each of them (e.g., help(list.insert)). This will give you the information about the positional parameters, keyword parameters, and what is returned by the method.
End of explanation
dir(list)
Explanation: In order to have a complete overview of all list methods, you can use the dir built-in function:
End of explanation
# your code here
Explanation: Exercises
Exercise 1:
Create an empty list and add three names (strings) to it using the append method
End of explanation
friend_list = ['John', 'Bob', 'John', 'Marry', 'Bob']
#  your code here
Explanation: Exercise 2:
Please count how many times John occurs in the list
End of explanation
friend_list = ['John', 'Bob', 'John', 'Marry', 'Bob']
# your code here
Explanation: Exercise 3:
Please use a built-in function to determine the number of strings in the list below
End of explanation
friend_list = ['John', 'Bob', 'John', 'Marry', 'Bob']
# your code here
Explanation: Exercise 4:
Please remove both John names from the list below using a list method
End of explanation
a_string = 'hello'
a_list = ['hello']
# your code here
Explanation: Exercise 4:
Please add world to the string and the list below. Only create a new object if it is necessary.
End of explanation
<END_TASK>