KAHIBARO Bookshelves
Discord Login Register

4.1. Arithmetic operations

Arithmetic with Numbers in Python

Arithmetic operations are how you make Python do math. In this chapter we focus only on the basic operations you can perform with numbers.

We’ll use two main numeric types here:

  • int – whole numbers (like 3, -7, 100)
  • float – numbers with decimals (like 3.5, 0.1, -2.75)

Python can mix them in calculations, but the result may change type (for example, division often gives a float).


The Basic Arithmetic Operators

Python supports the usual mathematical operations:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Floor division: //
  • Remainder (modulo): %
  • Exponentiation (power): **

Let’s look at each one with simple examples.

# Addition
print(2 + 3)       # 5
# Subtraction
print(10 - 4)      # 6
# Multiplication
print(3 * 7)       # 21
# Division (true division)
print(10 / 4)      # 2.5

Notice that / always does “true division” and usually returns a float, even if the division is exact:

print(8 / 2)       # 4.0 (a float, not 4)

Floor Division: `//`

Floor division divides and then rounds down to the nearest whole number (towards negative infinity).

print(10 // 4)     # 2
print(11 // 4)     # 2
print(12 // 4)     # 3

With negative numbers, “round down” means going to the smaller number:

print(-10 / 4)     # -2.5  (normal division)
print(-10 // 4)    # -3    (rounded down from -2.5)

Use // when you want an integer result, such as splitting items into full groups.


Remainder (Modulo): `%`

The modulo operator % returns the remainder after division.

If you divide $a$ by $b$:

$$
a = b \times q + r
$$

then $a % b gives $r$, the remainder.

Examples:

print(10 % 3)      # 1  (3*3 = 9, remainder 1)
print(10 % 4)      # 2  (4*2 = 8, remainder 2)
print(12 % 6)      # 0  (no remainder)

Modulo is very useful for:

  • Checking if a number is even or odd:
    • n % 2 == 0 → even
    • n % 2 == 1 → odd
number = 7
print(number % 2)         # 1
print(number % 2 == 0)    # False (7 is not even)

With negative numbers, Python’s modulo result is always the same sign as the divisor:

print(-10 % 3)     # 2  (because -10 = 3 * -4 + 2)
print(10 % -3)     # -2

You don’t need to memorize the rule now, just be aware that negative numbers with % can be surprising.


Exponentiation (Powers): `**`

Use ** to raise a number to a power:

  • $a^b$ in math becomes a ** b in Python.

Examples:

print(2 ** 3)      # 8   (2 * 2 * 2)
print(5 ** 2)      # 25  (5 squared)
print(9 ** 0.5)    # 3.0 (square root of 9)

You can also use ** with negative exponents or floats:

print(2 ** -1)     # 0.5  (1 / 2)
print(4 ** 1.5)    # 8.0  (4^(3/2) = sqrt(4^3))

For very advanced math, you’ll usually use the math library, but ** covers many basic needs.


Operator Precedence (Order of Operations)

Python follows standard math rules (PEMDAS):

  1. Parentheses
  2. Exponents
  3. Multiplication, Division, Floor division, Modulo
  4. Addition, Subtraction

In practice:

print(2 + 3 * 4)        # 14, not 20
# Because 3 * 4 happens first: 2 + 12
print((2 + 3) * 4)      # 20
# Parentheses force 2 + 3 to happen first

When in doubt, use parentheses to make your intention clear:

result = (10 - 2) / (4 + 4)
print(result)           # 1.0

Mixing `int` and `float` in Calculations

When you mix integers and floats, Python usually converts the result to a float:

print(3 + 2.0)     # 5.0
print(5 * 0.5)     # 2.5
print(8 / 2)       # 4.0

Some operations keep results as int when possible:

print(7 // 2)      # 3  (int)
print(7.0 // 2)    # 3.0 (float, but same numeric value)
print(7 % 2)       # 1

Be aware of this when you expect whole numbers. You can convert types explicitly when needed in other chapters.


Using Arithmetic with Variables

You can store numbers in variables and use them in calculations.

width = 5
height = 3
area = width * height
print(area)        # 15

Variables can also be updated based on their current value:

counter = 0
counter = counter + 1
print(counter)     # 1
counter = counter * 2
print(counter)     # 2

This pattern is very common in loops and other calculations.


Integer Division vs. Real Division

A frequent beginner confusion is between / and //.

  • / → real division (keeps the fractional part, returns float)
  • // → floor division (drops the fractional part, rounds down)

Compare:

print(7 / 3)       # 2.3333333333333335
print(7 // 3)      # 2

Use:

  • / for precise numeric results (e.g., averages, ratios)
  • // for counting whole groups, pages, rows, etc.

Simple Practice Ideas

You can try small arithmetic tasks like:

  • Converting minutes to hours and minutes using // and %
  • Calculating the area or perimeter of simple shapes
  • Splitting a bill between people and seeing the exact share vs. whole-number share

Experiment in the Python interactive mode:

>>> 35 // 60       # full hours in 35 minutes
>>> 35 % 60        # leftover minutes
>>>
>>> 5 ** 3
>>> (2 + 3) * 4

As you progress, these operations will become the building blocks for more complex programs.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Chapter 1: What Is Programming?
    • ☰ 1.1. What is a computer program?
      • ☰ 1.2. What is Python?
        • ☰ 1.3. Where is Python used?
          • ☰ 1.4. How programming helps solve problems
            • ☰ 1.5. Your first Python program
            • ☰ 2. Chapter 2: Setting Up Python
              • ☰ 2.1. Installing Python (Windows / macOS / Linux)
                • ☰ 2.2. Using Python in the terminal
                  • ☰ 2.3. Using an IDE (IDLE / VS Code)
                    • ☰ 2.4. Running Python scripts
                      • ☰ 2.5. Python interactive mode
                      • ☰ 3. Chapter 3: Variables and Data Types
                        • ☰ 3.1. What is a variable?
                          • ☰ 3.2. Creating variables
                            • ☰ 3.3. Numbers
                              • ☰ 3.4. Test
                                • ☰ 3.5. Boolean values
                                  • ☰ 3.6. Type checking
                                  • ☰ 4. Chapter 4: Basic Operations
                                    • ☰ 4.1. Arithmetic operations
                                      • ☰ 4.2. Working with numbers
                                        • ☰ 4.3. String operations
                                          • ☰ 4.4. Combining text and variables
                                            • ☰ 4.5. Comments in Python
                                            • ☰ 5. Chapter 5: User Input and Output
                                              • ☰ 5.1. Getting input from the user
                                                • ☰ 5.2. Converting input to numbers
                                                  • ☰ 5.3. Formatting output
                                                    • ☰ 5.4. Writing interactive programs
                                                    • ☰ 6. Chapter 6: Conditions
                                                      • ☰ 6.1. Comparison operators
                                                        • ☰ 6.2. Logical operators
                                                          • ☰ 6.3. if, elif, else
                                                            • ☰ 6.4. Nested conditions
                                                              • ☰ 6.5. Common beginner mistakes
                                                              • ☰ 7. Chapter 7: Loops
                                                                • ☰ 7.1. Why loops are useful
                                                                  • ☰ 7.2. for loops
                                                                    • ☰ 7.3. while loops
                                                                      • ☰ 7.4. Loop counters
                                                                        • ☰ 7.5. break and continue
                                                                          • ☰ 7.6. Loop exercises
                                                                          • ☰ 8. Chapter 8: Data Collections
                                                                            • ☰ 8.1. Lists
                                                                              • ☰ 8.2. Tuples
                                                                                • ☰ 8.3. Dictionaries
                                                                                  • ☰ 8.4. Sets (basics)
                                                                                    • ☰ 8.5. Choosing the right data structure
                                                                                    • ☰ 9. Chapter 9: Functions
                                                                                      • ☰ 9.1. Why functions matter
                                                                                        • ☰ 9.2. Defining functions
                                                                                          • ☰ 9.3. Parameters and arguments
                                                                                            • ☰ 9.4. Return values
                                                                                              • ☰ 9.5. Scope (local vs global)
                                                                                                • ☰ 9.6. Reusing code
                                                                                                • ☰ 10. Chapter 10: Working with Files
                                                                                                  • ☰ 10.1. What is a file?
                                                                                                    • ☰ 10.2. Reading text files
                                                                                                      • ☰ 10.3. Writing and appending files
                                                                                                        • ☰ 10.4. File paths
                                                                                                          • ☰ 10.5. Simple file-based programs
                                                                                                          • ☰ 11. Chapter 11: Errors and Debugging
                                                                                                            • ☰ 11.1. Types of errors
                                                                                                              • ☰ 11.2. Reading error messages
                                                                                                                • ☰ 11.3. Common beginner errors
                                                                                                                  • ☰ 11.4. try and except
                                                                                                                    • ☰ 11.5. Debugging strategies
                                                                                                                    • ☰ 12. Chapter 12: Using Python Libraries
                                                                                                                      • ☰ 12.1. What is a library?
                                                                                                                        • ☰ 12.2. Importing modules
                                                                                                                          • ☰ 12.3. Standard libraries
                                                                                                                            • ☰ 12.3.1. math
                                                                                                                              • ☰ 12.3.2. random
                                                                                                                                • ☰ 12.3.3. datetime
                                                                                                                                • ☰ 12.4. Installing external libraries with pip
                                                                                                                                • ☰ 13. Chapter 13: Object-Oriented Programming
                                                                                                                                  • ☰ 13.1. What is object-oriented programming?
                                                                                                                                    • ☰ 13.2. Classes and objects
                                                                                                                                      • ☰ 13.3. The `__init__` method
                                                                                                                                        • ☰ 13.4. Attributes and methods
                                                                                                                                          • ☰ 13.5. Encapsulation
                                                                                                                                            • ☰ 13.6. Inheritance (basics)
                                                                                                                                              • ☰ 13.7. Simple OOP examples
                                                                                                                                              • ☰ 14. Chapter 14: Automation with Python
                                                                                                                                                • ☰ 14.1. What is automation?
                                                                                                                                                  • ☰ 14.2. Automating simple tasks
                                                                                                                                                    • ☰ 14.3. Working with files and folders
                                                                                                                                                      • ☰ 14.4. Automating text processing
                                                                                                                                                        • ☰ 14.5. Scheduling scripts
                                                                                                                                                          • ☰ 14.6. Practical automation examples
                                                                                                                                                          • ☰ 15. Chapter 15: Web Development with Python
                                                                                                                                                            • ☰ 15.1. How the web works (basic overview)
                                                                                                                                                              • ☰ 15.2. Introduction to Flask
                                                                                                                                                                • ☰ 15.3. Creating a simple web server
                                                                                                                                                                  • ☰ 15.4. Routing and templates
                                                                                                                                                                    • ☰ 15.5. Handling user input (forms)
                                                                                                                                                                      • ☰ 15.6. Very basic APIs
                                                                                                                                                                      • ☰ 16. Chapter 16: Data Science with Python
                                                                                                                                                                        • ☰ 16.1. What is data science?
                                                                                                                                                                          • ☰ 16.2. Working with data
                                                                                                                                                                            • ☰ 16.3. Introduction to NumPy
                                                                                                                                                                              • ☰ 16.4. Introduction to pandas
                                                                                                                                                                                • ☰ 16.5. Data visualization with matplotlib
                                                                                                                                                                                  • ☰ 16.6. Simple data analysis examples
                                                                                                                                                                                  • ☰ 17. Chapter 17: Mini Projects
                                                                                                                                                                                    • ☰ 17.1. Number guessing game
                                                                                                                                                                                      • ☰ 17.2. Simple calculator
                                                                                                                                                                                        • ☰ 17.3. To-do list
                                                                                                                                                                                          • ☰ 17.4. Password generator
                                                                                                                                                                                            • ☰ 17.5. Quiz application
                                                                                                                                                                                              • ☰ 17.6. Small web or data project
                                                                                                                                                                                              • ☰ 18. Chapter 18: Next Steps
                                                                                                                                                                                                • ☰ 18.1. How to practice Python
                                                                                                                                                                                                  • ☰ 18.2. Writing clean and readable code
                                                                                                                                                                                                    • ☰ 18.3. Choosing a specialization
                                                                                                                                                                                                      • ☰ 18.3.1. Web development
                                                                                                                                                                                                        • ☰ 18.3.2. Data science
                                                                                                                                                                                                          • ☰ 18.3.3. Automation
                                                                                                                                                                                                            • ☰ 18.3.4. Software development
                                                                                                                                                                                                            • ☰ 18.4. Learning resources and communities
                                                                                                                                                                                                            • ☰ 19. Appendix
                                                                                                                                                                                                              • ☰ 19.1. Python syntax cheat sheet
                                                                                                                                                                                                                • ☰ 19.2. Common errors and solutions
                                                                                                                                                                                                                  • ☰ 19.3. Glossary of terms