Computer Science
Which of the following will create a single element tuple ?
- (1,)
- (1)
- ( [1] )
- 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
Choose the correct statement(s).
- In Python, a tuple can contain only integers as its elements.
- In Python, a tuple can contain only strings as its elements.
- In Python, a tuple can contain elements of different types.
- In Python, a tuple can contain either string or integer but not both at a time
Which of the following is/are correctly declared tuple(s) ?
- a = ("Hina", "Mina", "Tina", "Nina")
- a = "Hina", "Mina", "Tina", "Nina")
- a = ["Hina", "Mina", "Tina", "Nina"]
- a = (["Hina", "Mina", "Tina", "Nina"])
What will be the output of following Python code?
tp1 = (2,4,3) tp3 = tp1*2 print(tp3)
- (4,8,6)
- (2,4,3,2,4,3)
- (2,2,4,4,3,3)
- Error
What will be the output of following Python code?
tp1 = (15,11,17,16,12) tp1.pop(12) print(tp1)
- (15,11,16,12)
- (15,11,17,16)
- (15,11,17,16,12)
- Error