Comprehensive Python

Table Of Contents:

Python Datatypes: Lists

The Previous tutorials, we looked at the Python String datatype.
We almost covered every piece of function concerning the Python strings and other operators.

If I remeber so well, I see we also did a little bit Lists in our geting started tutorial.
It would be much better for your mastery of this tutorial, if you have gone through it.
Now that we know how to do calculations, and manipulate strings, lets dive into another most interesting python data type, List.

Python Lists

Unlike most other programming languages, Python has got it's unique conversion for different datatypes.
Most welknown datatype for most languages, which is the Array, is not the same in Python, and it's the best comparison
between an array and a python list. In short, arrays are called lists in python.
I would start by describing a list as a set of different datatypes, separated by commas, enclosed in square brackets.
It would be Numbers, Strings, Dictionaries, Sets, Tuples and even another Lists, and in general, we do not need to declare lists.
An example of list data type would be:
>>> lists = [1,"hello",{},set(),(),[]].
Enough to get our shoes off:

Working With Lists

We have done a bang to push ourselves through the Lists, and loll, just as we did with Strings,
there are also different primary oparators we can use to get started with python Lists.
Some of them include "+", and "+=", "*" and []
The "+" oparator is used to add/concatenate a list with a new list, "+=" oparator concatenates a list with a new list, and assign the results to the original variable, the asteric(*) multiplies the repeats the list and square brackets are used for slicing.
There are also this membership operators, in, not in to use when manipulating python Lists, for checking whether a given element is in the given list.
List data type can is mutable (changeable); We can use the above mentioned oparators to work with lists as follow:
>>> lists = [1,3,9,"a"]
>>> lists+[8,"b"] # [1,3,9,"a",8,"b"]
>>> lists += ["done"] # [1,3,9,"a","done"]
>>> lists * 2 # [1,3,9,"a",1,3,9,"a"]
>>> lists[0] # 1 ie first element
The other oparators, membership oparators are special and are useful in most datatypes, so you have to take note of them.
Here is a deep explaination of them using examples:
>>> lists = [1,3,9,"a"]
>>> "a" in lists # True
>>> 3 in lists # True
>>> "p" in lists #False
>>> "p" not in lists # True
There are also generic functions that are used more rapidly in lists, but most will not be covered in here, instead, the functions tutorial will take care of that.
Some of the most used functions for lists include sum(), max(), min(), sorted().
The sum() adds all the numeric items and return their sum,
max() returns the maximum/greatest item in the list, min() returns the lowest item in the list
and sorted() returns the list sorted in ascending or descending order as advised.
>>> lists = [2,1,3,4]
>>> sum(lists) # 2+1+3+4 = 10
>>> max(lists) # 4
>>> min(lists) # 1
>>> sorted(lists) # 1,2,3,4

There are far more functions than the ones listed above, useful in lists; some of which include map, reduce ect.
Go after them, and be familiar with them before you go along:

List Functions and Methods

Now that we know what list is, and explored some of the common list functions,
we are now good to start off with the core functions of the list.
By starting the use of list functions, I witness that we are now opening the door into Python's community and it's advanced functionality. In fact, Unlike the Numbers and Strings discussed earlier in our series,
Python lists has few function entries and we will tacle them as fast as we can.
Much of the talk-talk, time to walk-walk... Let's go deep down the sea and explore the pearls.

.append() is a python list method, used to add objects to the end of a list.
The appended object can be any python's acceptable datatypes, eg Strings, Dictionaries, Numbers and even other Lists Here is a precise example of a python list.
>>> List = [1,5,2,1]
>>> List.append('man')
>>> print(List) # [1,5,2,1,'man']
>>> List.append([9,'a']) # [1,5,2,1,'man',[9,'a']]

Just as the name implies, the function clear() removes/deletes everything in the list and leave it empty. Warning: This function must be used with care because, by using this function, all the data in the list will be deleted.
>>> List = [1,5,2,1]
>>> List.append('man')
>>> print(List) # [1,5,2,1,'man']
>>> List.clear() # []

