Python Create a Tuple with Example

Published on Mar 16, 2022

A tuple is a collection of immutable and ordered Python objects. It can hold elements of any arbitrary data type (integer, string, float, list, etc.). The difference between the two is that we cannot change the elements of a tuple whereas we can change the elements of a list.

A tuple can be created by putting different comma-separated values between parentheses(). You can also create a tuple without putting items in parentheses too.

# empty tuple
empty_tuple = ()
print(empty_tuple)

# without parentheses
wp_tuple = ('python', 'shouts')
print(wp_tuple)

# without parentheses
wop_tuple = 'python', 'shouts'
print(wop_tuple)

# different datatypes
ddt_tuple = (1.5, "Hi", 3, -10)
print(ddt_tuple)

# nested
nested_tuple = ("Hello", [1, 2, 3], (1, 5, 7))
print(nested_tuple)

Outputs:

()
('python', 'shouts')
('python', 'shouts')
(1.5, 'Hi', 3, -10)
('Hello', [1, 2, 3], (1, 5, 7))