Lesson 3: Your First Python Program
⏱️ Estimated time: 25-30 minutes
📚 Learning Objectives
By the end of this lesson, you will be able to:
- Write and run a "Hello, World!" program in Python
- Create and save a Python file with the .py extension
- Run Python scripts from the command line
- Use the
print()function to display output - Add comments to your code
- Recognize and fix common beginner errors
The Tradition of "Hello, World!"
In the programming world, writing a "Hello, World!" program is a tradition that dates back to the 1970s. It's the first program almost every programmer writes in a new language. It may seem simple, but it proves that your environment is set up correctly and that you can write, save, and run code.
print() is a built-in Python function that displays text (or other values) on the screen. It's the most basic way to see output from your program.
Here's your first Python program:
print("Hello, World!")
That's it—one line of code! Let's break it down:
print— tells Python you want to display something on screen(and)— parentheses wrap what you want to print"Hello, World!"— the text you want to display, enclosed in quotes
Creating and Running a Python File
There are two ways to run Python code: in a file (a "script") or interactively (which we'll cover in Lesson 4). Let's learn the file-based approach first.
Step 1: Create a Python File
Open your text editor (IDLE, VS Code, or any editor) and create a new file. Type the following:
# My first Python program print("Hello, World!")
Step 2: Save the File
Save the file as hello.py. The .py extension tells your computer this is a Python file.
⚠️ Important Naming Rules
- Python files must end in
.py - Use lowercase letters and underscores (e.g.,
my_program.py) - Don't use spaces in filenames (use
hello_world.py, nothello world.py) - Don't name your file
python.py—this will confuse Python!
Step 3: Run the File
Using IDLE: With the file open in IDLE, press F5 or go to Run > Run Module.
Using the command line: Open your terminal, navigate to the folder where you saved the file, and type:
python hello.py
✍️ Try It Yourself!
- Open your text editor
- Type
print("Hello, World!") - Save the file as
hello.pyon your Desktop or in a folder you can find easily - Run the file using IDLE (F5) or the command line
If you see "Hello, World!" in the output—congratulations! You just wrote and ran your first Python program!
Exploring the print() Function
The print() function is more versatile than just printing one message. Let's explore what it can do.
Printing Multiple Lines
Each print() statement creates a new line of output:
print("Hello!") print("My name is Python.") print("Nice to meet you!")
Printing Numbers
Numbers don't need quotes:
print(42) print(3.14) print(2 + 3)
Printing Multiple Items
Separate items with commas—Python adds spaces automatically:
print("The answer is", 42) print("Python", "is", "fun!")
Empty print()
A print() with nothing inside creates a blank line:
print("Line one") print() print("Line three (with a blank line above)")
Single vs. Double Quotes
Python accepts both single quotes (') and double quotes (") for text. They work the same way:
print("Hello with double quotes") print('Hello with single quotes')
💡 When to Use Which?
Use double quotes when your text contains an apostrophe:
print("It's a beautiful day!") # Works print('It\'s a beautiful day!') # Also works (with escape)
Use single quotes when your text contains double quotes:
print('She said "hello" to me.')
Comments: Notes to Yourself
Comments are notes in your code that Python ignores completely. They're written for humans—to explain what your code does, why you made certain decisions, or to temporarily disable code.
Single-Line Comments
Start a comment with the # symbol. Everything after # on that line is ignored:
# This is a comment - Python ignores this line print("This runs") # This comment is after code # print("This won't run because it's commented out")
Why Write Comments?
- Explain your thinking: Why did you write the code this way?
- Help future you: You might forget what your code does weeks later
- Help others: If someone else reads your code, comments guide them
- Debugging: Temporarily "turn off" code by commenting it out
💡 Good vs. Bad Comments
# Bad comment - just restates what the code does print("Hello") # prints hello # Good comment - explains WHY # Greet the user when the program starts print("Hello! Welcome to the Grade Calculator.")
Common Beginner Errors (and How to Fix Them)
Making errors is completely normal! Here are the most common mistakes beginners make and how to fix them.
Error 1: Missing Quotes
print(Hello, World!)
Fix: Text must be wrapped in quotes: print("Hello, World!")
Error 2: Mismatched Quotes
print("Hello, World!')
Fix: Use the same type of quote to open and close: print("Hello, World!")
Error 3: Missing Parentheses
print "Hello, World!"
Fix: In Python 3, print requires parentheses: print("Hello, World!")
Error 4: Wrong Capitalization
Print("Hello, World!")
Fix: Python is case-sensitive. Use lowercase print, not Print or PRINT.
Don't Fear Errors!
Error messages are helpful, not punishments. They tell you exactly what went wrong and where. Learning to read error messages is one of the most valuable programming skills you'll develop.
Putting It All Together
Let's create a more interesting program that uses everything we've learned:
# My About Me program # This program displays information about me print("================================") print(" About Me Program") print("================================") print() print("Name: Alex") print("Age:", 20) print("Major: Computer Science") print("Favorite number:", 7) print() print("Fun fact: I can count to", 10 * 10, "in one second!") print("================================")
✍️ Try It Yourself!
Create your own "About Me" program. Save it as about_me.py and include:
- At least 5
print()statements - At least one comment
- A mix of text and numbers
- At least one math calculation inside
print()
✅ Check Your Understanding
1. What will this code display?
print("I have", 3 + 4, "apples")
I have 7 applesPython calculates 3 + 4 = 7 and puts spaces between the comma-separated items automatically.
2. Why does this code produce an error?
print("She said "hello" to me")
" (after "She said "), then doesn't understand the rest. Fix it by using single quotes on the outside: print('She said "hello" to me')
3. What extension must Python files have?
.py extension (e.g., hello.py, my_program.py).
4. What does the # symbol do in Python?
# symbol starts a comment. Everything after # on that line is ignored by Python. Comments are used to add notes and explanations to your code.
🎯 Key Takeaways
print()displays text and values on the screen- Text (strings) must be wrapped in quotes—either single or double
- Python files are saved with a
.pyextension - Run scripts from the command line with
python filename.py - Comments start with
#and are ignored by Python - Python is case-sensitive—
printworks butPrintdoes not - Error messages are your friends—they tell you exactly what went wrong
Ready for More?
➡️ Next Lesson
In Lesson 4, you'll learn to use the Python interpreter (REPL) to test code interactively and explore Python as a calculator!
Start Lesson 4📊 Module Progress
You've completed Lesson 3! You can now write and run Python programs.