w3resource

Python Exercise: Create a tuple


1. Create a Tuple

Write a Python program to create a tuple.

Sample Solution:

Python Code:

# Create an empty tuple and assign it to the variable 'x'
x = ()
# Print the contents of the 'x' tuple, which is empty
print(x)

# Create an empty tuple using the tuple() constructor and assign it to the variable 'tuplex'
tuplex = tuple()
# Print the contents of the 'tuplex' tuple, which is also empty
print(tuplex) 

Sample Output:

()                                                                                                            
()

Flowchart:

Flowchart: Create a tuple

For more Practice: Solve these Related Problems:

  • Write a Python program to create a tuple containing a mix of numbers, strings, and booleans.
  • Write a Python program to generate an immutable tuple from a given list of values.
  • Write a Python program to create a tuple by reading comma-separated input from the user and then print its type.
  • Write a Python program to create a tuple using tuple() and verify its immutability by attempting (and failing) to change an element.

Python Code Editor:

Previous: Python Tuple Exercise Home
Next: Write a Python program to create a tuple with different data types.

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.