Unit 1 • Lesson 3

Writing and Running Your First Program

Overview

Start coding your very first Python script. You'll learn the difference between running code in a file versus interactively in the Python shell and understand how Python executes instructions line by line.

Beginner 10–15 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • How to create your first Python program: Write the classic "Hello, World!" program and understand what each part does.
  • The difference between interactive mode and script mode: Learn when to use the Python shell versus saving code in a file.
  • How Python executes code: Understand how Python reads and runs your code line by line, from top to bottom.
  • How to run Python files: Execute saved Python programs using both VS Code and the terminal.

Building on Previous Lessons

This lesson assumes you have Python and VS Code installed. If you haven't completed Lesson 2 yet, go back and set up your development environment first.

Step 1: Understanding Interactive Mode vs Script Mode

Python can run code in two different ways. Understanding the difference helps you choose the right approach for different tasks.

Interactive Mode (Python Shell)

You type code directly into the terminal and see results immediately. Great for testing small pieces of code or experimenting. Code is not saved.

When to use: Quick tests, learning, debugging small code snippets.

Script Mode (Python Files)

You write code in a .py file, save it, and run it later. Code is saved permanently. This is how real programs are written.

When to use: Writing actual programs, saving your work, sharing code with others.

Key Difference: Interactive mode is like having a conversation with Python. Script mode is like writing a letter that Python reads later. For learning and building real programs, you'll mostly use script mode (saving files).

Step 2: Your First Program: Hello, World!

The "Hello, World!" program is a tradition in programming. It's the first program most people write when learning a new language. It's simple but teaches you the basics of how to write and run code.

Hello, World! Program
print("Hello, World!")

This single line of code does something important: it tells Python to display the text "Hello, World!" on the screen. Let's break it down:

1

print

This is a built-in Python function. Functions are like commands that tell Python to do something specific. print tells Python to display text.

2

The Parentheses ()

Functions in Python always use parentheses. The parentheses contain what you want the function to work with. In this case, we're telling print what text to display.

3

The Quotation Marks " "

Text in Python is called a "string". Strings must be surrounded by quotation marks (single ' or double " quotes). This tells Python "this is text, not code to execute".

4

"Hello, World!"

This is the actual text that will be displayed. You can change this to any text you want, and Python will display it.

Mini Practice #1: Try Hello, World!

Try It Yourself

Type the Hello, World! program in the editor below and click Run to see it execute:

Press Run to see output

What happened? When you pressed Run, Python read your code from top to bottom, executed the print() function, and displayed "Hello, World!" in the output area. This is exactly how Python runs programs: it reads each line and executes it in order.

Step 3: Creating a Python File in VS Code

Now let's create a real Python file that you can save and run anytime. This is how you'll write most of your programs.

1

Create a New File

In VS Code, click File, then New File (or press Ctrl+N / Cmd+N). A blank editor window will appear.

2

Save the File

Click File, then Save As. Name your file hello_world.py. The .py extension is crucial. It tells VS Code and your computer that this is a Python file.

3

Write Your Code

Type print("Hello, World!") in the file. Notice how VS Code colors the code. This is syntax highlighting, which makes code easier to read.

4

Run the File

Click the play button (▶) in the top-right corner of VS Code, or right-click in the editor and select "Run Python File in Terminal". You should see "Hello, World!" appear in the terminal panel at the bottom.

Why Save Files?

Saving your code in a file means you can:

  • Run the same program multiple times without retyping
  • Edit and improve your code later
  • Share your code with others
  • Build larger programs by combining multiple files

Step 4: How Python Executes Code

Understanding how Python runs your code helps you write better programs and debug problems. Python follows a simple but important process:

1

Python Reads Your File

When you run a Python file, Python opens it and reads the entire file from top to bottom.

2

Python Checks for Errors

Before running anything, Python checks if your code has syntax errors (like missing quotes or parentheses). If it finds errors, it stops and shows you an error message.

3

Python Executes Line by Line

If there are no errors, Python starts at the top of your file and executes each line in order, one at a time. It never skips ahead or goes backwards.

4

Python Shows Results

As Python executes each line, it shows the results. For example, when it executes print(), the text appears in the terminal immediately.

Important: Python always executes code from top to bottom, line by line. This means the order of your code matters. If you put print("Second") before print("First"), "Second" will appear first in the output.

Mini Practice #2: Multiple Print Statements

Try It Yourself

Try writing multiple print() statements. Notice how Python executes them in order:

Press Run to see output

What to notice: Each print() statement appears on its own line in the output. Python executed them in the exact order you wrote them: first, then second, then third. This demonstrates Python's top-to-bottom execution order.

Step 5: Running Python Files from the Terminal

You can also run Python files directly from the terminal or command prompt. This is useful when you want to run programs without opening VS Code.

On Windows:

  1. Open Command Prompt and navigate to the folder containing your Python file. For example, if your file is on the Desktop, type: cd Desktop
  2. Run the file: Type python hello_world.py and press Enter.

Make sure you're in the same folder as your Python file. Use cd to change directories.

On Mac:

  1. Open Terminal and navigate to the folder containing your Python file. For example, if your file is on the Desktop, type: cd Desktop
  2. Run the file: Type python3 hello_world.py and press Enter.

Remember to use python3 on Mac, not python.

Expected Output
Hello, World!

VS Code vs Terminal

Both methods do the same thing: they run your Python program. VS Code is more convenient for beginners because it shows you the output right in the editor. Running from the terminal is useful when you want to run programs quickly or when working on servers.

End-of-Lesson Exercises

Exercise 1: Customize Your Hello Message

Write a program that prints your own custom message. Use the print() function with text inside quotation marks.

Remember: the text must be inside quotation marks (single or double quotes work).

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Print Multiple Lines

Write a program that prints three different lines of text. Each line should use a separate print() statement.

Each print() statement will create a new line in the output. Try printing your name, your favorite color, and your favorite food.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 3: Create and Run a File

Create a new Python file in VS Code called my_first_program.py. Write a program that prints three things you're excited to learn about Python.

Save the file with the .py extension, then run it using the play button in VS Code. Make sure you can see the output in the terminal panel.