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
5 ★ | 1 Vote
You should read it
- Int () function in Python
- The function id () in Python
- Max () function in Python
- Zip () function in Python
- The function dir () in Python
- The ord () function in Python
- The iter () function in Python
- Exec () function in Python
- The function set () in Python
- The globals () function in Python
- The map () function in Python
- Getattr () function in Python