What is tuple() Function in Python

Share Your Love

The tuple() is function called built-in function in python that can be used to create a tuple().

A tuple is an immutable sequence type.

Syntax Of tuple():


Parameters Of tuple():

This function accept single parameter iterable(optional). It is iterable of list, range… etc. or an iterator object. So if an iterable is passed through function then the corresponding tuple is created. If Iterable is not passed, then an empty tuple() is created.

Returns: It doesn’t return anything but creates a tuple.

Error And Exception:

It generates a TypeError, if an iterable is not passed through function.

Below programs illustrate tuple() function in Python:

Program 1: Program demonstrating the use of tuple() function

# Python3 program demonstrating
# the use of tuple() function

# when parameter is not passed
tuple1 = tuple()

# when an iterable(e.g., list) is passed
list1= [ 1, 2, 3, 4 ]
tuple2 = tuple(list1)

# when an iterable(e.g., dictionary) is passed
dict = { 1 : 'one', 2 : 'two' }
tuple3 = tuple(dict)

# when an iterable(e.g., string) is passed
string = "geeksforgeeks"
tuple4 = tuple(string)


(1, 2, 3, 4)
(1, 2)
('g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's')

Program 2: Program demonstrating the TypeError

# Python3 program demonstrating
# the TypeError in tuple() function

# Error when a non-iterable is passed
tuple1 = tuple(1)


Traceback (most recent call last):
  File "/home/eaf759787ade3942e8b9b436d6c60ab3.py", line 5, in 
TypeError: 'int' object is not iterable

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about this topic discussed above.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter