Concepts / Performing Arithmetic Operations

Performing Arithmetic Operations

Before choosing how to perform arithmetic, identify what kind of number the data represents.

  • Programming

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?

  • int

  • float

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.

intwhole numbersitem countdiscrete quantitydistancemeasurementindex positionwhole-number positionfloatdecimal valuesaveragefractional valuesuitable forsuitable forsuitable forsuitable for
What is the difference between an int and a float, and which numeric type should I choose for a particular kind of data?

Reading a numeric value

Worked example

Classifying three literals

Decide whether each literal is an int or a float: 2, 3.23, and 52.3E-4.

  1. 1
    Literal 2

    This is a whole number written without a decimal part, so Python recognizes it as an int.

  2. 2
    Literal 3.23

    This is written with a decimal part, so it is a float.

  3. 3
    Literal 52.3E-4

    The E notation form represents a floating-point number, so it is a float.

Result

2 is an int. 3.23 and 52.3E-4 are floats.

2intinteger arithmeticuses int values3.23floatfloating arithmeticuses float values52.3E-4floatcan participate incan participate incan participate in
What changes when arithmetic uses integer values, floating-point values, or a mixture of both? First identify the kind of value represented by each operand.

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.

52.3base value0.00523decimal equivalentEtimes 10 to the power of-4power of 10combined withusesgives
How does a literal such as 1.5e3 represent a number, and what does the exponent change?
Worked example

Converting 52.3E-4

Interpret the floating-point literal 52.3E-4.

  1. 1
    Read the marker

    E means times 10 to the power of.

  2. 2
    Read the exponent

    The exponent is -4, so the multiplier is 10 to the power of -4.

  3. 3
    Apply the notation

    52.3E-4 means 52.3 multiplied by 10 to the power of -4.

  4. 4
    Write the decimal form

    The decimal equivalent is 0.00523.

Result

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.

Before

ordinary whole number

The value uses int.

After

very large whole number

The value still uses int; Python does not require a separate long type.

Choosing the representation

Data being representedRecommended typeReason
Number of itemsintA count is a whole-number, discrete quantity.
Index positionintAn index represents a whole-number position.
DistancefloatA measurement can contain a decimal part.
AveragefloatAn average can be a fractional value.
Scientific datafloatScientific values may require decimal notation or E notation.

Choose the type that matches the meaning of the data.

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

  • Common mistake

    Assuming Python needs a separate type for large integers.

    What it looks like

    Looking for a long declaration when a whole-number value becomes very large.

    Why it fails

    Python uses the int type for integers of any size and does not have a separate long type.

    Do this instead

    Continue to represent the whole-number value with int.

  • Common mistake

    Treating every number as an int because it is written with digits.

    What it looks like

    Classifying 3.23 or 52.3E-4 as an integer.

    Why it fails

    Decimal notation and E notation represent floats.

    Do this instead

    Recognize 3.23 and 52.3E-4 as floating-point values.

  • Common mistake

    Reading E as part of a variable name rather than as numeric notation.

    What it looks like

    Reading 52.3E-4 without interpreting the E as times 10 to the power of.

    Why it fails

    In a floating-point literal, E or e introduces the power-of-10 exponent.

    Do this instead

    Rewrite the value as 52.3 multiplied by 10 to the power of -4, which equals 0.00523.

  • Common mistake

    Choosing a type only because the number currently looks simple.

    What it looks like

    Using int for a distance or average simply because the current value happens to be a whole number.

    Why it fails

    The type should match the kind of data being represented. Measurements and averages can have decimal parts.

    Do this instead

    Use float when the meaning of the value is a measurement, average, or other fractional quantity.

Check your choice

EASY

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.
  1. 1
    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.

Related concepts

More concepts starting with P