A club needs a tidy fact card for its welcome board. Right now the details are scattered: a name, a favorite color, and a hometown. Can you make the computer fill in a neat card like this?
CLUB FACT CARD Name: Maya Favorite color: teal Hometown: OaklandBy the end, changing three saved values will update the whole card.
We will build that payoff in three lessons. Each idea climbs the same ladder: minimal, one twist, then realistic. Read every Notice line before moving up.
-
Run a Python cell and use
print()to show a message. -
Write comments and text with quotes, including special characters inside strings.
-
Save values under clear variable names and ask for input.
-
Join text with
+or an f-string, and use an error message to find a mistake.
-
Lesson 1: Make Output Appear
Run a program
A program is a set of instructions. Running a notebook cell asks Python to follow the instructions in that cell from top to bottom.
Minimal: select the next cell and run it once.
print("Ready!")Scroll the code for more ↓
Output
Ready!One run follows the instruction once and shows one line.
One twist: one cell can hold more than one instruction.
print("First instruction") print("Second instruction")Scroll the code for more ↓
Output
First instruction Second instructionPython follows the lines in order, from the top down.
Realistic: run a tiny preview of the card we are building.
print("CLUB FACT CARD") print("Name: Maya") print("Favorite color: teal")Scroll the code for more ↓
Output
CLUB FACT CARD Name: Maya Favorite color: tealA useful program is still just clear instructions run in order.
Print output
print()puts a message where you can see it.Minimal: print one short label.
print("Name")Scroll the code for more ↓
Output
NameThe message inside
print()appears below the cell.One twist: separate
print()instructions make separate lines.print("Name: Maya") print("Hometown: Oakland")Scroll the code for more ↓
Output
Name: Maya Hometown: OaklandEach
print()starts a new output line.Realistic: an empty
print()can add breathing room.print("CLUB FACT CARD") print() print("Name: Maya") print("Hometown: Oakland")Scroll the code for more ↓
Output
CLUB FACT CARD Name: Maya Hometown: Oaklandprint()with nothing inside makes a blank line.Printing several things
Minimal: give
print()two pieces of text.print("Name:", "Maya")Scroll the code for more ↓
Output
Name: MayaA comma makes
print()put one space between the two pieces.One twist: choose a different separator for several pieces.
print("red", "teal", "gold", sep=" / ")Scroll the code for more ↓
Output
red / teal / goldsep=" / "puts a slash with spaces between the colors.Realistic: keep one status message on a single line.
print("Loading", end="") print("...done")Scroll the code for more ↓
Output
Loading...doneend=""keeps the first line open, so the nextprint()finishes it.Comments for readers
A comment is a note for people who read the program. It begins with
#, and Python does not print it.Minimal: put a note on its own line.
# Show the card title. print("CLUB FACT CARD")Scroll the code for more ↓
Output
CLUB FACT CARDThe comment explains the code, but only the title is output.
One twist: a short comment can sit after an instruction.
print("Name: Maya") # first factScroll the code for more ↓
Output
Name: MayaPython follows the instruction before
#and skips the note after it.Realistic: comments can divide a program into readable parts.
# Heading print("CLUB FACT CARD") # Details print("Name: Maya") print("Hometown: Oakland")Scroll the code for more ↓
Output
CLUB FACT CARD Name: Maya Hometown: OaklandUseful comments describe a section's job instead of repeating every instruction.
String literals
Text between matching quotes is a string literal. The quotes mark where the text begins and ends.
Minimal: use matching double quotes.
print("teal")Scroll the code for more ↓
Output
tealThe quotes tell Python that
tealis text; the quotes are not printed.One twist: matching single quotes work too.
print('Oakland')Scroll the code for more ↓
Output
OaklandBegin and end with the same kind of quote.
Realistic: spaces and punctuation inside the quotes are part of the string.
print("Favorite color: teal!") print("Maya's card is ready.")Scroll the code for more ↓
Output
Favorite color: teal! Maya's card is ready.Python preserves the colon, space, apostrophe, period, and exclamation mark exactly.
Special characters
Minimal: put a newline inside one string with
\n.print("First line\nSecond line")Scroll the code for more ↓
Output
First line Second line\nstarts a new output line inside the string.One twist: use
\tto indent text.print("Title\tClub Card")Scroll the code for more ↓
Output
Title Club Card\tinserts a tab between the two pieces of text.Another twist: put double quotes inside a double-quoted string.
print("The sign says \"Welcome\".")Scroll the code for more ↓
Output
The sign says "Welcome".\"puts a visible double quote in the output without ending the string.Realistic: a triple-quoted string can hold a small three-line picture.
print(""" ^ (o) |""")Scroll the code for more ↓
Output
^ (o) |The picture has three rows inside one triple-quoted string; its first row begins immediately after the opening quotes.
Lesson 2: Save Values
Variables
A variable is a name that saves a value for later. An assignment is an instruction that uses
=to save a value in a variable.Minimal: save one string, then print the variable.
name = "Maya" print(name)Scroll the code for more ↓
Output
Mayaname = "Maya"is an assignment. Thennamestands for the saved string, soprint(name)showsMaya.One twist: a new assignment replaces the old value.
color = "teal" print(color) color = "purple" print(color)Scroll the code for more ↓
Output
teal purpleThe most recently saved value is the one the variable remembers.
Realistic: save each fact once, then reuse it.
name = "Maya" favorite_color = "teal" hometown = "Oakland" print(name) print(favorite_color) print(hometown)Scroll the code for more ↓
Output
Maya teal OaklandThree variables keep three facts ready for the card.
Clear names
A clear variable name tells a reader what the saved value means. Names may use letters, numbers after the first character, and underscores, but not spaces. Python cares about exact spelling and capitalization.
Minimal: choose a name that describes the value.
hometown = "Oakland" print(hometown)Scroll the code for more ↓
Output
Oaklandhometownis clearer than a vague name such asthing.One twist: join words in a name with underscores.
favorite_color = "teal" print(favorite_color)Scroll the code for more ↓
Output
tealfavorite_coloris a valid, readable name;favorite colorwould contain a forbidden space.Realistic: a group of precise names makes the card easy to update.
member_name = "Maya" favorite_color = "teal" home_city = "Oakland" print(member_name) print(favorite_color) print(home_city)Scroll the code for more ↓
Output
Maya teal OaklandThe names explain the data even before the output appears.
Input from a person
input()pauses a program and waits for someone to type a reply. Run the next cells yourself and type an answer when each one asks.Minimal: collect one reply and greet the person.
Try it name = input("Name: ") print("Hello,", name)Scroll the code for more ↓
The reply is saved in
nameas a string; the comma prints a space afterHello,.One twist: use a specific prompt and a clear variable name.
Try it favorite_color = input("Favorite color: ") print(favorite_color)Scroll the code for more ↓
The prompt tells the person what to type; the variable tells the programmer what was saved.
Realistic: collect the three facts needed for a card.
Try it member_name = input("Name: ") favorite_color = input("Favorite color: ") home_city = input("Hometown: ") print(member_name) print(favorite_color) print(home_city)Scroll the code for more ↓
Each question has its own reply and variable. The exercises use fixed values so their cells can run on their own.
Lesson 3: Assemble the Card
String concatenation
An operator is a symbol that tells Python to perform an action. String concatenation uses the
+operator to join strings into one longer string.Minimal: join a label and a saved value.
name = "Maya" print("Name: " + name)Scroll the code for more ↓
Output
Name: Maya+glues the label string and the saved string into one output line.One twist: concatenation adds no spaces by itself.
first_name = "Maya" last_name = "Chen" print(first_name + last_name) print(first_name + " " + last_name)Scroll the code for more ↓
Output
MayaChen Maya ChenAdd a string containing one space when the finished text needs a space.
Realistic: build several exact card lines.
member_name = "Maya" favorite_color = "teal" home_city = "Oakland" print("Name: " + member_name) print("Favorite color: " + favorite_color) print("Hometown: " + home_city)Scroll the code for more ↓
Output
Name: Maya Favorite color: teal Hometown: OaklandThe fixed labels stay in string literals while the changing facts come from variables.
F-strings
An f-string puts saved values inside a string. Put
fbefore the opening quote and a variable name inside{}.Minimal: place one variable in one sentence.
name = "Maya" print(f"Hello, {name}!")Scroll the code for more ↓
Output
Hello, Maya!The
flets{name}become the saved value inside the string.One twist: one f-string can use more than one variable.
name = "Maya" city = "Oakland" print(f"{name} is from {city}.")Scroll the code for more ↓
Output
Maya is from Oakland.Each pair of braces marks a place where Python inserts a value.
Realistic: format the whole fact card with f-strings.
member_name = "Maya" favorite_color = "teal" home_city = "Oakland" print("CLUB FACT CARD") print(f"Name: {member_name}") print(f"Favorite color: {favorite_color}") print(f"Hometown: {home_city}")Scroll the code for more ↓
Output
CLUB FACT CARD Name: Maya Favorite color: teal Hometown: OaklandThe output has the same visible payoff as the opening, but now three values control all the changing details.
Try real input: ask for two card facts and put both in one f-string.
Try it member_name = input("Name: ") home_city = input("Hometown: ") print(f"{member_name} is from {home_city}.")Scroll the code for more ↓
The two typed replies fill the same braces that fixed values filled in the card above.
Read error messages
Errors are clues, not disasters. When Python stops, it shows a red error report called a traceback. Look at the traceback's final line first: it names the error and describes the problem. The broken examples below are shown as text so they don't run on their own. Read each one, predict the problem, then run its fixed partner.
Minimal: a string is missing its closing quote.
Error demo: this code stops with an error print("Name: Maya)Scroll the code for more ↓
Read from the bottom:
SyntaxErrortells us Python could not finish reading the instruction. Match the opening and closing quotes, then run the fixed cell.print("Name: Maya")Scroll the code for more ↓
Output
Name: MayaThe error named the kind of problem; the fixed line has a matching quote at each end.
One twist: a variable name changes spelling.
Error demo: this code stops with an error favorite_color = "teal" print(f"Favorite color: {favourite_color}")Scroll the code for more ↓
The final traceback line says
NameErrorand points tofavourite_color. Compare that spelling with the saved name above it.favorite_color = "teal" print(f"Favorite color: {favorite_color}")Scroll the code for more ↓
Output
Favorite color: tealPython needs the variable name to have the exact same spelling every time.
Realistic: a card line is missing the
+that joins two strings.Error demo: this code stops with an error city = "Oakland" print("Hometown: " city)Scroll the code for more ↓
The traceback ends with
SyntaxErrorbecause two string pieces sit together with no joining operator. Add+between them.city = "Oakland" print("Hometown: " + city)Scroll the code for more ↓
Output
Hometown: OaklandThe line named by the traceback is where Python got stuck; checking quotes, names, and joining operators gives you a focused repair plan.
Final build
First run the card as shown. Then make it yours: replace Maya, teal, and Oakland with your own name, favorite color, and city, and run the cell again. The labels and layout stay tidy while the whole card updates.
# Change these values to make a new card. member_name = "Maya" favorite_color = "teal" home_city = "Oakland" # Print the finished card. print("CLUB FACT CARD") print(f"Name: {member_name}") print(f"Favorite color: {favorite_color}") print(f"Hometown: {home_city}")Scroll the code for more ↓
Output
CLUB FACT CARD Name: Maya Favorite color: teal Hometown: OaklandSaved values separate the facts that change from the formatting that stays the same. That is the payoff: one small edit updates every place that uses a variable.
-
Run a cell to follow its instructions;
print()shows the result. -
Quoted text is a string;
#begins a comment that Python ignores. -
A clear variable name lets you change a saved fact in one place.
-
input()reads typed text;+and f-strings put text together. -
The final traceback line helps you locate and repair a mistake.
You can now build a club fact card whose details are easy to change.
-