QVSJ QVSJ - 7 months ago 10
Python Question

What is the difference between a number n and (n) in python

print(type(1))
print(type((1)))


gives me

<class 'int'>


Also

print(id(1))
print(id((1)))


gives me

1555424112
1555424112


however '(1)' is recognized as an instance where as '1' is not?
for eg, on doing a (1). in editor I get a lot of methods such as bit_length, conjugate, add() etc but not in doing 1.

What is the reason for the distinction?

Answer

(..) merely groups an expression. For integers, it also has the side-effect that the . character for floating point decimals can be disambiguated from the . attribute access operator.

So

1.bit_length()

is a syntax error, because bit_length is not a valid non-integer portion for a real number. But

(1).bit_length()

is valid Python syntax.

Alternatively, add a space:

1 .bit_length()
Comments