Python Basics

Numbers and Operators in Python

Python works like a calculator: arithmetic operators combine numbers and comparison operators answer yes-or-no questions about them.

What is Numbers and Operators?

Python works like a calculator: arithmetic operators combine numbers and comparison operators answer yes-or-no questions about them.

Do arithmetic in Python with +, -, *, /, //, % and **, and compare values.

When should you use it?

  • Learn Python syntax through a practical example.
  • Build a foundation for larger programming problems.
  • Test an idea quickly without a local setup.

Example code

Run code →
main.py
a = 17
b = 5

print("a + b =", a + b)     # addition
print("a - b =", a - b)     # subtraction
print("a * b =", a * b)     # multiplication
print("a / b =", a / b)     # division always returns a float
print("a // b =", a // b)   # floor division drops the remainder
print("a % b =", a % b)     # modulo: the remainder
print("a ** 2 =", a ** 2)   # power

# Augmented assignment updates a variable in place.
total = 10
total += 5
total *= 2
print("total =", total)

# Comparison operators return True or False.
print(a > b, a == 17, a != b)

# round() and abs() are built in.
print(round(3.14159, 2), abs(-8))

Expected output

a + b = 22
a - b = 12
a * b = 85
a / b = 3.4
a // b = 3
a % b = 2
a ** 2 = 289
total = 30
True True True
3.14 8

How it works

/ always gives a float, // drops the remainder and % returns it. Operators such as += update a variable in place, and comparisons return True or False.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in Python IDE →

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges