+ Reply Thread
#21
...
#22
Lists
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 "<pyshell#33>", line 1, in <module>
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



These are the functions i commonly use, i'll address more(if necessary) as we go on.
#23
Tuples
This data type is much like a list, the major distinguishing factor of a Tuple is that there are Immutable.

For example:
#A python list:
>>> peeps =
#Could be changed to:
>>> peeps[1] = 'Phyno'
>>> print(peeps)


The illustration above shows how mutable list are.


Tuple values are separated with comma and enclosed usually with a parenthesis.
Example:
langs = ('Rust', 'Matlab', 'Julia', 'Python', 'Go-lang')

Tuple methods:
Indexing methods with list can also be done with tuples.
Example:
>>> print(langs[2])
Julia


However you can't change tuple values:
So langs[2] = 'Java' would throw an error.

Tuple-List Conversion.
You could convert a list to a tuple.
Illustration:
>>> peeps =
>>> tup_peep = tuple(peeps)
>>> print(type(tup_peep))
<class 'tuple'>

The type() function is used to return the type of an object or variable.
#24
you are doing a good job here, please keep it up!                         
#25
On your first post, how do we open IDLE sir
#26

@ensodev

This is long over due. Been mostly offline for a while.

Hopefully you've resolved this already.

Install python and IDLE should be in your start menu if you using windows.


Nice work on notifications @dhtml

#27

Thanks my friend, the notification thing suffered us a bit. No easy way to keep track of stuffs earlier.

#28
Window button and search for IDLE, if you are on window 10
#29

Hmn, you seem to be a windows expert

Back to top