Diving Into Python: Chapter 11

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:

  • Tuples are not dynamic like lists, here dynamic means tuples can't be changed, unlike lists.
  • Tuples uses small brackets, whereas lists uses square brackets.

(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.


  • Tuples | Update

    As I said earlier, tuples are not dynamic (we can't change tuple values). But there is always an alternate or different way to do a certain operation in any programming language and it is in Python.

    In Python one can take portions of existing tuples to create new tuples. In simple words you can call it concatenation of two tuples. Let's have a look how.

    Example

    tup1 = ('.NET', 'HTML5', 'Python');
    tup2 = "C#", "CSS", "Django";

    # Creating a new Tuple
    tup3 = (tup1 + tup2);
    print (tup3)


    Output



  • Tuple | Delete

    In Python you can never delete a single tuple, so you need to always delete a complete tuple. To delete a tuple in Python we use the del statement.

    Example

    tup = ('.NET', 'HTML5', 'Python');

    del (tup)
    print (tup)


    Output



    You will get an error message, like this:

  • Tuple | Length

    A tuple length can be defined using len, the same as we use do for strings and in several other programming languages.

    Example

    a = len ('C# Corner')
    print (a)
    Output: 9


  • Tuple | Repitition

    To repeat an entire tuple or a tuple value any number of times, we do use tuple repetition. This can be done by using "*".

    Example

    1. Repetitions of an entire tuple:

      a = ("Goodbye World!")*2
      print (a)


      Output

      Goodbye World!Goodbye World!
    2. Repetitions of a certain value of tuple:

      a = ("Goodbye World!", "Hello World!" *2)
      print (a)

      Output

      ('Goodbye World!', 'Hello World!Hello World!')

  • Tuple | Membership

    This operations works somewhat like Boolean. It checks or verifies the values in a certain tuple then the result is either true or false.

    Example

    1. Condition- True

      a = (1, 2, 3, 4)
      b = 3 in a
      print (b)


      Output: True
    2. Condition- False

      a = (1, 2, 3, 4)
      b = 5 in a
      print (b)


      Output: False

    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

  • Do as much as code you can
  • Code anything you want, the best way to learn
  • Don't just study things, try to learn them
  • Work on your concepts
  • Work on the fundamentals of any technology or stuff you want to learn

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!

Up Next
    Ebook Download
    View all
    Learn
    View all