Appearance
Variables and types
A variable is a named container for a value. You create one with =:
python
name = "Alice"
age = 30
is_active = True
If you've come across variables before in another language, Python's version will feel familiar — with one notable difference. You don't declare a type upfront. There's no string name or int age. Python looks at the value you assign and figures out the type itself. This is called dynamic typing, and it keeps your code noticeably shorter.
The core types
Python has five types you'll work with constantly:
| Type | Example | What it holds |
|---|---|---|
str | "hello" | Text |
int | 42 | Whole numbers |
float | 3.14 | Decimal numbers |
bool | True / False | Yes/no values |
NoneType | None | Absence of a value |
str
Text wrapped in quotes. Single or double — Python treats them identically, so use whichever you prefer and stay consistent.
python
first_name = "Alice"
last_name = 'Smith'
For multi-line text, triple quotes keep things readable:
python
bio = """
Alice is a software engineer
based in Melbourne.
"""
Strings are immutable — you can't change individual characters in place. Any operation that looks like it modifies a string is actually creating a new one.
int and float
The difference is simple: integers are whole numbers, floats have a decimal point.
python
score = 100
year = 2024
price = 9.99
tax_rate = 0.08
One handy trick — underscores in numbers are ignored by Python but make large values much easier to read:
python
population = 8_200_000_000 # same as 8200000000
Watch out with floats
0.1 + 0.2 gives you 0.30000000000000004 in Python, not 0.3. This isn't a Python quirk — it's a fundamental limitation of how decimal numbers are stored in binary. For anything that needs exact precision (money, for example), use the decimal module instead of plain floats.
bool
True or False — always capitalised. Booleans are the backbone of any decision-making in code.
python
is_logged_in = True
has_paid = False
Worth knowing: under the hood, True equals 1 and False equals 0. This comes up occasionally in arithmetic and comparisons, so it's good to have in the back of your mind.
None
None represents the absence of a value — not zero, not an empty string, genuinely nothing. It's commonly used to initialise a variable before you have real data to put in it, or to signal that a function has nothing useful to return.
python
result = None
current_user = None
Checking the type
type() tells you what type a value is:
python
print(type("hello")) # <class 'str'>
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
In real code you'll more often use isinstance() when you actually need to act on the type:
python
if isinstance(age, int):
print("Got a whole number")
Converting between types
Python won't automatically convert types for you — if you try to add a number to a string, you'll get an error. You need to be explicit:
python
int("42") # 42
float("3.14") # 3.14
str(100) # "100"
bool(0) # False
bool("hello") # True
This comes up more than you might expect. Any time you read input from a user or a file, it arrives as a string — even if they typed a number. Converting it before doing maths on it is a very common pattern:
python
user_input = "25" # this came from somewhere as text
age = int(user_input) # now it's a number you can do maths with
next_year = age + 1 # works fine
Conversion fails loudly if the value doesn't make sense for the target type:
python
int("hello") # ValueError: invalid literal for int()
int("3.5") # ValueError — convert to float first, then int
Dynamic typing and reassignment
Because types are attached to values rather than variable names, you can reassign a variable to a completely different type:
python
x = 10
x = "ten" # perfectly valid in Python
x = True # also fine
This flexibility is useful but can bite you in larger codebases where you lose track of what a variable is supposed to hold. If that's a concern, Python 3.5+ lets you add type hints as documentation:
python
name: str = "Alice"
age: int = 30
These don't enforce anything at runtime — they're signals for you, your team, and tools like mypy.
Naming conventions
Python's community has strong conventions around naming. Following them makes your code immediately readable to other Python developers:
snake_casefor variables and functions:user_name,total_price,is_activeALL_CAPSfor constants:MAX_RETRIES,BASE_URL,PIPascalCasefor class names:UserProfile,ShoppingCart
Names are case-sensitive — score, Score, and SCORE are three different variables.
Multiple assignment
Python lets you assign multiple variables in one line, which is often cleaner than separate statements:
python
a, b, c = 1, 2, 3
# Swap without a temp variable
a, b = b, a
# Same value across multiple names
x = y = z = 0