KnowledgeBoat Logo

Computer Science

Which of the following will create a single element tuple ?

  1. (1,)
  2. (1)
  3. ( [1] )
  4. tuple([1])

Python Tuples

6 Likes

Answer

(1,) and tuple([1])

Reason — (1,) To create a tuple with only one element, comma needs to be added after the element, otherwise Python will not recognize the variable as a tuple and will consider it as an integer.
tuple([1]) tuple() function takes any iterable as an argument and hence when u pass a list it makes the list elements as its values.
Here, 1 is enclosed in square brackets which signifies list. When [1] is being passed as an argument in a tuple, it will generate a single element tuple with element is equal "1".

Answered By

2 Likes


Related Questions