int)A whole number such as 0, 12, or -5. Use integers naturally for counts, indexes, retry limits, and other whole-number quantities.
Learn with diagrams, code, systems and practical examples.
Work with integers, decimals, arithmetic, and comparisons.
Many programs calculate totals, compare limits, measure durations, or transform numeric data.
Python supports integers and floating-point numbers along with familiar arithmetic operators. Comparison operators produce Boolean values such as True or False.
Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.
price = 12.50
quantity = 4
total = price * quantity
is_large_order = total >= 40
print(total)
print(is_large_order) Python supports integers and floating-point numbers along with familiar arithmetic operators. Comparison operators produce Boolean values such as True or False.
50.0
TrueThe example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.
Assuming decimal floating-point arithmetic always behaves like exact money arithmetic.
For financial software, use an appropriate decimal or integer-minor-unit strategy instead of blindly relying on binary floating point.
These are deliberately small. If you can complete them without copying the example, you are ready to continue.
Prepared deep chapter · Core Python
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.
Python has several numeric types. In everyday beginner programs, the two you will meet most often are int and float.
int)A whole number such as 0, 12, or -5. Use integers naturally for counts, indexes, retry limits, and other whole-number quantities.
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__) int
floatPython 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.
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) 50.0| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 7 + 3 | 10 |
- | Subtract | 7 - 3 | 4 |
* | Multiply | 7 * 3 | 21 |
/ | True division | 7 / 3 | 2.333... |
// | Floor division | 7 // 3 | 2 |
% | Remainder | 7 % 3 | 1 |
** | Power | 2 ** 3 | 8 |
The symbols /, //, and % all involve division, but they answer different questions.
minutes = 95
hours = minutes / 60
print(hours)/ produces the quotient and integer division still produces a float.
items = 17
box_size = 5
full_boxes = items // box_size
print(full_boxes)// uses floor division. Here it says three full boxes fit.
leftover = 17 % 5
print(leftover)% gives the remainder. Here two items are left after filling three boxes.
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.
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) 20
60When 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.
-1**2 can surprise youExponentiation 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.
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) 0.30000000000000004
FalseThis 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.
Choose the numeric representation from the domain requirement. Do not automatically use float just because a value contains a decimal point.
^ for powersIn Python, ^ is bitwise XOR. Use ** for exponentiation: 2 ** 3.
10 / 0, 10 // 0, and 10 % 0 raise ZeroDivisionError. Validate a divisor when it comes from input.
/ keeps integers8 / 2 produces 4.0. If result type matters, predict it before using the value elsewhere.
Some calculated floats should be compared with an appropriate tolerance rather than exact equality. The right strategy depends on the domain.
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) 3
11
TrueThe 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.
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.
3 + 4 * 2, then compare it with (3 + 4) * 2.0.1 + 0.2 == 0.3. Explain why the result is not evidence that Python arithmetic is broken.int represents integers; float represents binary floating-point values./, //, and % answer different division questions.** means exponentiation; ^ does not.Next chapter: Strings — where Python values become readable messages, labels, paths, and user-facing text.