A list is another data type in python (just like ints, strs etc.). A list is a collection of elements (also known as items). A list can hold an infinite amount of elements of different data types. We can add, remove and find certain elements in a list using something called indexes (or indices). In python a list is represented by square brackets ().
To access individual elements in our list we can do something called indexing. Each element in our list is automatically given an integer value that represents its location in the list. We start assigning indexes at the number 0. Meaning in our fruit list (see above) the element "apple" would be index 0 and the element "pear" would be index 1. We can use these integers to access certain values in the list by typing our list name, followed by square brackets () enclosing our index:
Adding and Removing Elements
Lists are a mutable data type, meaning that we can change the elements within them. To change an already existing element we can assign that elements index to equal a new value:
To add a new item to a list we can use the method .append(). Whatever is in the brackets will be added to the end of the list:
To remove an item from the list we can use the method .remove(). Whatever item is inside of the brackets will be removed from the list. We can also remove items by index using the "del" keyword:
Tuples are another data type in python. They are similar to lists except they are immutable. This means we cannot change the value of a tuple by adding, removing or changing already existing items. To create a tuple we use regular brackets "()". We separate our items by commas and like lists we can index items by using square brackets "".
For now we will not worry about tuples and their uses. We will simply acknowledge that they do exist and understand some of their basic properties.