Python, Concept by Concept

unit

Output & Variables

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: Oakland

By 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.

You will learn

  • 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!")

Output

Ready!

Notice

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")

Output

First instruction
Second instruction

Notice

Python 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")

Output

CLUB FACT CARD
Name: Maya
Favorite color: teal

Notice

A 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")

Output

Name

Notice

The message inside print() appears below the cell.

One twist: separate print() instructions make separate lines.

print("Name: Maya")
print("Hometown: Oakland")

Output

Name: Maya
Hometown: Oakland

Notice

Each 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")

Output

CLUB FACT CARD

Name: Maya
Hometown: Oakland

Notice

print() with nothing inside makes a blank line.

Printing several things

Minimal: give print() two pieces of text.

print("Name:", "Maya")

Output

Name: Maya

Notice

A comma makes print() put one space between the two pieces.

One twist: choose a different separator for several pieces.

print("red", "teal", "gold", sep=" / ")

Output

red / teal / gold

Notice

sep=" / " puts a slash with spaces between the colors.

Realistic: keep one status message on a single line.

print("Loading", end="")
print("...done")

Output

Loading...done

Notice

end="" keeps the first line open, so the next print() 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")

Output

CLUB FACT CARD

Notice

The comment explains the code, but only the title is output.

One twist: a short comment can sit after an instruction.

print("Name: Maya")  # first fact

Output

Name: Maya

Notice

Python 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")

Output

CLUB FACT CARD
Name: Maya
Hometown: Oakland

Notice

Useful 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")

Output

teal

Notice

The quotes tell Python that teal is text; the quotes are not printed.

One twist: matching single quotes work too.

print('Oakland')

Output

Oakland

Notice

Begin 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.")

Output

Favorite color: teal!
Maya's card is ready.

Notice

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")

Output

First line
Second line

Notice

\n starts a new output line inside the string.

One twist: use \t to indent text.

print("Title\tClub Card")

Output

Title	Club Card

Notice

\t inserts a tab between the two pieces of text.

Another twist: put double quotes inside a double-quoted string.

print("The sign says \"Welcome\".")

Output

The sign says "Welcome".

Notice

\" 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)
  |""")

Output

  ^
 (o)
  |

Notice

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)

Output

Maya

Notice

name = "Maya" is an assignment. Then name stands for the saved string, so print(name) shows Maya.

One twist: a new assignment replaces the old value.

color = "teal"
print(color)
color = "purple"
print(color)

Output

teal
purple

Notice

The 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)

Output

Maya
teal
Oakland

Notice

Three 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)

Output

Oakland

Notice

hometown is clearer than a vague name such as thing.

One twist: join words in a name with underscores.

favorite_color = "teal"
print(favorite_color)

Output

teal

Notice

favorite_color is a valid, readable name; favorite color would 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)

Output

Maya
teal
Oakland

Notice

The 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 yourself

name = input("Name: ")
print("Hello,", name)

Notice

The reply is saved in name as a string; the comma prints a space after Hello,.

One twist: use a specific prompt and a clear variable name.

Try it yourself

favorite_color = input("Favorite color: ")
print(favorite_color)

Notice

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 yourself

member_name = input("Name: ")
favorite_color = input("Favorite color: ")
home_city = input("Hometown: ")
print(member_name)
print(favorite_color)
print(home_city)

Notice

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)

Output

Name: Maya

Notice

+ 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)

Output

MayaChen
Maya Chen

Notice

Add 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)

Output

Name: Maya
Favorite color: teal
Hometown: Oakland

Notice

The 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 f before the opening quote and a variable name inside {}.

Minimal: place one variable in one sentence.

name = "Maya"
print(f"Hello, {name}!")

Output

Hello, Maya!

Notice

The f lets {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}.")

Output

Maya is from Oakland.

Notice

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}")

Output

CLUB FACT CARD
Name: Maya
Favorite color: teal
Hometown: Oakland

Notice

The 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 yourself

member_name = input("Name: ")
home_city = input("Hometown: ")
print(f"{member_name} is from {home_city}.")

Notice

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.

Read the error

print("Name: Maya)

Read from the bottom: SyntaxError tells us Python could not finish reading the instruction. Match the opening and closing quotes, then run the fixed cell.

print("Name: Maya")

Output

Name: Maya

Notice

The error named the kind of problem; the fixed line has a matching quote at each end.

One twist: a variable name changes spelling.

Read the error

favorite_color = "teal"
print(f"Favorite color: {favourite_color}")

The final traceback line says NameError and points to favourite_color. Compare that spelling with the saved name above it.

favorite_color = "teal"
print(f"Favorite color: {favorite_color}")

Output

Favorite color: teal

Notice

Python needs the variable name to have the exact same spelling every time.

Realistic: a card line is missing the + that joins two strings.

Read the error

city = "Oakland"
print("Hometown: " city)

The traceback ends with SyntaxError because two string pieces sit together with no joining operator. Add + between them.

city = "Oakland"
print("Hometown: " + city)

Output

Hometown: Oakland

Notice

The 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}")

Output

CLUB FACT CARD
Name: Maya
Favorite color: teal
Hometown: Oakland

Notice

Saved 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.

Recap

  • 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.

Go to the exercises (20)

For parents and teachers: report a problem