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 Shallow Copy and Deep Copy
Python Program to Check If a String Is a Number (Float)
Python int()
Python Program to Check Whether a String is Palindrome or Not
Python Program to Find the Largest Among Three Numbers
Python eval()
Python String isprintable()
Python hash()
Python Program to Create a Countdown Timer
Python Objects and Classes
Python Set union()
Deep Learning in Python - LazyProgrammer
Python if...else Statement
Python Program to Make a Simple Calculator
Python Program Read a File Line by Line Into a List
Python List append()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Set copy()
Python String isdigit()
Python enumerate()
Python Program to Split a List Into Evenly Sized Chunks
Python Get Current time
Python Program to Parse a String to a Float or Int
Python Program to Multiply Two Matrices
Python Operator Overloading
Python Program to Convert Bytes to a String
Python Dictionary popitem()
Python Program to Find ASCII Value of Character
Python break and continue
Python format()
Python chr()
Introduction to Scientific Programming with Python - Joakim Sundnes