Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Tuple

Python

python logo

In this tutorial we will learn about tuple in Python.

We briefly talked about tuple in the Python - Data Type tutorial.

What is a tuple?

Tuples are immutable ordered sequence of items similar to a List.

Once a tuple is created it can't be changed.

To create a tuple in Python we use ( ) parenthesis and separate the items using , comma.

Items of a tuple are indexed and the first item gets the index 0.

In the following example we are creating a tuple of integer values.

myTuple = (1, 2, 3)

The tuple() constructor

We use the tuple() constructor to create a tuple in Python.

# tuple
myTuple = tuple((1, 3.14, 'yusufshakeel', 1+2j, True))

print("Type of myTuple:", type(myTuple))
print(myTuple)

Output

Type of myTuple: <class 'tuple'>
(1, 3.14, 'yusufshakeel', (1+2j), True)

Accessing tuple items

Items of a tuple are indexed and the first item of the list gets index 0, the second item of the list gets index 1 and so on.

So, to access the items of a tuple we use the following syntax tuple[i].

Where, tuple represents a tuple variable and i represents the index of an item in the tuple.

In the following example we are printing item at index 1 in the given tuple.

# tuple
myTuple = (1, 3.14, 'yusufshakeel', 1+2j, True)

# output
print(myTuple[1])        # 3.14

In the following Python program we are printing all the items of a tuple using for loop

# tuple
myTuple = (1, 3.14, 'yusufshakeel', 1+2j, True)

# output
for i in myTuple:
  print(i)

We will get the following output.

1
3.14
yusufshakeel
(1+2j)
True

Adding new items to tuple

Once a tuple is created we can't add new items to the tuple.

Updating existing items of a tuple

Once a tuple is created we can't update existing items of a tuple.

Deleting existing items of a tuple

Once a tuple is created we can't delete existing items of a tuple.

Counting tuple items

We use the len method to count the total number of items present in the tuple.

In the following Python program we are printing the total number of items in the tuple.

# tuple
myTuple = (1, 3.14, 'yusufshakeel', 1+2j, True)

# output
print(len(myTuple))    # 5

Check existence of tuple item

To check if an item exists in a give tuple we use the in - membership operator.

The in operator will return True if the item exists and False if it is not present.

In the following example we are checking existence of items in a tuple.

# tuple
myTuple = (1, 3.14, 'yusufshakeel', 1+2j, True)

# output
print(1 in myTuple)               # True
print(2 in myTuple)               # False

Concat tuples

To concat two tuples we use the + operator.

In the following Python program we are concatenating two tuples.

# tuple
veg = ('Potato', 'Tomato')
fruit = ('Apple', 'Mango')

# concat
result = veg + fruit

# output
print("veg", veg)
print("fruit", fruit)
print("result", result)

We will get the following output.

veg ('Potato', 'Tomato')
fruit ('Apple', 'Mango')
result ('Potato', 'Tomato', 'Apple', 'Mango')