Learn Without Walls
← Lesson 2 Lesson 3 of 4 Next Lesson →

Lesson 3: Your First Python Program

⏱️ Estimated time: 25-30 minutes

📚 Learning Objectives

By the end of this lesson, you will be able to:

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!")
Hello, World!

That's it—one line of code! Let's break it down:

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, not hello 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
Hello, World!

✍️ Try It Yourself!

  1. Open your text editor
  2. Type print("Hello, World!")
  3. Save the file as hello.py on your Desktop or in a folder you can find easily
  4. 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!")
Hello! My name is Python. Nice to meet you!

Printing Numbers

Numbers don't need quotes:

print(42)
print(3.14)
print(2 + 3)
42 3.14 5

Printing Multiple Items

Separate items with commas—Python adds spaces automatically:

print("The answer is", 42)
print("Python", "is", "fun!")
The answer is 42 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)")
Line one 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')
Hello with double quotes 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")
This runs

Why Write Comments?

💡 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!)
SyntaxError: invalid syntax

Fix: Text must be wrapped in quotes: print("Hello, World!")

Error 2: Mismatched Quotes

print("Hello, World!')
SyntaxError: unterminated string literal

Fix: Use the same type of quote to open and close: print("Hello, World!")

Error 3: Missing Parentheses

print "Hello, World!"
SyntaxError: Missing parentheses in call to 'print'

Fix: In Python 3, print requires parentheses: print("Hello, World!")

Error 4: Wrong Capitalization

Print("Hello, World!")
NameError: name 'Print' is not defined

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("================================")
================================ About Me Program ================================ Name: Alex Age: 20 Major: Computer Science Favorite number: 7 Fun fact: I can count to 100 in one second! ================================

✍️ 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")
Answer: I have 7 apples
Python 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")
Answer: Python thinks the string ends at the second " (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?

Answer: Python files must have the .py extension (e.g., hello.py, my_program.py).

4. What does the # symbol do in Python?

Answer: The # 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

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.