Get AI summaries of any video or article — Sign up free
Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data thumbnail

Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data

Corey Schafer·
4 min read

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.

TL;DR

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?

Use `type()` on the value. A variable like `num = 3` returns an integer type, while `num = 3.14` returns a float type. The difference is structural: integers are whole numbers, floats include decimal parts.

What’s the practical difference between `/` and `//` in Python 3?

`/` performs true division and keeps the decimal: `3 / 2` evaluates to `1.5`. `//` performs floor division and drops the fractional part: `3 // 2` evaluates to `1`. This distinction matters when later logic depends on whole-number results.

Why does `n % 2` work as an even/odd test?

Modulo returns the remainder after division. When dividing by 2, the remainder can only be `0` or `1`. If `n % 2` is `0`, the number is even; if it’s `1`, the number is odd. Examples: `2 % 2 = 0`, `3 % 2 = 1`, `4 % 2 = 0`, `5 % 2 = 1`.

What does `**` do, and how is it different from `*`?

`**` computes exponents (powers). For instance, `3 ** 2` equals `9` because it calculates 3 squared. By contrast, `*` multiplies numbers directly (e.g., `3 * 2` equals `6`).

What goes wrong when numbers are stored as strings, and how is it fixed?

If `num1 = '100'` and `num2 = '200'`, then `print(num1 + num2)` concatenates the strings into `'100200'` instead of adding numerically. Casting fixes it: convert with `num1 = int(num1)` and `num2 = int(num2)` so `num1 + num2` becomes `300`.

How do parentheses change arithmetic evaluation in Python?

Parentheses override the normal order of operations. `3 * 2 + 1` multiplies first, yielding `7`. But `3 * (2 + 1)` forces `2 + 1` to run first, yielding `9`.

Review Questions

  1. When should code use `==` instead of `=` in comparisons, and what does each symbol do?
  2. Given `x = 7`, what are the results of `x // 2` and `x % 2`?
  3. If `a = '3.7'` and `b = '2'`, what casting steps would be needed before performing numeric addition?

Key Points

  1. 1

    Integers represent whole numbers, while floats represent decimal values; `type()` reveals which one a variable holds.

  2. 2

    In Python 3, `/` keeps decimals (true division), while `//` drops the fractional part (floor division).

  3. 3

    Modulo `%` returns the remainder and is ideal for parity checks: `n % 2 == 0` for even, `n % 2 == 1` for odd.

  4. 4

    Use parentheses to override order of operations, changing results like `3 * 2 + 1` versus `3 * (2 + 1)`.

  5. 5

    Shorthand operators like `+=` and `*=` simplify common updates such as incrementing or scaling a variable.

  6. 6

    `abs()` and `round()` provide quick numeric utilities for absolute value and rounding precision.

  7. 7

    When numeric values are actually strings, cast them with `int()` (or appropriate numeric casting) to prevent string concatenation during arithmetic.

Highlights

Python 3’s `/` performs true division: `3 / 2` becomes `1.5`, while `3 // 2` becomes `1`.
Modulo `%` makes even/odd checks straightforward because dividing by 2 yields only remainders `0` or `1`.
Parentheses reliably override precedence, turning `3 * 2 + 1 = 7` into `3 * (2 + 1) = 9`.
`round()` can take a second argument to control decimal places, such as rounding to one digit after the decimal.
Casting fixes a common pitfall: `'100' + '200'` concatenates, but `int('100') + int('200')` produces `300`.

Topics

  • Integers vs Floats
  • Arithmetic Operators
  • Modulo and Parity
  • Order of Operations
  • Type Casting
  • Comparison Operators

Mentioned