w3resource

Python Exercise: Convert a list to a tuple

Python tuple: Exercise-11 with Solution

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/tuple/python-tuple-exercise-11.php