Lists are a collection of data or the same or different data types that can be stored in a systematic part of the memory.
A way of creating an empty list in Python is by equating an identifier to square brackets
I.e
Numbers= []
To create a list
Code
Output
On other instances or occasions, you can create a list with multiple datatypes.
Code
Output
Items in the list can be accessed
This is done my specifying the nth item on the list, list numeration starts from zero
So you use print(list[0]) to print the first item on the list then print(list[1]) for the second item.
Code
Output
Along with that, you can also reassign different values in place.
Code
Output
Now that we know how to create a list we can also alter the list by using some special keywords
List.append(5)-the word append is to add more values to a list.
List.remove(2)- deletes the number 2 in the list.
Max(list)-in a list of integers finds the maximum number.
Min(list)-in a list of integers find the minimum number.
List.reverse()-reverses the list
List.sort-sorts the list.
Code
Output
Lists are very useful in programming as we will see in later lessons.
Thanks for reading, like, share, comment.
Top comments (0)