What would be the output of the following? Python Data Handling
As found in PriP 1.2, Data Handling. Class 12 Computer Science with Python by Sumita Arora is referred to in schools affiliated to but not limited to CBSE.
Q8. What would be the output of the following?
Statement | Output | |
---|---|---|
(a) | print(type([1,2])) | <class 'list'> |
(b) | print(type(1,2)) | TypeError: type() takes 1 or 3 arguments |
(c) | print(type((1,2))) | <class 'tuple'> |
(d) | print(type(1/2)) | <class 'float'> |
(e) | print(type([1/2])) | <class 'list'> |
(f) | print(type((1/2))) | <class 'float'> |
(g) | print(type((1/2,))) | <class 'tuple'> |
- (a)Â
print(type([1,2]))
- Explanation: This creates a list with two elements,Â
1
 andÂ2
. - Output:Â
<class 'list'>
- Explanation: This creates a list with two elements,Â
- (b)Â
print(type(1,2))
- Explanation: This will raise aÂ
TypeError
 because theÂtype()
 function expects a single argument and does not allow multiple arguments directly. - Output:Â
TypeError: type() takes 1 or 3 arguments
- Explanation: This will raise aÂ
- (c)Â
print(type((1,2)))
- Explanation: This creates a tuple with two elements,Â
1
 andÂ2
. - Output:Â
<class 'tuple'>
- Explanation: This creates a tuple with two elements,Â
- (d)Â
print(type(1/2))
- Explanation: This performs regular division, resulting in a floating-point number.
- Output:Â
<class 'float'>
- (e)Â
print(type([1/2]))
- Explanation: This creates a list with a single element, which is the result ofÂ
1/2
 (i.e.,Â0.5
). - Output:Â
<class 'list'>
- Explanation: This creates a list with a single element, which is the result ofÂ
- (f)Â
print(type((1/2)))
- Explanation: This performs regular division, resulting in a floating-point number. For a tuple, it needs atleast two elements or a single element along with a trailing comma. Check (g) for example.
- Output:Â
<class 'float'>
- (g)Â
print(type((1/2,)))
- Explanation: The trailing comma indicates that this is a tuple with a single element, which is the result ofÂ
1/2
 (i.e.,Â0.5
). - Output:Â
<class 'tuple'>
- Explanation: The trailing comma indicates that this is a tuple with a single element, which is the result ofÂ