🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Arithmetic Operators

AI & DATA SCIENCE // arithmetic-operators

Arithmetic operators (+, -, *, /, //, %, **) perform mathematical calculations on numbers, and some of them also work on sequences like strings and lists.

Syntax

a + b
a - b
a * b
a / b
a // b
a % b
a ** b

Deep Dive Course

Python's arithmetic operators cover the basics you'd expect — addition, subtraction, multiplication — plus two forms of division: / (true division, always returns a float) and // (floor division, rounds toward negative infinity). % returns the remainder of floor division, and ** raises a number to a power, including fractional and negative exponents. + and * are also overloaded for sequences: concatenation for strings/lists, and repetition when multiplying a sequence by an int.

1Understanding Arithmetic Operators

Python's arithmetic operators cover the basics you'd expect — addition, subtraction, multiplication — plus two forms of division: / (true division, always returns a float) and // (floor division, rounds toward negative infinity). % returns the remainder of floor division, and ** raises a number to a power, including fractional and negative exponents. + and * are also overloaded for sequences: concatenation for strings/lists, and repetition when multiplying a sequence by an int.

💡

// rounds toward negative infinity, not toward zero, so -7 // 2 is -4, not -3 — this differs from integer division in languages like C, where truncation toward zero is standard.

editor.html
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
localhost:3000

2Practical Example

Here is a real-world application of Arithmetic Operators showing how it is used in production Python code.

editor.html
print(-7 // 2)
print("ab" * 3)
print([1, 2] + [3, 4])
localhost:3000

3Best Practices

Follow these guidelines when working with Arithmetic Operators:

1. Use for exponentiation instead of importing math for math.pow(), since works natively with ints and returns an int for integer inputs

2. Remember / always returns a float, even for two ints that divide evenly

3. Watch for // rounding toward negative infinity with negative operands, not toward zero, if your logic assumes C-style truncation

⚠️

Tip: // rounds toward negative infinity, not toward zero, so -7 // 2 is -4, not -3 — this differs from integer division in languages like C, where truncation toward zero is standard.

editor.html
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
localhost:3000

Examples

Example 01Basic Usage
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
Example 02Advanced Example
print(-7 // 2)
print("ab" * 3)
print([1, 2] + [3, 4])

Best Practices

  • Use ** for exponentiation instead of importing math for math.pow(), since ** works natively with ints and returns an int for integer inputs
  • Remember / always returns a float, even for two ints that divide evenly
  • Watch for // rounding toward negative infinity with negative operands, not toward zero, if your logic assumes C-style truncation

Interview Question

Why does -7 // 2 evaluate to -4 in Python instead of -3?

Hint: Think about which direction floor division rounds.

// is floor division — it always rounds the mathematical result down toward negative infinity, not toward zero. -7 divided by 2 is -3.5, and flooring that gives -4. This differs from truncating division in languages like C, where that same expression truncates toward zero and gives -3, so porting arithmetic between languages can silently change behavior on negative operands.

Exercises

MediumPractice using Arithmetic Operators in a real scenario.
View Solution
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)

Frequently Asked Questions

Why does -7 // 2 evaluate to -4 in Python instead of -3?

// is floor division — it always rounds the mathematical result down toward negative infinity, not toward zero. -7 divided by 2 is -3.5, and flooring that gives -4. This differs from truncating division in languages like C, where that same expression truncates toward zero and gives -3, so porting arithmetic between languages can silently change behavior on negative operands.

Related Functions

comparison-operatorsassignment-operatorsround()