02 Variables, Output, and Errors¶
In this notebook, you will store information, show messages, and practice reading small errors.
Run in Binder Open in Colab Download notebook
Read here. Practice in a notebook.
This page shows the original notebook. Open Binder or Colab to run and edit cells. Work one cell at a time: some examples are deliberately broken for debugging practice. Download your work before leaving Binder; its sessions are temporary.
Learning Objectives¶
By the end, you should be able to:
- Describe a variable as a labeled container.
- Use
print()to communicate with the user. - Recognize a few common beginner errors.
- Fix small mistakes with a calm step-by-step approach.
Run one cell at a time: Some cells in this notebook are broken on purpose so you can practice reading errors. Run one cell at a time instead of using Run All.
Note: Errors are a normal part of programming. Even experienced programmers make errors. The goal is not to avoid every error, but to learn how to read and fix them.
What is a Variable?¶
A variable is like a labeled container that stores information your program can use later. The label is the variable name, and the stored information is the value.
Think Before You Run: What do you think this code will print for the username and score?
# Variables store information for later use.
username = "alex"
score = 92
print("Username:", username)
print("Score:", score)
Using print() to Show Output¶
print() is how a Python program communicates with the user. It can show a message, a variable value, or both together.
Think Before You Run: Which lines will show a message, and which line will show a stored value?
# print() can show messages and stored values.
username = "Jordan"
print("Welcome to IT1009C")
print("Current user:", username)
print("System message: setup is ready")
Basic Variable Examples¶
Useful variable names describe what the value means. In IT examples, names like server_name and disk_usage are easier to understand than vague names.
Think Before You Run: What system information do you expect to see?
# Store simple IT-related values.
server_name = "Campus-Web-01"
disk_usage = 72
system_status = "online"
# Show the values in a readable way.
print("Server:", server_name)
print("Disk usage percent:", disk_usage)
print("Status:", system_status)
Guided Practice¶
Change only the values on the right side of the equals signs. Then run the cell again and check the output.
Think Before You Run: If you change tool_name, what output should change?
# TODO: Change these starter values.
tool_name = "Help Desk Portal"
user_role = "student"
room_number = "Lab 204"
# Print a short IT support message.
print("Tool:", tool_name)
print("Role:", user_role)
print("Room:", room_number)
Errors Are Normal¶
Errors are expected, useful, and part of learning Python. Each error message is a clue that can help you find one small thing to fix.
Exploring Common Errors¶
The next examples are broken on purpose. Run the broken code, read the message, then compare it with the corrected version.
NameError¶
Think Before You Run: Will this code run successfully if server_status was never created?
# Broken on purpose: server_status has not been defined.
print("Server status:", server_status)
A NameError often means Python does not know that variable name. Create the variable before using it.
# Corrected version
server_status = "online"
print("Server status:", server_status)
SyntaxError¶
Think Before You Run: What small typing mistake do you notice in this print statement?
# Broken on purpose: the closing quote is missing.
print("Disk check complete)
A SyntaxError means Python cannot understand the code as typed. Check quotes, parentheses, brackets, and colons first.
# Corrected version
print("Disk check complete")
TypeError¶
Think Before You Run: What error might happen when text and a number are combined with +?
# Broken on purpose: this tries to add text and a number.
disk_message = "Disk usage is " + 72
print(disk_message)
A TypeError can happen when values do not work together. For beginner output, using commas in print() is a simple fix.
# Corrected version
disk_usage = 72
print("Disk usage is", disk_usage)
How to Read Errors¶
Start with the last line of the error message. Identify the error name, then check the line Python mentioned.
Use this simple process:
- Look at the last line.
- Find the error name.
- Check the line mentioned.
- Fix one small thing and run again.
Debugging Practice¶
Fix one small issue at a time. Run the cell, read the error, fix one small issue, and run it again.
Think Before You Run: What variable name mismatch do you see?
# TODO: Fix the variable name so both lines use the same label.
server_name = "Campus-File-02"
print("Checking server:", server_nam)
Think Before You Run: What symbol is missing from the text value?
# TODO: Fix the missing quote.
tool_name = "Ticket System
print("Favorite tool:", tool_name)
Think Before You Run: How can this message show text and a number without causing a TypeError?
# TODO: Fix this by using a comma inside print().
disk_usage = 80
print("Disk usage is " + disk_usage)
Parsons Problem¶
The lines below are scrambled. Put them in the correct order in the next cell.
print("Tool:", favorite_tool)
favorite_tool = "Browser"
username = "Ava"
print("User:", username)
Think Before You Run: Which lines must happen before the print() lines?
# TODO: Reorder the scrambled lines from above.
# Create the variables before printing them.
username = "Ava"
favorite_tool = "Browser"
print("User:", username)
print("Tool:", favorite_tool)
Mini Lab¶
Create a short message using at least two variables. Use a practical context such as your name and favorite tool, or a username and system role.
Think Before You Run: What message should your variables help print?
# Mini Lab
# 1. Store a name.
# 2. Store a favorite technology tool.
# 3. Print a clear message for the user.
name = "Kofi"
tool = "Wireshark"
print("Hello", name)
print("You use", tool)
Wrap-Up¶
Variables store information your program can use later. print() communicates with the user, and errors are normal clues that help you improve your code.
In the next notebook, you will let users enter their own values and learn how Python handles that input.