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 String casefold()
Python range()
Python Program to Find Numbers Divisible by Another Number
Python Program to Catch Multiple Exceptions in One Line
Python help()
Python String encode()
Python Program to Merge Mails
Python callable()
Python Dictionary keys()
Python break and continue
Python Set union()
Python Program to Transpose a Matrix
Python Program to Get the Full Path of the Current Working Directory
Python String find()
Python List count()
Python del Statement
Python Program to Create Pyramid Patterns
Python String lstrip()
Python Keywords and Identifiers
Python for Loop
Python Program to Display Calendar
Python Dictionary pop()
Python Deep Learning Cookbook - Indra den Bakker
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python String istitle()
Python Iterators
Python String expandtabs()
Python id()
Python Program to Count the Number of Each Vowel
Python int()
Python Program to Delete an Element From a Dictionary
Python Set pop()