Lesson: Python Tupple
Well, list essentially is used to store information. List stores not
only related information, a buch of different data values can be
stored in the list and organise for later retrieval, just like array in Java,
Javascript, PHP and other programming languages; data are stored
in list and keep tract and access the list values anytime.
Values are stored in a list with each having and index number.
It is through this index that any list member
could be accessed and called for further data proccessing.
Without the index, accessing list values would not be possible.
Syntax for creating a list is:
list name = [values]
.
In the box below, let us create a book list and do some data proccessing
on it.
List of books
books = ["Biology", "Physics", "Chemistry", "Geograpy"]
Explanations:
We have created a variable called 'books'
and put its values in a list. This list contains four different
book elements which is refered to as list values - biology, physics,
Chemistry, and geography books. At this stage, we can do a lot of things
with this list, we can modify this list in many ways. We can modify
the list, we can add, delete, and update the list members;
we can also delete the entire list.
What next?:
We have created a book list above, the next is to access the list and
perform operation on its values.
To see the entire list values, we will simply say:
print(books)
This will show the content of the list like this:
['Biology','Physics','Chemistry','Geography']
>>>
But some times it is a good idea to see the content of the list i.e
list values outside the list itself, in that case we need to loop through
the list and display its contents like this:
for book in books:
print(book)
And this gives the output below:
Biology
Physics
Chemistry
Geography
>>>
Accessing Individual list member
To access any list values in python, the index number of such list value
is referenced. Values indexing in python start from 0 to the last number in
in the list. In our list of books above, four books are declared, that is,
we have four books in total. But when we are indexing in python, we will
have 0 - 3 indexing number of which the book in the first position takes
index [0] Biology, index [1] physics, index [2] Chemistry, and of course
Geography occupies the last index number in the list, which is index [3].
Let us try and access Chemistry, Chemistry is the third value in the
list, hence it occupies index of [2]. the command will be:
print (books[2])
the output of this command prduces the result below:
Chemistry
>>>