Technology Numbers and Operators | Python Tutorial

Learn with diagrams, code, systems and practical examples.

</>
TutorialsPythonNumbers and Operators
Core Python · Chapter 5

Numbers and Operators

Work with integers, decimals, arithmetic, and comparisons.

Why this matters

Many programs calculate totals, compare limits, measure durations, or transform numeric data.

Start with the idea

Python supports integers and floating-point numbers along with familiar arithmetic operators. Comparison operators produce Boolean values such as True or False.

IdeaUnderstand the purpose
CodeRun the smallest example
PracticeChange it yourself
SubjectVision learning pattern: understand the idea before memorizing syntax.

Small working example

Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.

Python
price = 12.50
quantity = 4
total = price * quantity
is_large_order = total >= 40
print(total)
print(is_large_order)
Expected output
50.0
True
What to notice

The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.

Common beginner mistake

Mistake

Assuming decimal floating-point arithmetic always behaves like exact money arithmetic.

Better approach

For financial software, use an appropriate decimal or integer-minor-unit strategy instead of blindly relying on binary floating point.

Quick recap

  • Arithmetic creates new numeric values.
  • Comparisons create booleans.
  • Choose numeric representations that fit the domain.

Try it yourself

These are deliberately small. If you can complete them without copying the example, you are ready to continue.

  1. Calculate a discounted price.
  2. Check whether a value is between two limits.

Prepared deep chapter · Core Python

Numbers and operators turn values into decisions

Programs constantly work with numbers: a shopping cart adds prices, a game changes a score, a monitoring script compares a response time with a limit, and a cloud-cost tool divides usage into billing units. Python gives you several numeric types and a small set of operators for doing that work.

The beginner goal is not to memorize symbols. It is to learn a dependable routine: identify the values, choose the operation that matches the question, predict the result type, then verify the output. That routine is more useful than guessing from punctuation.

From numeric values to a checked result Two numeric inputs enter an operator, produce a result, and then pass through a reasonableness check. The diagram emphasizes choosing an operation before relying on the calculated output. value 1price = 12.50 value 2qty = 4 operationprice * qty result + check50.0 ✓ Question → operator → result type → reasonableness check
Choose the operation from the question first. Then check whether the result and its type make sense.

Start with the two number types beginners use most

Python has several numeric types. In everyday beginner programs, the two you will meet most often are int and float.

Integer (int)

A whole number such as 0, 12, or -5. Use integers naturally for counts, indexes, retry limits, and other whole-number quantities.

Floating-point number (float)

A number stored in floating-point form, such as 2.5 or -0.75. Floats are useful for measurements and many calculations, but not every decimal fraction can be represented exactly.

items = 3
weight_kg = 2.75

print(type(items).__name__)
print(type(weight_kg).__name__)
Expected output
int
float

Python also supports complex numbers, and the standard library includes types such as decimal.Decimal. Those are useful in specific domains, but you do not need them to understand the basic arithmetic model.

Use arithmetic operators to express the question

Suppose a cart contains four notebooks at 12.50 each. The question is “What is the total?” That question tells you to multiply price by quantity.

price = 12.50
quantity = 4
subtotal = price * quantity
print(subtotal)
Expected output
50.0
Common Python arithmetic operators
OperatorMeaningExampleResult
+Add7 + 310
-Subtract7 - 34
*Multiply7 * 321
/True division7 / 32.333...
//Floor division7 // 32
%Remainder7 % 31
**Power2 ** 38

Division has three different questions hiding inside it

The symbols /, //, and % all involve division, but they answer different questions.

How many exactly?
minutes = 95
hours = minutes / 60
print(hours)

/ produces the quotient and integer division still produces a float.

How many complete groups?
items = 17
box_size = 5
full_boxes = items // box_size
print(full_boxes)

// uses floor division. Here it says three full boxes fit.

What is left over?
leftover = 17 % 5
print(leftover)

% gives the remainder. Here two items are left after filling three boxes.

Common mistake

Do not use // when you mean “throw away the decimal part.” Floor division rounds toward negative infinity, so -7 // 3 is -3, not -2. Learn the operator from its meaning, not from what it happens to do for one positive example.

Operator precedence decides what groups first

Python follows precedence rules similar to ordinary arithmetic. Multiplication and division group more tightly than addition and subtraction. Parentheses let you state the grouping explicitly.

without_parentheses = 10 + 2 * 5
with_parentheses = (10 + 2) * 5

print(without_parentheses)
print(with_parentheses)
Expected output
20
60

When the business meaning matters more than the reader's memory of precedence, use parentheses. They reduce ambiguity for people even when Python would calculate the same result without them.

Good to know: why -1**2 can surprise you

Exponentiation binds differently from unary minus. Python interprets -1**2 as -(1**2), which is -1. Write (-1) ** 2 when the negative number itself is the base.

Floating-point decimals are approximations

A float is stored in binary floating-point form. Many decimal fractions that look simple to humans do not have an exact finite representation in binary. That is why this is possible:

total = 0.1 + 0.2
print(total)
print(total == 0.3)
Typical output
0.30000000000000004
False

This is not Python “getting addition wrong.” It is a representation issue shared by common binary floating-point systems. For measurements and many ordinary calculations, floats are appropriate. For domains that require exact decimal arithmetic—especially money rules—choose a representation designed for that contract, such as integer minor units or decimal.Decimal.

Beginner rule

Choose the numeric representation from the domain requirement. Do not automatically use float just because a value contains a decimal point.

Four predictable beginner mistakes

Using ^ for powers

In Python, ^ is bitwise XOR. Use ** for exponentiation: 2 ** 3.

Dividing by zero

10 / 0, 10 // 0, and 10 % 0 raise ZeroDivisionError. Validate a divisor when it comes from input.

Assuming / keeps integers

8 / 2 produces 4.0. If result type matters, predict it before using the value elsewhere.

Comparing floats for exact equality

Some calculated floats should be compared with an appropriate tolerance rather than exact equality. The right strategy depends on the domain.

Build a small delivery estimate step by step

A delivery tool needs to divide 47 packages into vans that hold 12 packages each.

packages = 47
capacity = 12

full_vans = packages // capacity
leftover_packages = packages % capacity
needs_extra_van = leftover_packages > 0

print(full_vans)
print(leftover_packages)
print(needs_extra_van)
Expected output
3
11
True

The arithmetic operators answer two different questions: // tells us how many vans can be completely filled, while % tells us what remains. A comparison then turns the remainder into a yes/no decision for the next step.

Reveal a safer planning step

If even one package remains, the operation needs another van. One way to calculate the required number directly is (packages + capacity - 1) // capacity. Treat that as a useful pattern after you understand the simpler quotient-and-remainder reasoning above.

Practice before moving on

  1. A service has 73 jobs and processes 10 jobs per batch. Calculate the number of complete batches and the remainder.
  2. Predict the output of 3 + 4 * 2, then compare it with (3 + 4) * 2.
  3. Run 0.1 + 0.2 == 0.3. Explain why the result is not evidence that Python arithmetic is broken.
  4. Create a small cart calculation with price, quantity, subtotal, discount amount, and final total. Print each intermediate value so you can inspect the calculation.

Quick recap

  • int represents integers; float represents binary floating-point values.
  • /, //, and % answer different division questions.
  • ** means exponentiation; ^ does not.
  • Parentheses make calculation intent easier to read.
  • Floating-point results can be approximate, so numeric representation should match the domain.

Next chapter: Strings — where Python values become readable messages, labels, paths, and user-facing text.

Official references used in this chapter