Numerical Data Types


While not necessary for day-to-day Sage use, Python has 3 numerical data types that may become necessary to know when programming in the language, inside or outside of Sage. These types are int, float and complex. Type int is simply an integer. float is a decimal number with up to 12 digits of precision. For numbers with 12 integer digits and above, a float value will become scientific notation, and eventually around 308 digits and above will be estimated as inf, or infinity. complex is a complex number in the form a + bj. Note that python uses j for $\sqrt{-1}$ instead of i. Sage has its own types that it automatically assigns to new values, so to specifically use the Python types, you must use their constructors, which are simply commands named the same as the type name which convert the value passed to them to the python type. In order to demonstrate the type of the values we are constructing, we will use the type command, which returns the type of the value or object it is passed.

Sage Cell


a = int(1)
print(a, type(a))
a = float(2.5)
print(a, type(a))
a = complex(2 + 4j)
print(a, type(a))




Primary Tags—Programming: Python

Secondary Tags—Python: Data types

Related Cells




Date: 27 Mar 2019 00:18

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License