Invalid Syntax confusion on Python Integers [duplicate] - python

This question already exists:
accessing a python int literals methods [duplicate]
Closed 9 years ago.
We know that everything is an object in Python and so that includes integers. So doing dir(34) is no surprise, there are attributes available.
My confusion stems from the following, why is it that doing 34.__class__ gives a syntax error when I know that 34 does have the attribute __class__. Furthermore, why does binding an integer to a name, say x, and then doing x.__class__ yield my expected answer of type int?

Because 34.__class__ is not a valid floating-point number, which is what the . denotes in a numeric literal. Try (34).__class__.

Related

Python: Converting python2 to python3 [duplicate]

This question already has answers here:
Difference between int() and long()
(1 answer)
What are metaclasses in Python?
(25 answers)
Closed 1 year ago.
I am trying to use
https://github.com/iandees/mongosm/blob/master/insert_osm_data.py
this package. It seems like it is written in Python2. I have converted all the way to next(context). However, I am getting name 'long' is not defined.
Is there any way that I can define this somewhere? How can I define 'long' and I have no idea what this is for even for Python2 Script (which worked fine somehow).
long() is basically renamend to int() in Python 3.
Please see https://www.python.org/dev/peps/pep-0237/ for details.
So, either do a search of long and replace with int, or define it
long = int
somewhere at the beginning of your file.
You should convert all code to Python3
https://www.google.com/search?channel=fs&client=ubuntu&q=Converting+python2+to+python3
https://docs.python.org/3/library/2to3.html

how to decide type of a variable in python? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
enter image description here
I wrote a simple code in python.
Originally my assignment is to receive two inputs(name of person) and print them.
Here's my question.
When I try to sum two variables but one of them is int and another one is str, an error occurs.
But in this case (the picture) why variable 'a' is recognized as a str not int?
I think there must occurs an error but a is recognized as a str and work well.
In Python 3, input() always returns a string. (In Python 2, input() would try to interpret – well, evaluate, actually – things, which was not a good idea in hindsight. That's why it was changed.)
If you want to (try to) make it an int, you'll need int(input(...)).

lower versus length syntax in python? [duplicate]

This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 7 years ago.
I'm new to Python and I have a question about the string operations. Is there an over-arching reason that I should understand as to why the lower operation is written as 'variable.lower()' while another one, say length, would be written as 'len(variable)'?
lower is a string method, that is, a function built in to the string object itself. It only applies to string objects.
len is a built in function, that is, a function available in the top namespace. It can be called on many different objects (strings, lists, dicts) and isn't unique to strings.

Python assignment is returning an error (invalid token) [duplicate]

This question already has answers here:
Python: Invalid Token
(3 answers)
Closed 8 years ago.
Today I am learning python and I have assign one int variable in console like this
zipcode = 02492
But it's return me error like this
SyntaxError: invalid token
Why so, I don't understand it? Please help me to solve this query.
The reason you're getting the error is because Python interprets a number starting with the digit 0 as octal (base 8). However, the only valid octal digits are 0-7, so the 9 in your zip code is seen as invalid. Additionally, if you're using Python 3, the format of octal literals was changed so they begin with 0o now (a zero followed by the lowercase letter o), so you'd still get an error even if you tried to input zipcode = 02432, which would be valid in Python 2.
Since a zip code doesn't need to mathematical operations performed on it, it would be best if it was stored as a string.

Why are parentheses required around an integer to invoke methods on it? [duplicate]

This question already has answers here:
Why can't I call methods directly on int objects? [duplicate]
(1 answer)
Accessing attributes on literals work on all types, but not `int`; why? [duplicate]
(4 answers)
Closed 7 years ago.
This does not work.
>>> 10.__str__()
File "<stdin>", line 1
10.__str__()
^
SyntaxError: invalid syntax
But this works.
>>> (10).__str__()
'10'
Why are parentheses required around integer in order to invoke its methods? List or other data types don't seem to require it.
>>> [1, 2].__str__()
'[1, 2]'
>>> {'a': 'foo'}.__str__()
"{'a': 'foo'}"
Per the python documentation, numeric literals require parentheses because otherwise it is unclear if . is denoting a floating point number or a method invocation.
For example, to invoke a method on an integer:
(10).__str__()
but not
10.__str__()
Whereas to invoke a method on a floating point number:
(10.).__str__()
or
10..__str__()
both work because the first . can only be a floating point indicator because it is followed by a . invoking the method.

Categories