Skip to content
beginner

Basic Math in Python

Are you ready to start using Python for real-world tasks? One of the first things every programmer learns is how to do math with code. Whether you want to…

Published 2026-05-11Updated 2026-05-127 min read

Are you ready to start using Python for real-world tasks? One of the first things every programmer learns is how to do math with code. Whether you want to build a calculator, analyze data, or just automate everyday calculations, understanding python math operations is an essential skill.

In this tutorial, you’ll learn how to add, subtract, multiply, and divide numbers in Python. You’ll also discover some extra math tricks and see how Python makes calculations simple—even if you’re totally new to programming!

Why Learn Math in Python?

Math is everywhere in programming. From simple calculations to complex algorithms, almost every project uses math in some way. Here’s why learning math in Python is a great first step:

  • Math is used in many programming tasks. Whether you’re working with money, measuring data, or creating games, you’ll use math.
  • Python makes math simple and clear. Its basic math operations are easy to read and write, even for beginners.
  • Learning math operations builds confidence. Once you can do basic calculations, you’ll feel ready to tackle bigger projects.

If you’re just starting with python basics, mastering math operations will open the door to all kinds of programming possibilities!

How Python Handles Numbers

Before you can do math, it helps to know how Python works with numbers. Python has two main types of numbers:

  • Integers (whole numbers): Examples include 3, 42, and -7.
  • Floats (decimal numbers): Examples include 3.14, 0.5, and -2.0.

You can store numbers in variables—named containers that hold data. Here’s how it looks:

age = 25           # integer
price = 19.99      # float

Rules for writing numbers in Python:

  • Don’t use commas in numbers (write 1000, not 1,000).
  • Use a dot for decimals (3.5 not 3,5).
  • Variable names can be anything you like (as long as they start with a letter and have no spaces).

Quiz Question 1

Question: What is the difference between an integer and a float in Python?

  • A) Integers have decimals, floats do not
  • B) Integers are whole numbers, floats can have decimals
  • C) Integers are always positive, floats are negative
  • D) There is no difference

With these basics, you’re ready to start doing math!

Adding and Subtracting Numbers

Let’s start with the simplest python arithmetic: adding and subtracting.

  • Use the plus sign (+) to add numbers.
  • Use the minus sign (-) to subtract numbers.

Here are some examples:

# Addition
sum = 5 + 3
print(sum)   # Output: 8

# Subtraction
difference = 10 - 4
print(difference)   # Output: 6

You can also add or subtract variables:

a = 7
b = 2
result = a + b
print(result)   # Output: 9

Try changing the numbers and see what happens!

Quiz Question 2

Question: Which of these is a valid variable name in Python?

  • A) 2nd_number
  • B) number two
  • C) number2
  • D) number-two

Multiplying and Dividing Numbers

Now let’s look at multiplying and dividing—two more core python math operations.

  • Use the asterisk (*) to multiply.
  • Use the slash (/) to divide.
# Multiplication
product = 6 * 3
print(product)   # Output: 18

# Division
quotient = 12 / 4
print(quotient)   # Output: 3.0

A quick note: In Python, dividing two numbers with / always gives you a decimal (float), even if the answer is a whole number.

result = 10 / 2
print(result)   # Output: 5.0

If you want just the whole number part of a division (no decimals), you can use double slashes (//):

whole_number = 7 // 2
print(whole_number)   # Output: 3

Quiz Question 3

Question: What is the difference between / and // in Python division?

  • A) / gives whole numbers, // gives decimals
  • B) / gives decimals, // gives whole numbers
  • C) Both give decimals
  • D) Both give whole numbers

Other Useful Math Operations

Python has a few more math tricks that are handy in many situations.

  • Exponents: Use double asterisks (**) to raise a number to a power.

    squared = 4 ** 2   # 4 to the power of 2
    print(squared)     # Output: 16
    
  • Remainders (Modulo): Use the percent sign (%) to find the remainder after division.

    remainder = 10 % 3
    print(remainder)   # Output: 1
    

When are these useful?

  • Exponents are great for powers (like squaring or cubing numbers).
  • Remainders are useful for checking if a number is even or odd, or for repeating patterns.

Quiz Question 4

Question: Which operator would you use in Python to find the remainder after dividing two numbers?

  • A) %
  • B) //
  • C) **
  • D) /

Using Python’s Math Functions

For more advanced calculations, Python comes with a math module full of helpful functions. To use it, you first need to import it:

import math

Here are a few popular functions:

  • Square root: Use math.sqrt() to find the square root of a number.

    import math
    root = math.sqrt(16)
    print(root)   # Output: 4.0
    
  • Rounding numbers: Use round() to round a decimal to the nearest whole number.

    rounded = round(3.7)
    print(rounded)   # Output: 4
    
  • Absolute value: Use abs() to get the positive value of a number.

    value = abs(-5)
    print(value)   # Output: 5
    

You’ll use these functions whenever you need more than the basic math operations.

Quiz Question 5

Question: Which function would you use to round a number to the nearest whole number in Python?

  • A) abs()
  • B) round()
  • C) math.sqrt()
  • D) int()

Practice: Try Simple Math Yourself

The best way to learn is by doing! Try these exercises to build your confidence with python basics and python math operations:

  1. Add two numbers and print the result.
  2. Subtract one number from another.
  3. Multiply two numbers.
  4. Divide two numbers and print both the decimal and whole-number result.
  5. Find the remainder when dividing two numbers.
  6. Use math.sqrt() to find the square root of a number.
  7. Experiment with round() and abs() on different numbers.

Change the numbers and see what happens. The more you practice, the more comfortable you’ll get!

Conclusion

You’ve just learned the essentials of python math operations—from adding and subtracting, to using exponents and math functions. These skills are the foundation for all sorts of programming projects, big and small.

Keep practicing these basics, and you’ll soon be ready to tackle more complex problems with confidence. Remember: every pro programmer started with the basics, just like you! Happy coding!


Quiz Answer Key

Question 1

Correct answer: B) Integers are whole numbers, floats can have decimals

Explanation: Integers are numbers without decimals (like 3 or -2), while floats can have decimal parts (like 3.14 or -2.0).

Question 2

Correct answer: C) number2

Explanation: Variable names must start with a letter and cannot have spaces or dashes. number2 is valid.

Question 3

Correct answer: B) / gives decimals, // gives whole numbers

Explanation: / always returns a float (even if the result is a whole number), while // returns just the whole number part (integer division).

Question 4

Correct answer: A) %

Explanation: The % operator gives the remainder after dividing two numbers.

Question 5

Correct answer: B) round()

Explanation: The round() function rounds a number to the nearest whole number in Python.

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

beginner7 min read

Python Comments and Code Style

Learning Python is an exciting adventure! But writing code that works is only part of the journey. Making your code readable—for yourself and for others—is…

Read tutorial
beginner7 min read

Python Variables and Data Types

Learning to program is a bit like learning a new language. One of the first things you’ll need to know is how to store and use information. In Python, we…

Read tutorial