Tuples

A tuple in Python is a collection that cannot be modified. A tuple is defined using parenthesis.

A tuple with one item ( a comma is needed in the end ):

x = (1,)

A tuple with multiple items:

x = (1,2,3,4)

Accessing tuples

To access individual elements, we use square brackets. To print the first element (Python starts counting from zero):

print(x[0])

To print the second element:

print(x[1])

To print the last element, you can count from the back using the minus sign.

print(x[-1])

Command line arguments
Dictionary

2 thoughts on “Tuples

  1. Reply
    kevin - April 25, 2016

    what‘s the difference between Lists and tuples? Is the tuples similar with enumeration of C language?

    1. Reply
      admin - April 26, 2016

      Tuples cannot be modified once created. The only way to change a tuple is to create a new one and replace the existing one.

Leave a Reply

Your email address will not be published. Required fields are marked *