What are lists?
List refers to a collection of data which are normally related. Instead of storing these data as separate variables, we can store them as a list. As an example, to store the age of five users, instead of storing them as separate variables, we can make a list to store them.
Lists are not ordered vertically when output is printed, but are ordered horizontally.
Syntax
To declare a list, we write listName = [initial values]. Square brackets are used when declaring a list. Multiple values are separated by a comma.
Example
userAge = [21, 22, 23, 24, 25]
Declaring an empty list
We can also declare a list without assigning any initial values to it. We simply write listName = []. What we now have is an empty list with no initial values in it. We have to use the append() method to add values to a list.
Accessing individual values
Individual values are accessible by their indexes, and indexes always start from zero. This is a common practice in almost all programming languages. Hence the first value has an index of 0, the next value has an index of 1 and so on, such as userAge[0] = 21, userAge[1] = 22.
Alternatively, we can also access the values of a list from the back. The last item in the list has an index of -1, the second last value has an index of -2 and so on. As an example, userAge[-1] = 25, userAge[-2] = 24.
Assigning to a variable
We can also assign a list, or part of that list, to a variable. If we write userAge2 = userAge, the variable userAge2 becomes [21, 22, 23, 24, 25]
If we write userAge3 = userAge[2:4], we are assigning items with index 2 to index 3 from the list userAge to the list userAge3. In other words, userAge3 = [23, 24].
Notations
The notation 2:4 is known as a slice. Whenever we use the slice notation in Python, the item at the start index is always included, but the item at the end is always excluded. Hence the notation 2:4 refers to items from index 2 to index 4-1 (i.e. index 3), which is why userAge3 = [23, 24] and not userAge3 = [23, 24, 25].
The slice notation includes a third number known as the stepper. If we write userAge4 = userAge[1:5:2], we will get a sub list consisting of every second number from index 1 to index 4 because the stepper is 2. Hence, userAge4 = [22, 24].
In addition, slice notations have useful defaults. The default for the first number is zero, and the default for the second number is the size of the list being sliced. For example, userAge[ :4] gives values from index 0 to index 3 while userAge[1: ] gives values from index 1 to index 4 (since the list userAge has 5 items).
Modification
To modify items in a list, we write listName[index of item to be modified] = new value. For example, if we want to modify the second item, we write userAge[1] = 5. The list becomes userAge = [21, 5, 23, 24, 25].
Adding items to a list
To add items, we use the append() function. For example, if we write userAge.append(99), the list becomes [21, 5, 23, 24, 25, 99]. The value 99 is added to the end of the list.
Removing items in a list
To remove items, we write del userAge[2]. The third item is deleted and the list is now [21, 5, 24, 25, 99].
No comments:
Post a Comment