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 Count the Occurrence of an Item in a List
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Input, Output and Import
Python Program to Compute the Power of a Number
Python Program to Find the Largest Among Three Numbers
Python Dictionary setdefault()
Python List insert()
Python Program to Add Two Matrices
Python abs()
Python Program to Get the Last Element of the List
Python Set remove()
Python bool()
Python List sort()
Python Iterators
Python Program to Print all Prime Numbers in an Interval
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Set add()
Python String isalnum()
Python Tuple index()
Python String expandtabs()
Python String zfill()
Python Program to Generate a Random Number
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Calculate the Area of a Triangle
Python input()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python String split()
Python Program to Find LCM
Python ord()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...