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:

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

Value returned from tuple ()

  1. If no parameters are passed, tuple () returns an empty tuple.
  2. 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

May be interested

  • The function set () in PythonThe function set () in Python
    in this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
  • Help () function in PythonHelp () function in Python
    the built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
  • Sum () function in PythonSum () function in Python
    the built-in function sum () in python returns the sum of all numbers in iterable.
  • The slice () function in PythonThe slice () function in Python
    the slice () function in python returns a slice object that helps you determine how to cut an existing string.
  • The chr () function in PythonThe chr () function in Python
    in 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 dictionaryPython data type: string, number, list, tuple, set and dictionary
    in 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 PythonThe iter () function in Python
    continuing 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 in Python
    the 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 in Python
    the reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
  • Exec () function in PythonExec () function in Python
    the exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.