Python tuple() Function with Examples

tuple() Function in Python:

To create tuples in Python, use the tuple() built-in function.

A tuple is an immutable sequence type in Python. The tuple() construct is one method for generating tuples.

Syntax:

tuple(iterable)

Parameters

iterable: This is optional.An iterable (list, range, etc.) or an iterator object

  • If tuple() is not passed an iterable, the function returns an empty tuple.

Return Value: This function returns a tuple.

Examples:

Example1:

Input:

Given list = [-1, 3, 5, 6]
Given string = "good"
Given tuple = (10, 35, 45)

Output:

The result after applying tuple() function on the given list =  (-1, 3, 5, 6)
The result after applying tuple() function on the given string =  ('g', 'o', 'o', 'd')
The result after applying tuple() function on the given tuple =  (10, 35, 45)

Example2:

Input:

Given list = [10, 20, 30, 40]
Given string = "hello"
Given tuple = (1, 2, 3, 4, 5)

Output:

The result after applying tuple() function on the given list =  (10, 20, 30, 40)
The result after applying tuple() function on the given string =  ('h', 'e', 'l', 'l', 'o')
The result after applying tuple() function on the given tuple =  (1, 2, 3, 4, 5)

tuple() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the string as static input and store it in another variable.
  • Give the tuple as static input and store it in another variable.
  • Pass the given list as an argument to the tuple () function that converts a given list to a tuple and returns a tuple.
  • Store it in another variable.
  • Print the result tuple after applying the tuple() function on the given list.
  • Similarly, do the same for the given string and tuple and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [-1, 3, 5, 6]
# Give the string as static input and store it in another variable.
gvn_str = "good"
# Give the tuple as static input and store it in another variable.
gvn_tupl = (10, 35, 45)
# Pass the given list as an argument to the tuple() function that converts a given
# list to a tuple and returns a tuple.
# Store it in another variable.
rslt1 = tuple(gvn_lst)
# Print the result tuple after applying tuple() function on the given list.
print("The result after applying tuple() function on the given list = ", rslt1)
# similarly do the same for the given string and tuple and print it.
rslt2 = tuple(gvn_str)
print("The result after applying tuple() function on the given string = ", rslt2)
rslt3 = tuple(gvn_tupl)
print("The result after applying tuple() function on the given tuple = ", rslt3)

Output:

The result after applying tuple() function on the given list =  (-1, 3, 5, 6)
The result after applying tuple() function on the given string =  ('g', 'o', 'o', 'd')
The result after applying tuple() function on the given tuple =  (10, 35, 45)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the string as user input using the input() function and store it in another variable.
  • Give the tuple as user input using tuple (),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the tuple () function that converts a given list to a tuple and returns a tuple.
  • Store it in another variable.
  • Print the result tuple after applying the tuple() function on the given list.
  • Similarly, do the same for the given string and tuple and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the string as user input using the input() function and 
# store it in another variable.
gvn_str = input("Enter some random string = ")
# Give the tuple as user input using tuple(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(int, input(
   'Enter some random Tuple Elements separated by spaces = ').split()))
# Pass the given list as an argument to the tuple() function that converts a given
# list to a tuple and returns a tuple.
# Store it in another variable.
rslt1 = tuple(gvn_lst)
# Print the result tuple after applying tuple() function on the given list.
print("The result after applying tuple() function on the given list = ", rslt1)
# similarly do the same for the given string and tuple and print it.
rslt2 = tuple(gvn_str)
print("The result after applying tuple() function on the given string = ", rslt2)
rslt3 = tuple(gvn_tupl)
print("The result after applying tuple() function on the given tuple = ", rslt3)

Output:

Enter some random List Elements separated by spaces = 23 45 6 1
Enter some random string = hello
Enter some random Tuple Elements separated by spaces = 3 6 7 9
The result after applying tuple() function on the given list = (23, 45, 6, 1)
The result after applying tuple() function on the given string = ('h', 'e', 'l', 'l', 'o')
The result after applying tuple() function on the given tuple = (3, 6, 7, 9)