TipsMake

Tuple () function in Python

The built-in function tuple () in Python is used to create a tuple.

In Python, tuple is an immutable sequence. The built-in function tuple () in Python is used to create a tuple.

The syntax of the tuple () function in Python

 tuple(iterable) 

Parameter of tuple function ()

The tuple () function in Python has parameters:

  • iterable : iterable objects are built-in (like list, string, dict) or iterator object.

Value returned from tuple ()

  • If no parameters are passed, tuple () returns an empty tuple.
  • If the iterable parameter is passed, tuple () returns the corresponding tuple.

Example: How to create tuple with tuple ()?

 t1 = tuple() 
print('t1=', t1)

# tạo một tuple từ list
t2 = tuple([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
print('t2=', t2)

# tạo một tuple từ string
t1 = tuple('Quantrimang')
print('t1=',t1)

# tạo một tuple từ dictionary
t1 = tuple({1: 'one', 2: 'two'})
print('t1=',t1)

Run the program, the result is:

 t1= () 
t2= (2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
t1= ('Q', 'u', 'a', 'n', 't', 'r', 'i', 'm', 'a', 'n', 'g')
t1= (1, 2)

See also: Built-in Python functions

Discover more

python function python function available python document learn python
Lesley Montoya

Share by

Lesley Montoya
Update 25 May 2019