w3resource

Python Exercise: Convert a list to a tuple


Write a Python program to convert a list to a tuple.

Visual Presentation:

Python Tuple: Convert a list to a tuple.

Sample Solution:

Python Code:

# Create a list containing a sequence of numbers
listx = [5, 10, 7, 4, 15, 3]
# Print the contents of the 'listx' list
print(listx)

# Use the 'tuple()' function, a built-in Python function, to convert the 'listx' list to a tuple
tuplex = tuple(listx)
# Print the contents of the 'tuplex' tuple
print(tuplex)

Sample Output:

[5, 10, 7, 4, 15, 3]                                                                                          
(5, 10, 7, 4, 15, 3)

Flowchart:

Flowchart: Convert a list to a tuple

Python Code Editor:

Previous: Write a Python program to check whether an element exists within a tuple.
Next: Write a Python program to remove an item from a tuple.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.