Upon instantiation of a list, all elements contain values from the initializer list (i.e., the list of values inside the square brackets). If no initializer list is provided, the list is empty.
If the list is an expression WITH square brackets, then check if there is a colon in the square brackets. If there is a colon inside of the square brackets, it is a Slicing Multiple Values from a List. Otherwise, it is an Accessing List Element.
If adding a value elsewhere in the list, use the insert method to add the new value at the specified index. Existing values starting from that index are shifted to the right.
Determine the list that is being iterated over. If the expression also involves a range(len(list_name)), then the list is being traversed by index. The range function can either take one argument (the length of the list) or 3 arguments (the starting index, ending index, and step size). If the range function takes one argument, it will start at 0 and go to the length of the list - 1. If the range function takes 3 arguments, it will start at the first argument and go to the second argument - 1, incrementing by the third argument. Otherwise, if range is not used, then the list is being traversed by value.
Determine the loop control variable that is being used to iterate over the list. The loop control variable will take on each value or index in the list, one at a time, depending on whether we are iterating by value or by index.
The loop control variable is used to access the list element in the body of the loop. If iterating by index, the loop control variable is used as an index to access or update the list element. If iterating by value, the loop control variable is used directly to access the list element (no updates are possible).
The list can also be added to with the append or insert methods. The append method adds a new value to the end of the list, while the insert method adds a new value at the specified index. Existing values starting from that index are shifted to the right.
When calling a function, put variable name that represents the list as an argument in the method call. Remember that when passing a list as an argument that changes made by the function to the list are persistent. The list itself is not copied, so the function does not have its own copy of the list. However, the one exception to this is if you assign the argument to reference a different list in memory; then you will no longer be modifying the original list.
Q20: Put the following code in order to create a program that will declare and instantiate an list of 10 random values between 1 and 100 and then find the maximum value in the list.
import random
---
arr = []
---
for i in range(10):
arr.append(random.randint(1, 100))
print("arr["+str(i)+"] is " + str(arr[i]))
---
max = arr[0]
---
for i in range(1, len(arr)):
---
if arr[i] > max:
---
max = arr[i]
---
print("Maximum value is", max);