Computer Science
What would following statements print? Given that we have tuple= ('t', 'p', 'l')
- print("tuple")
- print(tuple("tuple"))
- print(tuple)
Answer
- print("tuple") ⇒
tuple
It will simply print the item inside the print statement as it is of string type. - print(tuple("tuple")) ⇒ it will throw error.
TypeError: 'tuple' object is not callable
This is because the variable "tuple" is being used to define a tuple, and then is being used again as if it were a function. This causes python to throw the error as now we are using tuple object as a function but it is already defined as a tuple. - print(tuple) ⇒
('t', 'p', 'l')
It will return the actual value of tuple.
Related Questions
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 ?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))
How is an empty tuple created ?
How is a tuple containing just one element created ?