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 strftime()
Python Program to Create a Long Multiline String
Python String isdigit()
Python Program to Differentiate Between type() and isinstance()
Python Program to Transpose a Matrix
Python File I/O Operation
Python Matrices and NumPy Arrays
Python Program to Convert Kilometers to Miles
Python getattr()
Python Statement, Indentation and Comments
Python Program to Measure the Elapsed Time in Python
Python List Comprehension
Python Dictionary copy()
Python Program to Randomly Select an Element From the List
Python oct()
Python Operator Overloading
Python Package
Python sorted()
Python List extend()
Python compile()
Python Program to Check if a Number is Odd or Even
Python format()
Python Program to Sort a Dictionary by Value
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String expandtabs()
Python min()
Python Set issubset()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python String splitlines()
Python Program to Merge Two Dictionaries
Python Program to Get the Last Element of the List
Python Modules