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 Convert Celsius To Fahrenheit
Python len()
Python slice()
Python Dictionary
Python Program to Check Leap Year
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Find the Factors of a Number
Python Program Read a File Line by Line Into a List
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Print all Prime Numbers in an Interval
Python RegEx
Python Objects and Classes
Python List copy()
Python List
Python Program to Convert Kilometers to Miles
Python Set issubset()
Python staticmethod()
Python getattr()
Python bytearray()
Python for Loop
Python Dictionary copy()
Python String replace()
Python String swapcase()
Python String partition()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python memoryview()
Python Program to Slice Lists
Python Program to Shuffle Deck of Cards
Python Program to Concatenate Two Lists
Python String isspace()
Python Program to Find LCM
Python abs()