DEV Community

Mike Vincent
Mike Vincent

Posted on

Quark's Outlines: Python Arithmetic Conversions

Quark's Outlines: Python Arithmetic Conversions

Overview, Historical Timeline, Problems & Solutions

An Overview of Python Arithmetic Conversions

What are Python arithmetic conversions?

When you use math symbols like +, -, or * in Python, Python tries to make sure both sides of the math are the same type before it does the work. This is called arithmetic conversion.

Python checks the types of your values. If they are not the same type, Python converts them. This helps make sure the math works right.

Python will convert values so both sides match during arithmetic.

x = 5       # type int
y = 3.0     # type float
z = x + y   # Python converts 5 to 5.0
print(z)    # prints 8.0
Enter fullscreen mode Exit fullscreen mode

Here, Python converts the int to float so the math can happen.

Which types can Python convert?

Python uses certain rules to choose what type to convert to. These rules use a hierarchy. If one value is more complex than the other, Python will convert the simpler value.

The hierarchy is:

  1. complex
  2. float
  3. int

If no conversion is needed, Python will keep both values as integers if they are both whole numbers.

Python uses a type hierarchy to choose which type to use.

a = 10            # int
b = 3.2           # float
c = 1 + 2j        # complex
print(a + b)      # prints 13.2, converts a to float
print(b + c)      # prints (3.2+2j), converts b to complex
Enter fullscreen mode Exit fullscreen mode

Each time, Python converts the simpler type to match the more complex one.


A Historical Timeline of Python Arithmetic Conversions

Where do Python's arithmetic conversion rules come from?

Python's arithmetic conversion rules evolved from earlier programming languages. These rules help avoid errors when mixing different numeric types and keep the math correct.


People invented ways to combine numbers of different sizes

1970 — Type coercion in BASIC

Basic math in early languages used fixed types. You had to declare numbers in advance. People wanted a way for the machine to adjust them.

1983 — C arithmetic types

C added type promotion rules for mixed math. It promoted integers to floats or doubles when needed.

People designed Python's type conversion rules

1990 — Python numeric types

Python 1.0 introduced int, float, and complex types. It kept C-style promotion rules but made it simpler and safer.

1994 — Consistent coercion logic

Python added clear rules for +, -, *, and others to convert values before running the operation.

2000 — Coercion-free integers

Python 2.0 kept int and long as two separate types but promoted them when needed. Python 3.0 merged these into one.

2008 — Unified integer type

Python 3.0 changed long to one built-in int type that behaves like Python 2's long.

2025 — Stable conversion rules

Python's arithmetic conversion rules have not changed, and most new features do not alter the numeric types.


Problems & Solutions with Python Arithmetic Conversions

How do you use Python arithmetic conversions the right way?

Python arithmetic conversions help you combine numbers of different types. These problems show how Python converts values so you do not get errors.


Problem: How do you mix integers and floats in Python math?

You are working with scores that are whole numbers. You also have decimal values for time. You want to add them together, but you are not sure what will happen when you mix types.

Problem: What happens to the result when you add an int to a float in Python?

Solution: Python converts the int to a float so the result is a float.

Python converts an integer to float when adding to float.

score = 10         # int
time = 0.5         # float
total = score + time
print(total)       # prints 10.5
Enter fullscreen mode Exit fullscreen mode

Python does the math safely by converting the 10 to 10.0.


Problem: How do you mix complex and real numbers in Python math?

You are using a math library that returns complex results. You want to add these to real numbers, but you are not sure how Python will treat the inputs.

Problem: What happens when you add a float or int to a complex value in Python?

Solution: Python converts the real value to complex so the types match.

Python converts a real number to complex when adding to complex.

real = 4.5        # float
comp = 2 + 3j     # complex
result = real + comp
print(result)     # prints (4.5+3j)
Enter fullscreen mode Exit fullscreen mode

Python converts the real part to match the complex number.


Problem: How do you avoid losing precision with division in Python?

You divide two integers and expect a whole number, but the result has a decimal. You want to keep the result as a whole number.

Problem: Why does 5 / 2 not return 2 in Python 3?

Solution: Python 3 uses true division and returns a float. Use // for floor division.

Python uses // to divide and keep an integer result.

print(5 / 2)      # prints 2.5
print(5 // 2)     # prints 2
Enter fullscreen mode Exit fullscreen mode

Use // if you want the result as a whole number.


Problem: How do you force both values to stay as integers in Python math?

You are adding small numbers and want to keep the result an integer. But one input is a float. You do not want Python to convert the whole expression to float.

Problem: What happens if you pass a float into an expression with integers?

Solution: Python promotes all values to float if at least one is float. Use int() to convert back after.

Python promotes to float unless all values are int.

a = 7          # int
b = 3.0        # float
c = a + b
print(c)       # prints 10.0
Enter fullscreen mode Exit fullscreen mode

To keep it as an int, write c = int(a + b).


Problem: How do you add a string to a number using % in Python?

You want to create a message using a number and a string. You want to format the string like Your score is 10. You attempt use the old % operator.

Problem: What happens if the left side of % is a string?

Solution: Python allows only string formatting with that operator. Use f-strings or str.format() instead.

Python uses % for string formatting when left is a string.

score = 10
print("Your score is %d" % score)  # prints Your score is 10
Enter fullscreen mode Exit fullscreen mode

But in a pure math operation, this will fail. Use formatted strings for better safety.


Like, Comment, Share, and Subscribe

Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!


Mike Vincent is an American software engineer and app developer from Los Angeles, California. More about Mike Vincent

Top comments (0)