Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python Numbers and Arithmetic Operators

Core Python · Tutorial 4

Python Numbers and Arithmetic Operators

Work with integers and floats, use arithmetic operators, understand precedence, and avoid floating-point surprises.

Open the compilerPractice exercises
beginner9 min readUpdated November 19, 20264 learning goals
On this page
  1. Basic operators
  2. Precedence and parentheses
  3. Integers do not overflow
  4. Floating-point numbers
  5. Useful numeric functions
  6. Try it

What you will learn

  • Use +, -, *, /, //, %, and **
  • Predict the result type of a calculation
  • Format decimal output
  • Explain why 0.1 + 0.2 is not exactly 0.3
Before this lesson: Python Variables and Data Types

Python supports integers, floating-point numbers, and complex numbers. Most beginner programs use the first two.

Basic operators#

Python
a = 7
b = 2

print(a + b)    # 9
print(a - b)    # 5
print(a * b)    # 14
print(a / b)    # 3.5
print(a // b)   # 3
print(a % b)    # 1
print(a ** b)   # 49
  • / always produces a float.
  • // performs floor division and keeps the integer type when both operands are integers.
  • % returns the remainder.
  • ** raises the left value to the power of the right value.

Precedence and parentheses#

Exponentiation runs before multiplication and division, which run before addition and subtraction. Parentheses override precedence.

Python
print(2 + 3 * 4)      # 14
print((2 + 3) * 4)    # 20
print(2 ** 3 ** 2)    # 512: right-associative
print(-2 ** 2)        # -4: exponentiation binds first

When a formula is not obvious, add parentheses. They cost nothing and make intent clear.

Integers do not overflow#

Python integers have arbitrary precision. They grow as needed, limited only by memory:

Python
print(2 ** 100)
Expected output

1267650600228229401496703205376

Floats follow IEEE 754 double precision and can lose exactness.

Floating-point numbers#

Some decimal values cannot be represented exactly in binary. The result is a tiny rounding error:

Python
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
Expected output

0.30000000000000004 False

For display, format the value:

Python
total = 0.1 + 0.2
print(f"{total:.2f}")
Expected output

0.30

For money, store integer cents or use the decimal module when exact decimal arithmetic is required.

Useful numeric functions#

Python
import math

print(abs(-9))            # 9
print(round(3.14159, 2))  # 3.14
print(math.sqrt(81))      # 9.0
print(math.floor(3.9))    # 3
print(math.ceil(3.1))     # 4

round() uses banker's rounding for exact ties. Do not rely on it for financial calculations.

Use ints for counting

Use an integer for counters and IDs. A value such as 3.0 is a float even though it looks whole; type(3.0) confirms the difference.

Try it#

Compute the area and circumference of a circle with radius 4.5. Format both answers to two decimal places. Then compute the average of 12, 19, and 7 as a float and as a formatted string.

Previous tutorialPython Variables and Data TypesNext tutorialPython Strings: Creation, Indexing, and Slicing

Related tutorials

beginner

Python Type Conversion and Input Values

Convert between strings, integers, and floats safely, and understand which conversions can fail.

7 min read
beginner

Python if, elif, and else Statements

Control program flow with conditions, nested branches, and clear boolean expressions.

8 min read

On this page

  1. Basic operators
  2. Precedence and parentheses
  3. Integers do not overflow
  4. Floating-point numbers
  5. Useful numeric functions
  6. Try it

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.