Learn by reading through in order

Numeric Types and Floating Point

Get comfortable with Python's int and float types and the operators that trip people up, all by running examples in your browser.

Python has two numeric types

Python has two main numeric types for working with numbers: int for whole numbers and float (floating-point) for decimals.

Both are numbers, so you can do all the basic arithmetic — addition, subtraction, multiplication, division, and modulo — with either one.

But their syntax and precision differ, so you pick the right one for each situation.

How int and float split the work
25(digits only)int typewhole number<class 'int'>175.5(has a .)float typefloating-point<class 'float'>type decidedtype()type decidedtype()

Write it without . and you get an int.

Write it with . and you get a float.

Either way, type() tells you which one you have.

Even if the value looks like a whole number, adding .0 like 3.0 makes it a float. On the flip side, just writing 175 makes it an int.

Negative numbers work the same way — -5 is an int, -5.5 is a float.

# int (whole number)
age = 25
print(type(age))      # <class 'int'>

# float (floating-point)
weight = 175.5
print(type(weight))   # <class 'float'>

# Same "3", different types depending on how you write it
print(type(3))        # <class 'int'>
print(type(3.0))      # <class 'float'>

# Negatives work the same way
print(type(-10))      # <class 'int'>
print(type(-10.5))    # <class 'float'>

Check the difference between int and float in the terminal.

① Create count = 7 and pi = 3.14, and print both values and types with print().

② Write print(type(3.0)) to see with your own eyes that 3 and 3.0 have different types.

Python Editor

Run code to see output

Quick tour of the operators

There are 7 operators you use for numeric math. The four basic ones + - * / work the same as in other languages, but // (integer division), % (modulo), and ** (exponentiation) come up a lot in Python, so it's worth locking them in.

OperatorMeaningExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division (always float)10 / 33.3333...
//Integer division (truncates decimals)10 // 33
%Modulo (remainder of division)10 % 31
**Exponentiation2 ** 101024

/ returns a float even between ints

10 / 2 is 5.0 (float), not 5.

Python's / is designed to always return a float, so the result is a float even when both operands are ints.

If you want an integer result, use // instead.

# Basic arithmetic
print(10 + 3)    # 13
print(10 - 3)    # 7
print(10 * 3)    # 30

# Division always returns a float
print(10 / 3)    # 3.3333333333333335
print(10 / 2)    # 5.0  (still a float even when it divides evenly)

