Skip to content

Lesson 04

Python Integers and Floats

How numbers behave in Python — arithmetic, conversion, comparison — and why a humanist needs them.

Now that you can read and write strings, let’s look at the other half of the data world: numbers. Python has two basic numeric types, integers and floats. The difference is simple — integers are whole numbers, floats have a decimal point — but the consequences for analysis are real.

Why a humanist needs numbers

It’s tempting to think numbers are for the social scientists down the hall. They aren’t. Almost any humanities project ends up counting something:

  • How many letters did Voltaire send to England, and to whom?
  • How does the average sentence length in Hemingway compare to James?
  • How often does a particular topic word appear in a corpus of sermons?
  • Which characters in a novel share scenes the most?

Stanford’s Republic of Letters project is a good demonstration of what counting can reveal: the visualizations are essentially aggregations of integers — letters per year, letters per correspondent — built from a textual archive. Once you can manipulate numbers in Python, every humanities dataset you touch becomes available for that kind of question.

Creating numbers

There is no special syntax. Type a number on the right of an = sign and Python figures out the type from whether you wrote a decimal point.

year = 1988          # int
average = 0.42       # float
zero_decimal = 1.0   # still a float — the .0 is enough
print(year, average, zero_decimal)

You can check the type at any time with type:

year = 1988
average = 0.42

print(type(year))      # <class 'int'>
print(type(average))   # <class 'float'>

For long literals, Python lets you sprinkle in underscores as visual separators:

million = 1_000_000   # same as 1000000, just easier to read
print(million)

Converting between types

Three built-in conversion functions cover almost all cases: int, float, and str.

print(int(1.7))        # 1   — drops the decimal, does NOT round
print(float(1))        # 1.0
print(str(1988))       # "1988"

A few things worth pinning down:

  • int(1.7) returns 1, not 2. It truncates toward zero. If you want rounding, use the built-in round: round(1.7) returns 2.
  • int("1988") works — string-to-int conversion succeeds when the string is a clean number. int("1988 ") with extra whitespace also works (it strips). int("nineteen") raises a ValueError.
  • str(1988) is what you reach for when you need to glue a number into a filename: f"letter_{n}.txt".

Arithmetic

The seven arithmetic operators you’ll use most:

OperationOperatorExampleResult
Addition+7 + 29
Subtraction-7 - 25
Multiplication*7 * 214
Exponent**7 ** 249
Division/7 / 23.5
Floor division//7 // 23
Modulo%7 % 21

Two notes that catch people out:

  • / always returns a float, even when the numbers divide cleanly. 4 / 2 is 2.0, not 2. If you want an integer quotient, use //.
  • % (modulo) returns the remainder. It’s how you check whether a number is even (n % 2 == 0), or pick every tenth item in a loop, or wrap around a fixed range.

You can also combine an operator with assignment to update a variable in place:

total = 0
total += 1     # same as total = total + 1
total *= 10    # same as total = total * 10
print(total)

Comparison

Comparisons return a Boolean — True or False. You’ll use these constantly inside if statements and loops (Lessons 10 and 11).

ComparisonOperator
Equal==
Not equal!=
Greater than>
Greater than or equal>=
Less than<
Less than or equal<=
print(1815 < 1850)        # True
print(1815 == 1850)       # False
print(1815 != 1850)       # True

The single-most common mistake for new Python users: writing = (assignment) when you mean == (comparison). The first changes a variable, the second asks a question.

Floats are approximate

This last point trips everyone up at least once. Run this in the console:

print(0.1 + 0.2)

Python returns 0.30000000000000004, not 0.3. This isn’t a bug — it’s how floating-point numbers work in every modern language. They use binary fractions, which can’t represent some decimal numbers exactly.

For humanities data this rarely matters — you’re not splitting hairs at the fifteenth decimal. But it does mean: never compare two floats with ==. Compare them with a tolerance instead:

print(abs((0.1 + 0.2) - 0.3) < 1e-9)   # True

Or, when you’re storing money or any value that has to be exact, use decimal.Decimal from the standard library.

Once these basics are solid, continue to Lesson 06: Python Tuples.

Running the code

Save any snippet from this lesson to a file — say try.py — and run it from your project folder:

uv run try.py

uv run uses the project’s Python and dependencies automatically; no virtualenv to activate. If you haven’t set the project up yet, Lesson 01 walks through it.