PyPython · Lesson 2 of 14

Variables & Data Types

Python is dynamically typed, which means you don't declare types — Python figures it out. This is either liberating or terrifying depending on your background.

Variables in Python are created by assignment. There's no need for keywords like var, let, or int. The built-in types cover all common use cases: integers, floats, strings, booleans, and None (Python's null).

Python
# Integer
age = 25
year = 2024

# Float
pi = 3.14159
temperature = -10.5

# String — single or double quotes are equivalent
name = "Alice"
greeting = 'Hello, world'

# Multi-line string
bio = """I'm Alice.
I like Python.
I am a multi-line string."""

# Boolean
is_student = True
has_car = False

# None (the absence of a value)
result = None

# Check a variable's type
print(type(age))         # <class 'int'>
print(type(pi))          # <class 'float'>
print(type(name))        # <class 'str'>
print(type(is_student))  # <class 'bool'>

Python supports multiple assignment in one line, and you can swap variables without a temporary variable — a small but delightful trick.

Python
# Multiple assignment
x, y, z = 1, 2, 3

# Swap without a temp variable
x, y = y, x
print(x, y)   # 2 1

# String formatting — f-strings are the modern way
name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")

# Expressions work inside f-strings
print(f"Next year I'll be {age + 1}.")

# Type conversion
num_str = "42"
num = int(num_str)       # str -> int
flt = float(num_str)     # str -> float
back = str(num)          # int -> str
◆ Note
Python uses snake_case for variable names (my_variable, not myVariable). Constants are written in UPPER_SNAKE_CASE by convention (MAX_RETRIES = 3), though Python doesn't enforce immutability.