# Integer division (truncates) and modulo
print(10 // 3)   # 3
print(10 % 3)    # 1

# Exponentiation: a ** b means "a to the power of b"
print(2 ** 10)   # 1024
print(3 ** 2)    # 9
print(2 ** 0.5)  # 1.4142135623730951  (use it as a square root)

Let's find out how many minutes and seconds seconds = 365 is.

① Use // to print "how many minutes".

② Use % to print "how many leftover seconds".

③ Use `** to print 2 to the 8th power (256).

④ Print 10 / 2 and confirm that the result is a float (5.0) even when it divides evenly.

Python Editor

Run code to see output

Compound assignment operators (+= / -= and friends)

Patterns like "x = x + 1" — where you do something to a variable and write the result back to itself — come up all the time.

You can shorten that to "x += 1", and these shortcut forms are called compound assignment operators.

Each of `+ - / // % * has a compound form.

How compound assignment works
x += 3x = x + 313x -= 3x = x - 37x *= 3x = x * 330x **= 2x = x ** 2100expandresultexpandresultexpandresultexpandresult

The short form is a one-line way to say "do something to myself and write it back".

Internally it behaves exactly like the expanded form on the right.

As an example, here's what each operator does when x = 10.

score = 10

score += 5      # 10 + 5 -> 15
print(score)    # 15

score *= 2      # 15 * 2 -> 30
print(score)    # 30

score -= 7      # 30 - 7 -> 23
print(score)    # 23

# Works the same way with float
weight = 60.0
weight += 1.5
print(weight)   # 61.5

Let's update a character's HP starting from hp = 100, using compound assignment.

① Decrease hp by 30 and print it.

② Then double hp and print it.

③ Then replace hp with the remainder of hp divided by 5, and print it.

Python Editor

Run code to see output

Mixing int and float in operations

When you mix int and float, the result is always a float. That's because Python automatically promotes to whichever type has more precision.

As mentioned above, / (division) returns a float even between ints, while // (integer division) returns an int when both operands are ints.

Type rules for operation results
int + int-> intint + float-> floatint / int-> float(always)resultpromoteexception

int + int = int.

But / always returns a float.

If either side is float, the result is float.

# int with int stays int
print(10 + 3)        # 13   -> int
print(type(10 + 3))  # <class 'int'>

# If either side is float, the result is a float
print(10 + 3.0)         # 13.0
print(type(10 + 3.0))   # <class 'float'>

# / returns a float even between ints
print(10 / 2)           # 5.0
print(type(10 / 2))     # <class 'float'>

# // stays int when both are ints
print(10 // 3)          # 3
print(type(10 // 3))    # <class 'int'>

# // becomes float if either side is float
print(10 // 3.0)        # 3.0
print(type(10 // 3.0))  # <class 'float'>

Try predicting the result type, then check it.

① Print the result and type of 7 + 2.

② Print the result and type of 7 + 2.0.

③ Print the result and type of 10 / 5 (notice that it's a float even when the division is exact).

Python Editor

Run code to see output

float has limited precision

Computers store floats as a binary approximation. That's why even simple-looking calculations can produce a tiny amount of error.

The example below is a classic "float gotcha" — every beginner runs into it at some point.

Where float errors come from
0.1(written in decimal)0.00011001100...(repeats in binary)truncated to finite bits~= approx. of 0.10.1 + 0.2math on approximations0.30000000000000004(error appears)to binarystorerunresult

Let's see the error with your own eyes.

① Print 0.1 + 0.2 (confirm that it doesn't become 0.3).

② Print 0.1 + 0.1 + 0.1.

③ Print 0.1 + 0.2 == 0.3 (it won't be True).

Python Editor

Run code to see output

Use Decimal when you need exact math

For situations where even a tiny error is unacceptable — like money calculations — you can do exact decimal math with the standard library's Decimal type. We won't cover it in this article, but just remember: "float is an approximation" and "there's a different tool for exact math".

Switching types with int() and float()

To convert between numeric types, use the built-in functions int() and float().

One thing to watch out for: int() truncates the decimal part — it doesn't round. So passing 3.9 to int() gives you 3, not 4. For negative values, it rounds toward zero, so -3.9 becomes -3, not -4.

Converting with int() and float()
3.9floatint()truncate3int5intfloat()add .05.0floatpass inresultpass inresult
# float -> int is "truncate" (NOT rounding!)
print(int(3.9))     # 3
print(int(3.1))     # 3
print(int(-3.9))    # -3   (rounds toward zero)

# int -> float just adds .0
print(float(5))     # 5.0
print(float(-2))    # -2.0

# Using conversion through a variable
a = 3.9
a = int(a)
print(a, type(a))       # 3 <class 'int'>

b = 5
b = float(b)
print(b, type(b))       # 5.0 <class 'float'>

Let's check how int() behaves.

① Print the result of int(7.8) (it doesn't round).

② Print the result of int(-7.8) (see which direction it rounds on the negative side).

③ Print the result and type of float(10).

Python Editor

Run code to see output

In this article, you learned:

  • The difference between int and float
  • Exponentiation with "**" and compound assignment (+= and friends)
  • The return-type difference between "/" (division) and "//" (integer division)
  • Float precision issues
  • Type conversion with int() / float()
QUIZ

Knowledge Check

Answer each question one by one.

Q1What does this code print?
print(type(10 / 2))

Q2What does this code print?
x = 10
x **= 3
print(x)`

Q3What does this code print?
print(int(-3.9))