Python actually makes the description of a function by its name, as can be seen here,
the function copy() copies/duplicates everything in the list into another list. Actually, we use this function when, for instance, we want to use contents of the list while leaving the initial list unchanged.
>>> List1 = [1,5,2,1]
>>> List2 = List1.copy()
>>> print(List1) # [1,5,2,1]
>>> print(List2) # [1,5,2,1]

This function is used to calculate the occurences of a particular item in a list. The function count() takes one argument and returns how many times the argument appears in the list.
The proper usage of this function is:

>>> List = [1,5,2,1]
>>> List.count(2) # 1
>>> print(List.count(1)) # 2
>>> List.count('a') # 0

The python function extend() adds/extends a list by adding items from the iterable. Note: The iterable can be another list, a tuple, dictionary and/or set. Here is a brief usage of this fucntion.

>>> List = [1,5,2,1]
>>> List.extend(['a','b'])
>>> print(List) [1,5,2,1,'a','b']
>>> List.extend((1,3,4)) # []

This function takes a value and returns the position of that value in the list.
the function index() is most useful when the position of that item in the list is not known. The proper usage of this function is as follows.
>>> List1 = [1,5,2,1]
>>> List1.index(5) 1
>>> print(List1.index(1)) # 0
>>> print(List1.index(2)) # 2

.insert(index, object)
This function takes two arguments, the position index of that object to be inserted, and the object itself.
the function insert() is mostly used when the position of the item to be inserted into list matters. Below is the proper usage of this function in examples.
>>> List1 = [1,5,2,1]
>>> List1.insert(0,10) 1
>>> print(List1) # [10,1,5,2,1]
>>> print(List1[0:2]) # [10,1]

This function takes an index position of an item in the list and returns an item on that index.
If the index is not provided, then the item on the last index (-1) of the list will be popped and returned.
If the list is empty, then the function will raise index error.
Note : The popped item is no longer in the list (i.e it is removed).
here is how we would use this function in lists.
>>> List1 = [1,5,2,1]
>>> List1.pop() 1 -> Last Item
>>> print(List1.pop(1)) # 5 -> item on index 1
>>> [].pop() # IndexError: pop from empty list

This function takes a value and removes the first occurence of that value in a list.
Unlike pop() function, a method .remove() do not return anything.
It only removes the presence of the value in the list. If the value is not in the list, it raises ValueError.
The proper usage of this function is as follows.
>>> List1 = [1,5,2,1]
>>> List1.remove(5)
>>> print(List1.index(1)) # 2 (5 is no longer there.)
>>> List1.remove(12) # ValueError: list.remove(x): x not in the list.

What if we want to change the order of our sequence in the list? At ease! that is the job of reverse().
the function reverse() as the name tells, is the powerful weapon to reverse the order of our sequence. If we have a sequence of ascending order numbers, then we can make use of this function to reverse it to go descending.
Look at the below example:
>>> List1 = [1,2,3,4,5]
>>> List1.reverse()
>>> print(List1) # [5,4,3,2,1]

.sort(key=None, reverse=False)
Last, but not the least, is our sorting algorithm master, the sort() method.
This method is used to sort a list in ascending or descending order.
If the key is given, the key is the item used for sorting, and if the reverse is true, the items are arranged (sorted) in descending order. The proper usage of this function is as follows.
>>> List1 = [3,2,1,4]
>>> List1.sort()
>>> print(List1) # [1,2,3,4]

Despite all the methods described above, there is still one more method for dealing with lists, the del method.
The del statement can delete different items and objects in python.
here is the simple syntax for using it in the python code!

>>> List1 = [1,5,2,1]
>>> del List1[2]
>>> print(List1) # [1,2,1]

Is it not that we're done with the Lists, hahaha, yes it is! We're done with this little creature called List.
Let's now work with the Tuples.