dhtml

So far so good. nice ride. Just some little typing mistakes.Vote up! Vote down! …

Mon 18 Dec, 2017 02:56 pm
So far so good. nice ride. Just some little typing mistakes.Vote up! Vote down!Points: 0You voted ‘up’Top Wed, 11/22/2017 - 22:41(Reply to #16)#174kingsFestus Thanks. :)
I'll proof read the write-up again, i've been very busy that's the reason for the delay in the tutorial.Vote up! Vote down!Points: 0You voted ‘up’Top Thu, 11/30/2017 - 00:06(Reply to #17)#18FestusYou're good boss.
I love your work.


I am hoping I will one day become smart and learned enough to serve your likes and programming needs of the greater populace.Vote up! Vote down!Points: 0You voted ‘up’Top Thu, 11/23/2017 - 05:36(Reply to #18)#194kings...Vote up! Vote down!Points: 0You voted ‘up’Top Thu, 11/23/2017 - 17:03#204kingsLists
List function in python is denoted as [].
This square brackets contains all elements of the list, separated with comma(,).
For example:

>>> mylist = [ 'Lagos', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']

That's an example of list assignment.

To get length or Number of items in a list.

>>> len(mylist)
5
I haven't discussed this, but len() function can also be used to get the length of a string too.

List Indexing
We can index position of elements of a list in python.
Python's indexing start from 0.
Therefore index 0 in the list created above is 'Lagos'
Illustration:

>>> mylist[0]
'Lagos'
>>> mylist[1]
'Ogun'

So we've seen how list are indexed.
More to indeixng, we can also index a list from last position without counting the number of elements by using -1.
Example:

>>> mylist[-1]
'Zamfara'
Therefore:
>>> mylist[-2]
'AkwaIbom'

List Slicing
The concept behind list slicing is to cut out and return part of a list.
Using [start:stop] method, where start is the value to index from and stop is the value to stop at.
For example:

>>> mylist = [ 'Lagos', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> mylist[0:3]


Notice that index 3 is 'AkwaIbom' but the return didn't include this.
The concept behind that is that python start slicing your list from your first index and stop just before your last index. In this case our first and last index is 0 and 3 respectively.

Also we can add an increment parameter to the list to increment how elements in the list are to be sliced
So we use [start:stop:increment]

>>> mylist[0:-1:2]

So we can see the list is meant to start at index 0 and stop at the last index[-1] but we added an increment parameter which is to increase the slice of element by 2.
Therefore, when it starts at index 0, it goes to the second element, and does same for the next and so on depending on the length of the list and the stop parameter.

Mutability of List:
This simply means that list element can be changed once created.
For example:

>>> mylist =[ 'Lagos', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> mylist[0] = 'Borno'
>>> print(mylist)
[ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
We can see above how we changed the first element of the list from 'Lagos' to 'Borno'

We can also change element slices of the list at the same time.
For example:

>>> mylist = [ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> mylist[1:3] = 'Jos', 'Kwara'
>>> mylist
[ 'Borno', 'Jos', 'Kwara', 'AkwaIbom', 'Zamfara']

List elements can be of multiple data types.
For example:

>>> mylist = [1000, 'Nigeria', 9.11, ['Dhtml', 'Festus', 'Seun']]
>>> print(mylist)
[1000, 'Nigeria', 9.11, ['Dhtml', 'Festus', 'Seun']]

We can also see that our list can have a sub-list too.

Now let's look at some List functions:
min
This function returns the minimum value of elements in the list. And is of this format==> min(list)
Example:

>>> mylist = [300,699,2,91,419, 1]
>>> min(mylist)
1

>>> mylist2 = [ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> min(mylist2)
'AkwaIbom'

We can also see that for string, min finds the minimum alphabetically.

However we can't use min for different data types.
For example:

>>> mylist = [1000, 'Nigeria', 9.11, ['Dhtml', 'Festus', 'Seun']]
>>> min(mylist)

Traceback (most recent call last):
File "", line 1, in
min(mylist)
TypeError: unorderable types: str() < int()


We would discuss errors later on in this tutorial.

append
This is another function of python's list. It's function is to add item to an already existing python list.
For example:

>>> mylist = [ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> mylist.append('Kogi')
>>> print(mylist)
[ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara', 'Kogi']


extend
Extend function is similar to append, but extend adds a list or an iterable item to an existing list.

>>> mylist = [ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara', 'Kogi']
>>> east = [ 'Ebonyi', 'Anambra', 'Abia']
>>> mylist.extend(east)
>>> print(mylist)
[ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara', 'Kogi', 'Ebonyi', 'Anambra', 'Abia']


sort
As the name implies, this sorts an element in a list.
For example:

>>> mylist = [300,699,2,91,419, 1]
>>> mylist.sort()
>>> print(mylist)
[1, 2, 91, 300, 419, 699]

reverse
This reverse items in a list.
Example:

>>> mylist = [300,699,2,91,419, 1]
>>> mylist.reverse()
>>> print(mylist)
[1, 419, 91, 2, 699, 300]

count
This function counts number of times an item appears in a list.

>>> mylist = [1,2,4,5,1,5,2,4,5,8,3,]
>>> mylist.count(1)
2