Numbers in Python
Python has two main numeric types: int (integers) and float (floating-point numbers).
Two Numeric Families
Python has two main numeric types: int for integers and float for floating-point numbers. The first distinction to make is whether a value is a whole number or a number represented with a fractional part. For example, 42 is an integer, while 42.0 is written as a floating-point value.
A decimal point is meaningful: 42 is an int literal, while 42.0 is a float literal.
Reading E Notation
E notation expresses multiplication by a power of ten. In a literal such as 1.5e3, the part before E is the number being scaled, and the part after E is the power of ten. Therefore, 1.5e3 means 1.5 multiplied by 10 to the power of 3, which is 1500.0.
Interpreting a negative exponent
Interpret 52.3E-4.
Read the notation: The E means times 10 to the power of. The exponent is negative 4.
Apply the power: 52.3E-4 means 52.3 multiplied by 10 to the power of negative 4.
Evaluate the value: The result is 0.00523.
52.3E-4 equals 0.00523.
One Type for Every Integer Size
Python's int type can represent integers of any size. Python does not use one type for ordinary integers and another type for very large integers; there is no separate long type. A small integer and a very large integer are both handled with the same int type.
Both variables use the int type.The important point is not a particular maximum value. The important point is Python's type design: it unifies integers under int rather than making you select a different integer type when the value becomes large.
Selecting the Numeric Type
| Programming need | Appropriate type | Typical form |
|---|---|---|
| Whole-number counting | int | 42 |
| A value with a fractional part | float | 3.23 |
| A value written with a power of ten | float | 52.3E-4 |
| A whole number that may become very large | int | a large integer |
Choose between int and float according to the kind of numeric value being represented.
Use int when the value is a whole number, including a whole number that is very large. Use float when the value is fractional or when it is written in the floating-point forms described in the source, including decimal notation and E notation. You do not need to replace int with another integer type as a number grows.
Mistakes with Numeric Types
Treating 42 and 42.0 as the same kind of literal
The presence of the fractional notation distinguishes the two numeric forms.
Fix:
Identify whether the value is written as an int or a float before choosing how to use it.Assuming Python needs a separate long type for very large integers
Python's int type can represent integers of any size, and Python has no separate long type.
Fix:
Continue using int for both small and very large integer values.Reading E as part of an arbitrary name or ignoring its exponent
E notation means times 10 to the power of, so the exponent changes the scale of the value.
Fix:
Separate the number before E from the exponent after E, then apply the power of ten.
Quick Classification Practice
Classify each value as int or float, and explain any E notation: 8, 8.0, 3.23, 1.2E3, and 900000000000000000000000.
Hints
- Look for a fractional part or E notation.
- A large whole number is still an int in Python.
What do you think happens?
What numeric type should Python use for 1.2E3?
Reveal answer
Answer: float
E notation is one of the forms used to write floating-point numbers. The literal represents 1200.0.
Key Takeaways
- Python's two main numeric types are int and float.
- Integers represent whole-number values, while floats represent floating-point values such as 3.23.
- E notation means times 10 to the power of; for example, 52.3E-4 equals 0.00523.
- Python's int type can represent integers of any size, so there is no separate long type to select.
- Choose int for whole-number values and float for fractional values or values written in E notation.
Key Takeaways
- Python uses int for integers and float for floating-point numbers.
- A decimal form such as 3.23 and an E notation form such as 52.3E-4 are floating-point forms.
- E notation scales a number by a power of ten.
- Python uses int for integers of any size and does not require a separate long type.
- Choose the type that matches whether the value is whole-number or fractional.