Unit 1 · Fundamentals

Lesson · Unit 1 · 8 min read

Data types and variables, the four types you'll use 95% of the time.

Every value in Python has a type, and Python is strict about which types do what. Get this lesson right and most type-related bugs disappear from your code forever.

Section · 01

The four types you'll actually use

Python has more types than this, but four cover almost every line of code you’ll write in the first year:

name = "Ada Lovelace"        # str   — text
age = 36                     # int   — whole number
balance = 1287.45            # float — number with a decimal
is_active = True             # bool  — True or False

Plus a fifth that means “no value yet”:

last_login = None            # NoneType — used as a placeholder

Check what type something is with the type() function. This is useful when a value isn’t doing what you expected:

type(name)        # <class 'str'>
type(age)         # <class 'int'>
type(balance)     # <class 'float'>
type("36")        # <class 'str'>  — quotes win, even if it looks like a number
type(36)          # <class 'int'>

That second-to-last one is the trap. "36" and 36look identical to you but are completely different to Python — one is text, one is a number. You can’t do math on text.

Section · 02

Variables are just names for values

A variable is a label pointing at a value. You bind one with =:

subtotal = 49.99
tax_rate = 0.0725
total = subtotal + (subtotal * tax_rate)
print(total)    # 53.61...

Two things to know about =:

1. It’s not equality. The single =is assignment — “point the name on the left at the value on the right.” The double ==is equality — “are these two things equal?” Mixing them up is a daily occurrence at first.

2. The right side runs first. Python evaluates everything to the right of =, then assigns the result. That’s why x = x + 1works — the right side becomes “old x plus one”, then the result is bound to x.

Section · 03

The rules for naming

Python is strict about what counts as a valid name. Three hard rules:

user_id = 1          # OK — letters, digits, underscores
_internal = "..."    # OK — leading underscore is fine
score2 = 100         # OK — digits anywhere except the start

2score = 100         # SYNTAX ERROR — can't start with a digit
user-id = 1          # SYNTAX ERROR — hyphens are not allowed (that's minus)
class = "Math 101"   # SYNTAX ERROR — 'class' is a reserved word

And one rule that isn’t enforced by Python but is enforced by every other Python developer who’ll read your code:

# Use snake_case for variables and functions
user_age = 32              # ✓
first_login_at = "..."     # ✓

# NOT camelCase or PascalCase for ordinary variables
userAge = 32               # technically works, but everyone will hate it
FirstLoginAt = "..."       # looks like a class, will confuse readers

Capitalization matters: userId, UserId, and USERIDare three different variables. This causes bugs you’ll spend 20 minutes looking for before you spot the typo. Be consistent.

Section · 04

Converting between types

When data comes in from outside your program — a form, a file, a web request — it almost always arrives as a string. Your job is to convert it to the right type before doing anything with it:

age_str = input("Your age? ")       # always a string
age = int(age_str)                  # str → int

price_str = "19.99"
price = float(price_str)            # str → float

count = 7
label = str(count)                  # int → str, for printing/concatenation

Each conversion can fail. int("hello") raises a ValueError. float("19.99") works; int("19.99")does not (you can’t go directly from a decimal string to an int — convert to float first, then int).

The classic beginner bug

# This looks fine but produces "55" instead of 10:
five_str = input("First number: ")    # user types "5", gets "5"
also_five = input("Second number: ")  # user types "5", gets "5"
print(five_str + also_five)           # "55"  — string concatenation!

# Convert first:
n1 = int(input("First number: "))     # int
n2 = int(input("Second number: "))    # int
print(n1 + n2)                        # 10   — numeric addition

+ means different things depending on the types on either side. Two strings? Concatenation. Two numbers? Addition. A string and a number? TypeError. Python won’t guess what you meant.

Curriculum source

Lesson content is original to YorkSims. Topic structure aligns with Python for Everybody by Dr. Charles R. Severance (py4e.com), licensed under Creative Commons Attribution 3.0 Unported.