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 Concatenate Two Lists
Python Program to Convert Bytes to a String
Python Dictionary update()
Python help()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python compile()
Python zip()
Python Program Read a File Line by Line Into a List
Python String encode()
Python Set difference_update()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python String isspace()
Python callable()
Python List Comprehension
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python bool()
Python Custom Exceptions
Python Set discard()
Python Program to Find the Size (Resolution) of a Image
Python Machine Learning - Sebastian Raschka
Python String zfill()
Python Program to Sort Words in Alphabetic Order
Python id()
Python Program to Shuffle Deck of Cards
Python List remove()
Python Program to Display Fibonacci Sequence Using Recursion
Python Type Conversion and Type Casting
Python String join()
Python exec()
Python Program to Represent enum
Python map()
Python Set issubset()