Computer Science
The syntax for a tuple with a single item is simply the element enclosed in a pair of matching parentheses as shown below :
t = ("a")
Is the above statement true? Why? Why not ?
Python Tuples
7 Likes
Answer
The statement is false. Single item tuple is always represented by adding a comma after the item. If it is not added then python will consider it as a string.
For example:t1 = ("a",)
print(type(t1)) ⇒ tuplet = ("a")
print(type(t)) ⇒ string
Answered By
5 Likes
Related Questions
If a is (1, 2, 3)
- what is the difference (if any) between a * 3 and (a, a, a) ?
- Is a * 3 equivalent to a + a + a ?
- what is the meaning of a[1:1] ?
- what is the difference between a[1:2] and a[1:1] ?
Does the slice operator always produce a new tuple ?
Are the following two assignments same ? Why / why not ?
T1 = 3, 4, 5 T2 = ( 3, 4 , 5)
2.
T3 = (3, 4, 5) T4 = (( 3, 4, 5))
What would following statements print? Given that we have tuple= ('t', 'p', 'l')
- print("tuple")
- print(tuple("tuple"))
- print(tuple)