Post

Python tuple()

The tuple() is a built-in function of python that is used to create a tuple in python.

Python tuple()

Tuples are immutable sequences and you can learn more about tuples from here.

The syntax of tuple() function is:

1
2
tuple(iterable)

tuple() Parameters

The tuple() function takes only one parameter as argument.

  • iterable (Optional) - an iterable or collection of items like list, set, dictionary , etc.

Lets an example of tuple() function in python:

Example 1: How to use tuple() function in python?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
tuple1 = tuple()
print(tuple1)

# creating a tuple from a list
tuple2 = tuple([1, 4, 6])
print(tuple2)

# creating a tuple from a string
tuple3 = tuple('Python')
print(tuple3)

# creating a tuple from a dictionary
tuple4 = tuple({1: 'one', 2: 'two'})
print(tuple4)

Output:

1
2
3
4
5
()
(1, 4, 6)
('P', 'y', 't', 'h', 'o', 'n')
(1, 2)

Rules of tuple()

  • It will only return when only an iterable is passed.
  • It will return an empty tuple when passed with no parameters.
This post is licensed under CC BY 4.0 by the author.