In this tutorial, we are going to learn all about the Python tuple function which is a part of python’s built-in functions. tuple() function in Python is used to convert an iterable into a tuple. Before going through this article, we will see a brief description of Python tuple.
Headings of Contents
What is Python Tuple?
In Python, Tuple is a collection of items that are ordered and unchangeable or immutable. Python tuple is written inside the circular bracket ( ). Tuple in Python also accepts duplicate members or items.
Example:
students = ("Vishvajit", "Raju", "Harsh", "Harshita", "Vinay", "Harry")
# print the students
print(students)
# print tupe of tuple
print("Type of students", type(students))
Output
('Vishvajit', 'Raju', 'Harsh', 'Harshita', 'Vinay', 'Harry')
Type of students <class 'tuple'>
Python tuple function?
Python tuple() is a built-in function in Python that is used to create a tuple. In Python, a tuple is an immutable sequence of types. which is not changeable once it has been created.
Syntax:
tuple(iterable)
Parameter:
tuple() function supports a parameter iterable ( list, set, string, etc ), Which is optional. If the iterable not passed into the tuple() function, then tuple() function return the empty tuple.
Python tuple() function example
Here we will go through some examples to understand the tuple function in Python.
Example: Creating a tuple from a list
students = ["Vishvajit", "Raju", "Harsh", "Harshita", "Vinay", "Harry"]
# print the students
print(students)
# creating tuple from list
print("Tupl is:- ", tuple(students))
Output
['Vishvajit', 'Raju', 'Harsh', 'Harshita', 'Vinay', 'Harry']
Tuple is:- ('Vishvajit', 'Raju', 'Harsh', 'Harshita', 'Vinay', 'Harry')
Example: Creating a tuple from a set
students = {"Vishvajit", "Raju", "Harsh", "Harshita", "Vinay", "Harry"}
# print the students
print("Set is:- ", students)
# creating tuple from set
print("Tuple is:- ", tuple(students))
Output
Set is:- {'Harshita', 'Raju', 'Vishvajit', 'Harsh', 'Vinay', 'Harry'}
Tuple is:- ('Harshita', 'Raju', 'Vishvajit', 'Harsh', 'Vinay', 'Harry')
Example: Creating a tuple from a string
name = "Python"
# creating tuple from string
print("Tuple is:- ", tuple(name))
Output
Tuple is:- ('P', 'y', 't', 'h', 'o', 'n')
Example: Creating a tuple from an integer
You can’t create a tuple from an integer value because a tuple takes iterable and an integer is not iterable. You will get TypeError when you will pass any integer value in the tuple() function.
x = 125
print(tuple(x))
Output
Traceback (most recent call last):
File "C:\Users\Vishvajit Rao\OneDrive\Desktop\Pyspark\main.py", line 29, in <module>
print(tuple(x))
TypeError: 'int' object is not iterable
Conclusion
So, In this article, we have learned all about the tuple functions in Python along with examples. tuple function is the best python built-in function to convert any iterable into a tuple. If you like this article, please share and keep visiting for further Python tutorials.
Python built-in functions
For more information:- Click Here
Thanks for your valuable time… 🙏🙏❤️❤️