Python Lists
Creating and Accessing:
- A list is a sequence of values (a.k.a. items) which can be used like variables (similar to an array in Java)
- To access the value in a list, you put the array name followed by the slot position containing the value, where 0 is the first slot
- If you pass a list as a function parameter value, changes made to the list within the function will remain changed out of the function
- A list can contain values of differing types, for example myList = [1,2,'apple','orange']
Copying and Slicing:
- If you don't put a number before the ':', it will start at index 0
- If you don't put a number after the ':', it will get the values until the end of the List
- If you just leave a ':', it will copy all of the values
Adding to a list:
- List A will become [1,2,3,9,3,7]
- List C will be [1,2,3,9,3,7,2,4,6]
Removing values from a list:
- myList.pop(1) also gets the value that was removed, you can store it in a variable
- If no index is specified for pop(), it will remove last value by default
Sorting:
- This will put them in numerical order for numbers (or abc order for text strings)
Other:
Challenge
Make a list of 10 strings and a list of 5 strings, write code that modifies the first item and displays the last item. Run the code once for each list.