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.
- 1Read the notation
The E means times 10 to the power of. The exponent is negative 4.
- 2Apply the power
52.3E-4 means 52.3 multiplied by 10 to the power of negative 4.
- 3Evaluate 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
Common mistake Treating 42 and 42.0 as the same kind of literal
What it looks like42 is an integer literal, while 42.0 is a floating-point literal.
Why it failsThe presence of the fractional notation distinguishes the two numeric forms.
Do this insteadIdentify whether the value is written as an int or a float before choosing how to use it.
Common mistake Assuming Python needs a separate long type for very large integers
What it looks likeA programmer expects to replace int with long when a number grows.
Why it failsPython's int type can represent integers of any size, and Python has no separate long type.
Do this insteadContinue using int for both small and very large integer values.
Common mistake Reading E as part of an arbitrary name or ignoring its exponent
What it looks like52.3E-4 is read without applying the negative exponent.
Why it failsE notation means times 10 to the power of, so the exponent changes the scale of the value.
Do this insteadSeparate 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
float
E notation is one of the forms used to write floating-point numbers. The literal represents 1200.0.
Key Takeaways
- 1Python's two main numeric types are int and float.
- 2Integers represent whole-number values, while floats represent floating-point values such as 3.23.
- 3E notation means times 10 to the power of; for example, 52.3E-4 equals 0.00523.
- 4Python's int type can represent integers of any size, so there is no separate long type to select.
- 5Choose int for whole-number values and float for fractional values or values written in E notation.
At a glance
- 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.