Tuple () function in Python
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
May be interested
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- Python data type: string, number, list, tuple, set and dictionaryin this section, you'll learn how to use python as a computer, grasp python's data types and take the first step towards python programming.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.