Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long type in Python-3, NameError: name 'long' is not defined

I am new to python.And one of my requirement is to deal with long values.The problem is i didnt know how to assign a long value.This question might be very silly.But im just now starting to learn the language.I have seen a blog and i tried something like this :

# Long program in Python
x=1
y = long(x)
print(type(y))

But i am getting an error like this:

Traceback (most recent call last):                                
File "main.py", line 4, in <module>                             
y = long(x)                                                   
NameError: name 'long' is not defined 

Can anyone please help me in acheiving this?

like image 214
Deepak Ramakrishnan Kalidass Avatar asked Jul 17 '26 22:07

Deepak Ramakrishnan Kalidass


1 Answers

The long() function is no longer supported by Python 3 (no pun intended). It only has one built-in integral type, named int; but it behaves mostly like the old long type. So you just need to use int() built-in function in python-3.x.

Also for more information here are the complete list of changes on integer type in python-3.x:

  • PEP 0237: Essentially, long renamed to int. That is, there is only one built-in integral type, named int; but it behaves mostly like the old long type.

  • PEP 0238: An expression like 1/2 returns a float. Use 1//2 to get the truncating behavior. (The latter syntax has existed for years, at least since Python 2.2.)

  • The sys.maxint constant was removed, since there is no longer a limit to the value of integers. However, sys.maxsize can be used as an integer larger than any practical list or string index. It conforms to the implementation’s “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).

  • The repr() of a long integer doesn’t include the trailing L anymore, so code that unconditionally strips that character will chop off the last digit instead. (Use str() instead.)

  • Octal literals are no longer of the form 0720; use 0o720 instead.

like image 84
Mazdak Avatar answered Jul 20 '26 11:07

Mazdak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!