Comprehensive Python

Table Of Contents:

Python Datatypes: Tuples

Welcome Guys to Our Fourth Lesson On data type series... This is a bread for those who have done the Previous tutorials.
This tutorial ill introduce you to the next new concept on data types, the tuple.

Trying to scratch my head, the statement that keeps blemishing my head is that we explored a little bit the concept of tuples in our Get Started tutorial.
We will however squeeze this concept far away from the current boundary, and we are going to explore it in it's completeness!
The verses below will make an utmost effort to dismantle tuple fragment-by-fragment... here we go!

A Tuple is a collection of various data, enclosed in braces ().
Unlike List, tuple data are within opening and closing braces as (1, 5, 'a').
The tuple data can be, just like list, any other data types, i.e Lists, Numbers, Strings, Dictionaries, Sets, and Even another Tuples.
The other difference, distinguishing python Tuples and Lists is that tuples are immutable,
which stands that the Tuple data maintain there form without being altered.
The example below will surely render myself clear on this phrase:
>>> Lists  = [1, 2, 3]
>>> Tuples = (1, 2, 3)
>>> Lists[2] = 4 # Correct, no error!
>>> Tuples[2]= 4 # TypeError: 'tuple' object does not support item assignment

Working With Tuples

The past tutorials has seen the rising of Strings and better enough, the Lists.
just as we did with Strings, and Lists, there are also different primary oparators we can use when working with Tuples.
The most common of which may include "+", and "+=", "*", "*=" and []
The "+" oparator is used to add/concatenate a tuple with another tuple, "+=" oparator concatenates a tuple with another tuple, and assign the results to the original variable, the asteric(*) multiplies or repeats the tuple,
(*=) does the same as (+=) but it repeats the tuple instead, and square brackets are used for slicing.
There are also this membership operators, in, not in to use when manipulating python Tuples, for checking whether a given element is in the given tuple.
List data type can is immutable (unchangeable); We can use the above mentioned oparators to work with tuples as follow:
>>> Tuples = (1,3,9,"a")
>>> Tuples+(8,"b") # (1,3,9,"a",8,"b")
>>> Tuples += ("done",) # (1,3,9,"a","done")
>>> Tuples * 2 # (1,3,9,"a",1,3,9,"a")
>>> Tuples[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:
>>> Tuples = (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 Tuples 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.
>>> Tuples = (2,1,3,4)
>>> sum(Tuples) # 2+1+3+4 = 10
>>> max(Tuples) # 4
>>> min(Tuples) # 1 >>> sorted(Tuples) # 1,2,3,4

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

Tuple Functions and Methods

Even though it is more strict in python, unless we dive deeper, python tuples has got few functions than Lists, and other data types... Without taking much of our precious time, let's take a look at them immediately, so we look on other stuff.
This function is used to calculate the occurences of a particular item in an iterable, in this case Tuple. The function count() takes one argument and returns how many times the argument appears in the Tuple.
Just like with the lists, The proper usage of this function is:

>>> Tuple = (1,5,2,1)
>>> Tuple.count(2) # 1
>>> print(Tuple.count(1)) # 2
>>> Tuple.count('a') # 0

This function takes a value and returns the position of that value in the tuple.
the function index() is most useful when the position of that item in the list is not known. You may also reference with Lists The proper usage of this function is as follows.

>>> Tuple = (1, 5, 2, 1)
>>> Tuple.index(5) 1
>>> print(Tuple.index(1)) # 0
>>> print(Tuple.index(2)) # 2

Ladies and Gentlemen, that marks the end of our tuple tutorial, and now, let's take a look at the python Dictionaries.