Performing Arithmetic Operations
Python has two main numeric types: int (integers) and float (floating-point numbers).
Start with the value
Before choosing how to perform arithmetic, identify what kind of number the data represents. Python has two main numeric types: int for integers and float for floating-point numbers. An integer represents a whole number, while a float represents a number that can include a decimal part.
What do you think happens?
What numeric type does Python recognize for the literal 2?
Reveal answer
Answer: int
A number written as 2 is an integer literal. Python recognizes it as an int.
Two numeric types
The int type is used for whole numbers. You can use integers in arithmetic, store them in variables, and pass them to functions. Python does not divide integers into separate small-integer and large-integer types. The same int type represents integers of any size, so there is no separate long type that you must declare for a very large whole number.
The float type is used for floating-point numbers. Floats can be written in ordinary decimal notation, such as 3.23, or in scientific notation using E notation, such as 52.3E-4. Both forms represent floating-point values.
Reading a numeric value
Classifying three literals
Decide whether each literal is an int or a float: 2, 3.23, and 52.3E-4.
Literal 2: This is a whole number written without a decimal part, so Python recognizes it as an int.
Literal 3.23: This is written with a decimal part, so it is a float.
Literal 52.3E-4: The E notation form represents a floating-point number, so it is a float.
2 is an int. 3.23 and 52.3E-4 are floats.
The important first step is classification. A whole-number value such as 2 belongs to int, while values written with decimal notation or E notation belong to float. This distinction tells you how Python represents the operands before you reason about the arithmetic involving them.
Interpreting E notation
In a floating-point literal, E or e means times 10 to the power of. The exponent tells you which power of 10 multiplies the number before the E. Therefore, 52.3E-4 means 52.3 multiplied by 10 to the power of negative 4.
Converting 52.3E-4
Interpret the floating-point literal 52.3E-4.
Read the marker: E means times 10 to the power of.
Read the exponent: The exponent is -4, so the multiplier is 10 to the power of -4.
Apply the notation: 52.3E-4 means 52.3 multiplied by 10 to the power of -4.
Write the decimal form: The decimal equivalent is 0.00523.
52.3E-4 and 0.00523 are two written forms of the same float value.
Large integer values
Python uses the same int type for ordinary and very large integers. You do not switch to another integer type when a whole-number value becomes large, and you do not declare a separate long type. This differs from the mental model some programmers bring from languages such as Java or C, where separate types for regular and very large integers may be part of the language experience described in the source.
Choosing the representation
| Data being represented | Recommended type | Reason |
|---|---|---|
| Number of items | int | A count is a whole-number, discrete quantity. |
| Index position | int | An index represents a whole-number position. |
| Distance | float | A measurement can contain a decimal part. |
| Average | float | An average can be a fractional value. |
| Scientific data | float | Scientific values may require decimal notation or E notation. |
Choosing int or float is a modeling decision. Use int for counts, indexing positions, and other discrete whole quantities. Use float for measurements, averages, and scientific data when decimal parts are part of the value. The goal is to represent what the data means, not merely how large the number looks.
Mistakes with numeric types
Assuming Python needs a separate type for large integers.
Python uses the int type for integers of any size and does not have a separate long type.
Fix:
Continue to represent the whole-number value with int.Treating every number as an int because it is written with digits.
Decimal notation and E notation represent floats.
Fix:
Recognize 3.23 and 52.3E-4 as floating-point values.Reading E as part of a variable name rather than as numeric notation.
In a floating-point literal, E or e introduces the power-of-10 exponent.
Fix:
Rewrite the value as 52.3 multiplied by 10 to the power of -4, which equals 0.00523.Choosing a type only because the number currently looks simple.
The type should match the kind of data being represented. Measurements and averages can have decimal parts.
Fix:
Use float when the meaning of the value is a measurement, average, or other fractional quantity.
Check your choice
For each item, choose int or float and explain why: the number of books on a shelf, the distance between two locations, the average score for a group, and the literal 2.5E3.
Hints
- Counts and index positions are whole-number quantities.
- Measurements and averages can contain decimal parts.
- E notation represents a float.
- Python's two main numeric types are int and float. Use int for whole numbers, including very large integers; Python does not require a separate long type. Use float for values with decimal parts, and remember that decimal notation and E notation both represent floats. In E notation, E means times 10 to the power of, so 52.3E-4 equals 0.00523. Choose the type that matches the meaning of the data: int for counts and positions, and float for measurements, averages, and scientific values.
Key Takeaways
- Python uses int for integers and float for floating-point numbers.
- Integers can be any size in Python, and there is no separate long type.
- E notation means times 10 to the power of; 52.3E-4 equals 0.00523.
- Use int for counts and whole-number positions, and float for measurements, averages, and scientific data.