Computer Science
What are different ways of creating a tuple?
Python Tuples
5 Likes
Answer
Tuples can be created by the following ways:
- By placing a sequence of values separated by comma within parentheses.
Example:tup = (1, 2, 3)
- By placing a sequence of values separated by comma without parentheses (parentheses are optional).
Example:tup = 1, 2, 3
- By using the built-in tuple type object (tuple( )) to create tuples from sequences as per the syntax given below:
T = tuple(<sequence>)
where sequence can be any type of sequence object like strings, tuples, lists, etc.
Example:tup = tuple([1, 2, 3, 4])
Answered By
4 Likes