Python Tutorial
Python - Introduction Python - Syntax Python - Variable Python - Number Python - Casting Python - String Python - Operators Python - Conditions Python - Loops Python - List Python - Tuple Python - Set Python - Dictionary Python - Function Python - Module Python - Date & Time Python - Input Output Python - Error & Exception Python - File Handling
Python OOP
OOP - Introduction OOP - Class Members OOP - Constructor Destructor OOP - Data Hiding OOP - Inheritance OOP - Overriding OOP - Overloading

Python Tuple

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The main difference between the tuples and the lists is that the tuples cannot be changed unlike lists. Tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally, you can put these comma-separated values between parentheses also.

It is also possible to use the tuple() constructor to make a tuple:

Accessing Values in Tuples

To access values in tuple, use the square brackets for slicing along with the index or indices to obtain the value available at that index.

Updating Tuples

Tuples are immutable, which means you cannot update or change the values of tuple elements.

You are able to take portions of the existing tuples to create new tuples as the following example demonstrates.

Delete Tuple Elements

Removing individual tuple elements is not possible. To explicitly remove an entire tuple, just use the del keyword.

Python Built-in Tuple Functions

Function Description
len(tuple) Gives the total length of the tuple.
max(tuple) Returns item from the tuple with max value.
min(tuple) Returns item from the tuple with min value.
tuple(seq) Converts a list into tuple.

Python Built-in Tuple Methods

Method Description
tup.count(obj) Returns count of how many times obj occurs in tup
tup.index(obj) Returns the lowest index in tup that obj appears
tup.any() Checks if any Element of an Iterable is True
tup.all() returns true when all elements in iterable is true
tup.ascii() Returns String Containing Printable Representation
tup.bool() Coverts a Value to Boolean
tup.enumerate() Returns an Enumerate Object
tup.filter() constructs iterator from elements which are true
tup.iter() returns iterator for an object Applies Function and Returns a List
tup.reversed() returns reversed iterator of a sequence
tup.slice() creates a slice object specified by range()
tup.sorted() returns sorted list from a given iterable
tup.sum() Add items of an Iterable Returns an Iterator of Tuples