1.

Consider following expression in Python console:>>> num=100, Is the above expression valid? If yes, what is the data type of num? Why?

Answer»

At first, it gives the impression that given expression is invalid because of railing comma. But it is perfectly valid and Python interpreter doesn’t raise any error.

As would be expected, expression without railing comma declares an int object.

>>> num=100 >>> type(num) <class 'int'>

Note that use of parentheses in the REPRESENTATION of tuple is optional. Just comma separated values FORM a tuple.

>>> x=(10,20) >>> type(x) <class 'tuple'> >>> #this is ALSO a tuple EVEN if there are no parentheses ... x=10,20 >>> type(x) <class 'tuple'>

Hence a number (any object for that matter) followed by comma, and without parentheses forms a tuple with single element.

>>> num=100, >>> type(num) <class 'tuple'>

As mentioned above omitting comma is a REGULAR assignment of int variable.



Discussion

No Comment Found