How do you print long in python?

So, I have a very large number that I'm working out in python, but when I try to print it I get something like this:

3.101541146879488e+80

How do I print all the digits of my lovely number?

Bhargav Rao

47.1k27 gold badges121 silver badges137 bronze badges

asked Dec 5, 2014 at 22:27

5

both int and long work for this

>>> a
3.101541146879488e+80
>>> int[a]
310154114687948792274813492416458874069290879741385354066259033875756607541870592L
>>> long[a]
310154114687948792274813492416458874069290879741385354066259033875756607541870592L
>>> print [int[a]]
310154114687948792274813492416458874069290879741385354066259033875756607541870592
>>> print [long[a]]
310154114687948792274813492416458874069290879741385354066259033875756607541870592

answered Dec 5, 2014 at 23:02

Bhargav RaoBhargav Rao

47.1k27 gold badges121 silver badges137 bronze badges

2

Not the answer you're looking for? Browse other questions tagged python numbers or ask your own question.

In this tutorial, we will learn the different methods to print a large number in Python. And the contrast between printing integers or floats in those situations.  

As you go through the article, feel free to watch my explainer video:

How to Print All Digits of a Large Number in Python?

  • Problem Formulation: Float vs Integer Printing
    • What is the maximum possible value of an integer in Python?
    • How do you print large numbers in Python?
  • Method 1: Convert Floats to Integers
  • Method 2: Decimal Module
  • Method 3: Fractions
  • Summary

Problem Formulation: Float vs Integer Printing

Python by default prints an approximation in the case of float numbers. Most people do not need that much precision. This is to keep the number of digits manageable by displaying a rounded value instead:

>>> print[0.1]
0.1

If the numbers weren’t rounded this would be the result:

0.1000000000000000055511151231257827021181583404541015625

If you try to print the large float number, this would be the result using the print[] function:

>>> print[0.1000000000000000055511151231257827021181583404541015625]
0.1

To tell you the truth, most programmers that don’t work with accounting or mathematical applications do not bother to understand why printing large floats or integer numbers is an issue in Python. They simply don’t have the need.

What is the maximum possible value of an integer in Python?

In the case of integers, those types of numbers don’t have any kind of limitation went printing large numbers. They can be as large value as memory is available.

Try running this:

y = 99999**1000
print[y]

The output should be a large integer number. And it should not have any problem displaying the results of this exponentiation.  

If you increase the exponent, let say to 10000 value, you should get the results with a message "Squeed text [255 lines].", on the IDLE Shell. 

If you double click the message, it will follow up by a warning that expands it will make it slow or unresponsive. That is because the output consumes your PC memory.  

How do you print large numbers in Python?

Is went we dwell with large float numbers, that is went problems start to arise. Example:

x=9.9999**1000
print[x]

Output:

Traceback [most recent call last]:
  File "", line 1, in 
    x=9.9999**1000
OverflowError: [34, 'Result too large']

The print[] function can’t show on the screen the results because the answer is too large.

The reality is that Python floats are neither of unlimited size and don’t have arbitrary precision by default, meaning: there is a size limit based on the system it is running [most common 32 or 64 bits]. That restriction does not apply to integers or strings.

To know more about the technical limitations of the Python programming language read the Floating-Point Arithmetic: Issues and Limitations.

Let say the numbers you are working on are large, but not large enough to produce an overflow error. Printing the results should be easy? Right?

>>> x=9.9999**100
>>> print[x]

Output:

9.990004948383415e+99

Well, it prints a summary because the number is still too high. The print[] function standard limitations can be circumvented using the following methods:

Method 1: Convert Floats to Integers

Python displays a rounded value to keep the number of digits manageable. It shows float up to 16 digits of precision. 

One method to get all the numbers is to convert the results from floats to integers using the int[] function.  

>>> print[int[x]]

Output:

9990004948383414538969489466669439837918658430314936559225135051476998194588052528854885488023044096

On older versions of Python, the long[] function can be used to get identical results. Check PEP 237 for more information.

Method 2: Decimal Module

In cases like accounting and high-precision applications, jobs where printing the exact decimal representation is needed, use the decimal module.

Using the previous example:

>>> from decimal import Decimal
>>> Decimal.from_float[x]

Output:

Decimal['9990004948383414538969489466669439837918658430314936559225135051476998194588052528854885488023044096']

This module is designed to work in the same way as arithmetic is taught at school. It provides support for fast correctly-rounded decimal floating-point arithmetic, even for large numbers.

Method 3: Fractions

Another way to print large float numbers is supported by the fractions module which implements arithmetic based on rational numbers [so the numbers like 1/10 can be represented exactly].

>>> from fractions import Fraction
>>> Fraction[x]

Output:

Fraction[9990004948383414538969489466669439837918658430314936559225135051476998194588052528854885488023044096, 1]

Also, Python has a method that delivers the exact value of float, as a fraction: The float.as_integer_ratio[]

x.as_integer_ratio[]

Output:

Fraction[9990004948383414538969489466669439837918658430314936559225135051476998194588052528854885488023044096, 1]

Summary

In this tutorial, we learned three different ways to print large numbers:

  • Convert floats to integers
  • Decimal module
  • Fractions

We hope this brief article helps you understand better the ways of printing large integers and floats numbers and the limitation of each. 

How do you print a long value in Python?

The long[] function is no longer supported by Python 3. 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.

How do you write long in Python?

Type int[x] to convert x to a plain integer. Type long[x] to convert x to a long integer.

How do you print long integers in Python?

The best way to deal with this is to convert the long into a string and when the template parses the string, use {{ created_on_timestamp|safe }} to render the quote marks as quote marks.

Does Python have a long type?

Python 3 does not have a long type.

Chủ Đề