Variables and types
Every program needs to remember things. A quiz needs the player's name. A game needs the current score. A weather script needs the city you're checking. Python uses variables for this: names you attach to values so you can use them throughout your program.
player_name = "Alice"
score = 0
city = "Tokyo"Three lines. Three things Python now remembers. Use any of those names later and Python hands you back the value.
Storing a value
The = sign trips up almost everyone coming from maths class. In Python, = does not mean "equals". It means store this value under this name. Read it left to right:
city = "Tokyo"city gets "Tokyo". You're telling Python: remember "Tokyo" and label it city.
You can replace a variable's value at any time. Python just uses the most recent one:
score = 0
score = 10 # score is now 10
score = 15 # score is now 15Naming your variables
You choose the name. Python has a few hard rules, and the community follows conventions worth adopting from day one. Clear names make code readable weeks later. Cryptic names cause pain.
Rules Python enforces:
- Letters, digits, and underscores only. No spaces or hyphens.
- Must start with a letter or underscore, never a digit
- Case-sensitive:
score,Score, andSCOREare three separate variables
Conventions everyone follows (PEP 8):
| Thing | Style | Example |
|---|---|---|
| Variables and functions | snake_case | user_name, total_price |
| Constants | UPPER_SNAKE_CASE | MAX_RETRIES, BASE_URL |
| Classes | PascalCase | UserAccount, DataLoader |
# clear names, readable at a glance
user_name = "Alice"
total_price = 49.99
is_logged_in = True
MAX_RETRIES = 3
# you'll regret these within an hour
x = "Alice"
tp = 49.99
b = TrueOne trap worth knowing early: do not name a variable after a Python built-in like list, input, type, or print. Python allows it, but you will silently break the built-in for the rest of that scope and the resulting errors are hard to trace.
What you can store
Python has four types you will use in almost every program. Python figures out which type you mean from how you write the value. You never declare a type explicitly.
Text (str)
Any text goes inside quote marks, single or double. The quotes tell Python you mean literal characters, not a variable name. Once created, a string cannot be changed in place. The Strings chapter covers everything you can do with them.
player_name = "Alice"
city = "Tokyo"
message = 'Game over'If your text contains an apostrophe, use double quotes to avoid having to escape it:
note = "It's a great day"
note = 'It\'s a great day' # same result, using an escapeWhole numbers (int)
Whole numbers go in without quotes or a decimal point. Python calls them integers. They can be as large as you need; Python handles arbitrarily big numbers without any special effort on your part.
score = 0
age = 28
population = 8_100_000_000 # underscores are just for readabilityDecimal numbers (float)
Any number with a decimal point is a float. They work as expected for most calculations. One thing to know: some decimal values cannot be stored exactly in binary, so you can get a tiny rounding error:
price = 4.99
temperature = 36.6
0.1 + 0.2 # 0.30000000000000004For everyday work this rarely matters. For financial calculations where fractions of a cent count, Python has a decimal module that handles it correctly. That is covered in the Numbers chapter.
True or False (bool)
Some things are simply on or off. Python uses booleans for this: exactly two values, True and False. They look minor at this stage, but every condition and branch in your program runs on a boolean.
is_logged_in = True
has_errors = FalsePython also treats certain values as if they were False when used in a condition: 0, 0.0, "", and None (Python's "no value here") all behave like False. Everything else behaves like True. This becomes useful in the Control flow chapter.
Checking and converting types
When you are not sure what type a value is, type() tells you. To check whether a value is a specific type, isinstance() is the more reliable tool:
print(type("hello")) # <class 'str'>
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type(True)) # <class 'bool'>
isinstance(42, int) # True
isinstance("hi", str) # TruePython does not mix types automatically. Concatenating a string and a number raises a TypeError:
score = 42
print("Your score is " + score) # TypeError
print("Your score is " + str(score)) # worksConvert explicitly using the type name as a function:
| Call | Result |
|---|---|
str(42) | "42" |
int(3.9) | 3 (truncates, does not round) |
float("3.14") | 3.14 |
int("3.14") | ValueError: cannot convert a decimal string to int directly |
int(float("3.14")) | 3 (convert to float first, then to int) |
bool(0) / bool("") | False |
In practice
All four types working together in a small script. The output lines use f-strings to embed values in text: put f before the opening quote and wrap any variable in {}. Python replaces it with the variable's actual value. You will learn them properly in the next chapter.
player_name = "Alice"
level = 3
accuracy = 0.94
is_premium = True
print(f"{player_name} is on level {level} with {accuracy:.0%} accuracy.")
print(f"Premium account: {is_premium}")The types matter because level + 1 works and player_name + 1 does not. Each variable holds exactly one kind of thing; Python will not silently mix them for you.

