PCC·02 · Chapter record
Variables & Simple Data Types
Chapter 2 — the data you'll work with in every program
Five sessions covering the data types you'll use constantly: variables, strings, numbers, and the comments that explain them. Each one explains the concept, lists the key terms, then gives you an exercise — try it yourself before revealing the code.
Variables & the Traceback
Concept
- A variable is a name connected to a value.
- It's often described as a box you store a value in, but it's more accurate to think of it as a label attached to a value — you can move the label to a new value at any time.
- Variable names can only contain letters, numbers, and underscores, and can't start with a number.
- Spaces aren't allowed — use underscores instead — and you should avoid Python's reserved keywords and built-in function names like
print. - Misspell a variable name Python hasn't seen before, and you get a NameError, shown as a traceback pointing at the exact line.
Key Terms
- variable
- a name connected to a value
- NameError
- raised when Python can't find a variable or function by the name you used
- traceback
- the report Python prints showing where and why an error happened
Exercise
Assign a greeting to a variable, then print that message.
Print that same variable, then reassign it to a new message and print it again.
Strings: Methods & f-strings
Concept
- A string is a series of characters inside single or double quotes.
- One of the simplest things you can do with a string is change its case using a method — an action Python performs on a value, written with a dot after it.
name.title()capitalizes each word,name.upper()makes it all caps, andname.lower()makes it all lowercase.- To insert a variable's value into a string, place an f immediately before the opening quote and wrap the variable name in braces:
f"{first_name} {last_name}". - These are called f-strings — Python replaces each
{name}with that variable's value, and you can even call a method right inside the braces, likef"Hello, {full_name.title()}!".
Key Terms
- string
- a series of characters inside quotes
- method
- an action performed on a value, written as value.method()
- f-string
- a string prefixed with f that inserts {variable} values directly
Exercise
Use a variable for someone's name, and print a short, friendly greeting to them.
Print that same name in lowercase, uppercase, and title case.
Print a quote you like along with who said it, keeping the quotation marks in the output.
Redo the quote exercise, but build the full quote-plus-author line into its own variable first, then print that variable.
Whitespace & Stripping
Concept
- Whitespace is any nonprinting character — spaces, tabs (
\t), and newlines (\n). - To a program,
'python'and'python 'are different strings, even though they look the same to you — extra whitespace can quietly break comparisons like checking a username at login. - Python removes it with three methods:
rstrip()strips trailing whitespace,lstrip()strips leading whitespace, andstrip()strips both. - Each one only changes the value temporarily — the original variable is untouched unless you reassign the result back to it, like
favorite_language = favorite_language.rstrip().
Key Terms
- whitespace
- nonprinting characters — spaces, tabs (\t), newlines (\n)
- rstrip() / lstrip() / strip()
- remove whitespace from the right, left, or both sides
Exercise
Store a name with extra whitespace at both ends (try mixing in \t and \n). Print it once as-is, then print it again after each of lstrip(), rstrip(), and strip().
Numbers: Arithmetic & Multiple Assignment
Concept
- Python handles the usual arithmetic —
+ - * /— plus**for exponents, and it respects order of operations, so2 + 3 * 4is 14, not 20. - Numbers with a decimal point are floats; whole numbers are ints.
- Mix a float into any operation, or use
/, and the result is always a float — even when the answer is a whole number, like4 / 2 → 2.0. - Assign several variables in one line by separating both the names and the values with commas:
x, y, z = 0, 0, 0. - By convention, a variable meant to never change is written in ALL_CAPS, like
MAX_CONNECTIONS = 5000— Python won't enforce that, but every Python programmer reads it as a signal not to reassign it.
Key Terms
- int / float
- a whole number vs. a number with a decimal point
- **
- the exponent operator — 3 ** 2 is 9
- multiple assignment
- assigning several variables in one line: x, y, z = 0, 0, 0
- constant
- a variable meant to stay fixed, written in ALL_CAPS by convention
Exercise
Write four print() calls, one each using +, -, *, and /, that all result in the number 8.
Store your favorite number in a variable, then print a message built around it.
Comments
Concept
- A comment is a note in plain English that Python ignores completely — it starts with a hash mark (#), and everything after it on that line doesn't run.
- As programs get longer, comments help explain your overall approach so that you (or anyone else) can understand it later without re-reading every line.
- The best comments explain why you did something, not what the code does — the code itself already shows what.
- A comment like
# apply a 10% loyalty discountis more useful than# multiply by 0.9, which just restates the line below it.
Key Terms
- comment
- a note starting with # that Python ignores when running the file
Exercise
Add a comment above one line of an earlier exercise explaining why that line exists, not what it does.