Jump Start with Python Tuples

Tuples are immutable lists. In Python, tuples can be written as comma separated values (items) enclosed between parentheses irrespective of their type. Tuples are similar to lists only difference is that it cannot be changed in any way once it is created. This is example of simple tuple in Python:

Tuples Basic Example
Tuples Basic Example

Creating Tuples

It is very easy to create a tuple in Python, just enclose comma separated values (items) between parentheses.
Syntax: tuple_name=(item1, item2, item3, ---- , itemn)

Some facts about tuples:

  • Declaring zero and one element tuple:If empty parentheses are assigned to a variable then it is termed as zero element tuple.
    Syntax: tuple_name=()

    One element tuple is declared in Python using the following syntax:
    Syntax: tuple_name=("item1", )

  • In Python any set of elements which are declared as comma separated values (items) are considered as tuples.
  • Indexes for tuples start from 0. Positive indexes start from left to right while negative indexes start from right to left.
  • Python also supports slicing index in Tuples which allows accessing multiple elements of tuple at once. Below is an example for slicing in tuples.

Basic Operations in Tuples

  • Packing & Unpacking
  • Deletion Operation
  • Addition Operation
  • Multiplication Operation
  • Iteration Operation
  • Membership Operation

Tuple Methods

  • Length Method
  • Convert to Tuple Method
  • Maximum Method
  • Minimum Method
  • Membership Operation

My article “Jump Start with Python – Part 7 (Tuples)” is the seventh article in series, and explains how to get started with programming in Python at C# Corner. Click the link below to read the article:

Read Article

To read “Jump Start with Python Numbers – Part 6 (Lists)“, Click Here

Leave a Reply

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