Hello guys!
This is the 11th part of the article series "Python". In this article series, you will learn Python step-by-step and easily.
Getting Theme
For getting the theme of Python, kindly go through my previous articles:
Tuples
A tuple can be defined as “A sequence of immutable Python objects.”
In simpler words, a Tuple can be defined as a sequence and it can be of any type (either homogeneous or heterogeneous) based on functionality and the requirements of the program, just like lists in Python.
Pin Points
As I said above in this article, tuples are similar to lists. So is there any difference between them?
Yes, there is a catch, let's have a look:
(For more info regarding lists, kindly visit my previous article.)
Creating a Simple Tuple
You can represent a tuple as:
tup1 = (‘.NET’, ‘HTML5’, ‘Python’);
or
tup1 = “.NET”, “HTML5”, “Python”;
or
tup1 = (1, 2, 3, 5, 8);
or # An empty tuple
tup1 = ();
Special Case:
Assume you want to put only one element into a tuple. Be careful, because there is a catch. Let's have a look.
Tup1 = (1, );
As you can see, there is still a comma, in spite of being only one element in the list.
Accessing Tuples
To access the values in the tuple, we use square brackets. That square bracket will access the index to obtain the value available at that index.
Example
# Defining Tuples
tup1 = ('.NET', 'HTML5', 'Python');
tup2 = "C#", "CSS", "Django";
tup3 = (1, 2, 3);
# Accessing Tuples
print (tup1[1])
print (tup2[0])
print (tup3[2])
Output
Operations on Tuple
Now to explain some of the basic operations on tuples.
Built-in Tuple functions
Here are some built-in tuple functions.
  1. Tuple | Compare
    This built-in function is used for comparing the two tuples. It compares them in terms of there values.
    Example
    tuple1 = ('CSK', 180)
    tuple2 = ('MI', 175)
    print (cmp(tuple1, tuple2))
    print (cmp(tuple2, tuple1))
    Output
    -1, 1
  2. Tuple | Length
    As the name of this built-in function suggests, it is used for getting the length of any tuple value from a tuple.
    Example
    tup = len ('C# Corner')
    print (a)
    Output: 9
  3. Tuple | Maximum
    As the name of this built-in function suggests, it is used for fetching the max tuple value or max tuple from the given set of tuples and values.
    Example
    tup1 = ('DOTNET', 'JAVA')
    tup2 = ('PYTHON', 'C')
    print ("Max value : ", max(tup1))
    print ("Max value : ", max(tup2))
    print ("Max value :", max(tup2, tup1))
    Output
  4. Tuple | Minimum
    As the name of this built-in function suggests, it is used for fetching the min tuple value or min tuple from the given set of tuples and values.
    Example
    tup1 = ('DOTNET', 'JAVA')
    tup2 = ('PYTHON', 'C')
    print ("Min value : ", min(tup1))
    print ("Min value : ", min(tup2))
    print ("Min value :", min(tup2, tup1))
    Output
Guidelines from my Side
Moto
“Keep calm and code Python”.
I tried to make this an interesting and interactive article and wish you guys will like that, meanwhile if you have any suggestions then your welcome.
Until the next part, keep sharing!