Table of Contents
The tuple() builtin can be used to create tuples in Python.
In Python, a tuple is an immutable sequence type. One of the ways of creating tuple is by using the tuple()
construct.
The syntax of tuple()
is:
tuple(iterable)
1. tuple() Parameters
- iterable (optional) – an iterable (list, range, etc.) or an iterator object
If the iterable is not passed to tuple()
, the function returns an empty tuple.
2. Example: Create tuples using tuple()
t1 = tuple() print('t1 =', t1) # creating a tuple from a list t2 = tuple([1, 4, 6]) print('t2 =', t2) # creating a tuple from a string t1 = tuple('Python') print('t1 =',t1) # creating a tuple from a dictionary t1 = tuple({1: 'one', 2: 'two'}) print('t1 =',t1)
Output
t1 = () t2 = (1, 4, 6) t1 = ('P', 'y', 't', 'h', 'o', 'n') t1 = (1, 2)
Recommended reading: Python Tuples
Related posts:
Python Program to Get the Last Element of the List
Python List remove()
Python Set update()
Python Decorators
Python pass statement
Python Tuple index()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Set symmetric_difference()
How to get current date and time in Python?
Python String format_map()
Python Program to Measure the Elapsed Time in Python
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Get a Substring of a String
Python String istitle()
Python String rfind()
Python Statement, Indentation and Comments
Python Program to Find Factorial of Number Using Recursion
Python for Loop
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Set difference_update()
Python Set intersection()
Python delattr()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python pow()
Python bytearray()
Python Dictionary popitem()
Python Program Read a File Line by Line Into a List
Python Program to Merge Two Dictionaries
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python String rindex()
Python String casefold()
Python oct()