Python Exercise: Convert a list to a tuple
11. Convert a List to a Tuple
Write a Python program to convert a list to a tuple.
Visual Presentation:

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:

For more Practice: Solve these Related Problems:
- Write a Python program to convert a list of mixed data types into a tuple using the tuple() function.
- Write a Python program to accept a list from the user and output it as a tuple.
- Write a Python program to create a tuple from a list and then print the type of the resulting object.
- Write a Python program to demonstrate converting a list to a tuple and then performing tuple operations.
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.