Read the text and answer the questions.

The biggest advantage of using a Python list over a tuple is that lists can be changed after you initialize them. It is possible to add and remove items from the list or change items that are already in the list. In this lesson, we'll cover some common list and tuple management functions that you can use in your own code.

Functions That Work on Any List or Tuple
You are familiar with calling functions like print() and passing in parameters inside the parentheses. Many other built-in Python functions exist to do different jobs. There are three functions that will accept a list or tuple as an input parameter and tell you the number of items in the list or find the largest or smallest value in the list.

Function Description>
len(<list or tuple>) Returns the integer number of items in the input list or tuple.
max(<list or tuple>) Returns the largest value found in the input list or tuple
min(<list or tuple>) Returns the smallest value found in the input list or tuple

Because these functions do not modify the contents, they will work on both lists and tuples. The example below declares a list of integers and a tuple of string values. Try running the code below to see sample output for each function!The meaning of "max" and "min" is obvious when considering numeric input. But what does finding the "max" or "min" value means when looking at strings? By default, Python will use alphabetical ordering (like you would find in a dictionary) to find the minimum and maximum string values.

Functions That Work on a Specific List
Once you declare a specific list like myGrades or mySiblings, you can call several additional Python functions using that list variable name. In your code, you would write the variable name, then a dot (.), and then the function name. For example, "myGrades.append()" would be used to call the append() function on the list stored in the myGrades variable.

Function Description>
append(<value to add>) Adds a new value to the end of the list.
insert(<index>,<value to add>) Adds a new value into the list before the specified index. Using 0 as the index will insert the value at the very beginning of the list.
remove(<value to remove>) Deletes the first value in the list that matches the input. If value is not found, a ValueError exception will be thrown at run-time!
pop(<index>) Deletes the value at the specified index, and returns that value so you can do something with it.
clear() Deletes all of the items in the list, leaving it empty.
index(<value to find>) Searches the list and returns the numeric index of the first element where the input value is found. A ValueError exception will be thrown at run-time if the value is not found!
count(<value to count>) Returns the number of times that the input value was found in the list.
sort()
sort(None,False) Changes the order of items in the list so they are sorted from smallest to largest. If you add "None" and "False" parameters, the list will be sorted from largest to smallest instead.
reverse() Reverses the order of the values in the list.

Only two of these functions, index() and count() will also work on tuples because they do not change the contents of the tuple. All of the other functions result in some change to the list and will work only on lists.

The example below declares a list called fruityFlavors that holds several string values. We then practice calling each of the above functions on the list and print out the results. Study the code carefully and then run it. Do you get the expected output on each line?Try changing the value to find for remove() or index() to something like "Blueberry" which is not in the list. You should get a ValueError exception when that function is run.

Traceback (most recent call last):
File "code1.py", line 7, in <module>
fruityFlavors.remove("Blueberry")
ValueError: list.remove(x): x not in list
Remember that exceptions will halt your program when it is running. So, you should carefully avoid making function calls that will cause exceptions at run-time.

Copying List Contents
In Python, you cannot copy the contents of one list to another with a simple assignment statement. In the example below, the assignment of dogBreeds1 to dogBreeds2 merely makes the dogBreeds2 and dogBreeds1 variables share the same list in memory. What will happen when you assign a new value to dogBreeds1[0] and then print both lists?Both dogBreeds1 and dogBreeds2 appear to have been changed by the assignment statement to the first element in dogBreeds1. That's because the dogBreeds1 and dogBreeds2 variables are pointing to the same list, so any change to elements through either variable will impact that one list in memory.

To make a true copy of a list, you can call the copy() function on a list variable. Then, assign the results of that function call to your new list variable. Try changing the "dogBreeds2 = dogBreeds1" line as follows:

dogBreeds2 = dogBreeeds1.copy() # make a copy of the first list and assign that copy to the second list
Copy
Now, when you run the program, the update to the first element in the first list will only impact dogBreeds1. The dogBreeds2 list holds a separate list that is not changed.

['Foxhound', 'Collie', 'Terrier', 'Beagle']
['Poodle', 'Collie', 'Terrier', 'Beagle']
The list copy() function makes a "shallow" copy of a list, which works well when you need to copy simple data like numbers and strings. However, copy() will not make a complete copy of more complex elements like lists of lists or lists of objects.
Students taking the AP Computer Science Principles exam will encounter pseudocode like "aList ← bList" that assigns one list to another. This assignment should be treated as a true copy operation that stores an independent copy of the right-hand list into the left-hand variable.
Introducing the "in" Keyword
When working with lists, tuples, or strings, you can use the "in" keyword to ask whether a particular value is present in that list, tuple, or string. The logical expression "<value> in <list, tuple or string>" will return a Boolean True or False.

For example, the following code declares a list, and then asks if the value "Cherry" is in the list using "in". We store the result in a Boolean variable.

fruityFlavors = ["Cherry","Banana", "Grape", "Orange"]
isCherryPresent = "Cherry" in fruityFlavors # should be True
Copy
Similarly, you could use "in" to ask if a letter or sub-string was present in a string. The two lines below check the word "Cherry" to see if the letter "q" or the sub-string "rr" is present in the word.

result1 = "q" in "Cherry" # should be False
result2 = "rr" in "Cherry" # should be True
Copy
Of course, you can use the "in" keyword to form a logical expression in an "if" statement as well. You don't need to store the result in a variable.

if ("rr" in "Cherry"): # should be True, so this "if" block will be run
Copy
Try running the sample code below and see the results for yourself! You can change the "in" expressions to produce True or False values based on the data in your target list or string.The "in" keyword can also be used iterate through each element in a list, as you will see in the next lesson.

1. Why would you need to store a group of items in a single variable?
2. How do you create a list in Python?
3. What kinds of data can you store in lists?
4. Does each element need to be the same data type?
5. How do you print list contents?
6. How do index values work to access specific list elements?
7. For any given list size, what is the minimum and maximum valid index value?
8. How do ranges allow you to read a sequence of list elements?
9. When using a range, does the output contain the element at the ending index?
10. How do you declare a tuple instead of a list?
11. What is the biggest difference between a tuple and a list?

Respond in shorter sentences.

1. Storing a group of items in a single variable allows for easier management and manipulation of the items as a whole.

2. To create a list in Python, you use square brackets [] and separate the items with commas.
3. Lists can store any type of data, including numbers, strings, booleans, and even other lists or objects.
4. No, each element in a list can be a different data type.
5. You can print list contents by using the print() function and passing the list variable as an argument.
6. Index values work by specifying the position of an element in the list. The first element is at index 0, the second element is at index 1, and so on.
7. For any given list size, the minimum valid index value is 0, and the maximum valid index value is (list size - 1).
8. Ranges allow you to specify a sequence of index values to access multiple list elements.
9. No, when using a range, the output does not include the element at the ending index.
10. To declare a tuple in Python, you use parentheses () instead of square brackets [].
11. The biggest difference between a tuple and a list is that a list is mutable, meaning you can change its elements, while a tuple is immutable, meaning its elements cannot be changed after it is created.