Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Integers represent whole numbers, while floats represent decimal values; `type()` reveals which one a variable holds.
Briefing
Python’s numeric toolbox hinges on two core types—integers and floats—and the practical rules for operating on them. Integers represent whole numbers (like 3), while floats represent decimal values (like 3.14). Python’s built-in `type()` function makes the distinction explicit, returning `int` for whole-number variables and `float` when decimals appear. That difference matters because it affects how arithmetic results behave, especially with division.
Arithmetic operators cover the everyday calculations: addition (`+`), subtraction (`-`), multiplication (`*`), and exponentiation (`**`). Division is where Python 3’s behavior stands out. Using `/` performs true division and preserves decimals—so `3 / 2` yields `1.5` in Python 3, whereas Python 2 would have dropped the decimal and returned `1`. When the goal is to discard the fractional part, floor division uses `//`, so `3 // 2` becomes `1`. For remainders, the modulo operator `%` returns what’s left after division; `3 % 2` gives `1`.
Modulo becomes especially useful for parity checks. Dividing any integer by 2 produces only two possible remainders: `0` or `1`. If `n % 2` equals `0`, the number is even; if it equals `1`, the number is odd. Examples like `2 % 2 = 0`, `3 % 2 = 1`, `4 % 2 = 0`, and `5 % 2 = 1` illustrate the pattern and show why this check is reliable.
Python also follows standard order of operations, with parentheses able to override precedence. Without parentheses, `3 * 2 + 1` evaluates multiplication first and produces `7`. With parentheses—`3 * (2 + 1)`—the addition happens first, changing the result to `9`. For updating values repeatedly, Python offers both the explicit increment form (`num = num + 1`) and a shorthand (`num += 1`). The same shorthand works with other operations, such as multiplying via `num *= 10`.
Built-in numeric functions round out the toolkit. `abs()` returns absolute value by removing negative signs (e.g., `abs(-3)` becomes `3`). `round()` rounds to the nearest integer by default, and it can take a second argument to control decimal precision—`round(3.75)` becomes `4`, while `round(23.8, 1)` rounds to one digit after the decimal.
Comparisons produce boolean `True`/`False` results, using operators like `==` (equality), `!=` (not equal), `>`, `<`, `>=`, and `<=`. A key detail is that equality uses `==`, not `=`—the single equals sign is assignment.
Finally, numeric bugs often come from type confusion: values that look like numbers may actually be strings. Adding `'100'` and `'200'` concatenates into `'100200'` rather than producing `300`. The fix is casting—converting string numerals into integers with `int(num1)` and `int(num2)`—so arithmetic behaves as expected. This foundation sets up the next step: working with sequences like lists, sets, and tuples.
Cornell Notes
Integers and floats are Python’s two main numeric types: integers hold whole numbers, while floats hold decimals. Arithmetic operators (`+`, `-`, `*`, `/`, `//`, `**`, `%`) determine how calculations behave, with `/` doing true division in Python 3 and `//` performing floor division. Modulo (`%`) is especially useful for parity: `n % 2 == 0` means even, and `n % 2 == 1` means odd. Comparisons (`==`, `!=`, `>`, `<`, `>=`, `<=`) return boolean values. When numeric-looking values arrive as strings, casting with `int()` is required to avoid string concatenation during addition.
How can Python code distinguish between an integer and a float?
What’s the practical difference between `/` and `//` in Python 3?
Why does `n % 2` work as an even/odd test?
What does `**` do, and how is it different from `*`?
What goes wrong when numbers are stored as strings, and how is it fixed?
How do parentheses change arithmetic evaluation in Python?
Review Questions
- When should code use `==` instead of `=` in comparisons, and what does each symbol do?
- Given `x = 7`, what are the results of `x // 2` and `x % 2`?
- If `a = '3.7'` and `b = '2'`, what casting steps would be needed before performing numeric addition?
Key Points
- 1
Integers represent whole numbers, while floats represent decimal values; `type()` reveals which one a variable holds.
- 2
In Python 3, `/` keeps decimals (true division), while `//` drops the fractional part (floor division).
- 3
Modulo `%` returns the remainder and is ideal for parity checks: `n % 2 == 0` for even, `n % 2 == 1` for odd.
- 4
Use parentheses to override order of operations, changing results like `3 * 2 + 1` versus `3 * (2 + 1)`.
- 5
Shorthand operators like `+=` and `*=` simplify common updates such as incrementing or scaling a variable.
- 6
`abs()` and `round()` provide quick numeric utilities for absolute value and rounding precision.
- 7
When numeric values are actually strings, cast them with `int()` (or appropriate numeric casting) to prevent string concatenation during arithmetic.