25:00
Focus
Lesson 1

Your First Python Script Setup

~5 min50 XP

Introduction

Welcome to your journey into software development! In this lesson, we will bridge the gap between your curiosity and your first functional line of code by setting up a professional Python environment and executing your first script.

Preparing Your Environment

To run Python, your computer needs the Python interpreter, which is the engine that executes your code. While Python is pre-installed on many systems, it is best practice to install the latest version directly from the official source to ensure you have the full feature set. Once installed, you will use a Command Line Interface (CLI)β€”such as Terminal on macOS/Linux or Command Prompt/PowerShell on Windowsβ€”to talk to the interpreter.

The most common pitfall for beginners is failing to add Python to the system's PATH. The PATH is a list of directories that your computer searches to find executable files. If Python isn't in your PATH, your computer won't know what you mean when you type python into your terminal. During installation, always make sure to check the box labeled "Add Python to PATH."

Exercise 1Multiple Choice
Why is adding Python to your PATH during installation important?

Creating Your First Script

A Python script is simply a plain text file saved with a .py extension. While you can write code in any basic text editor like Notepad or TextEdit, professionals use a Code Editor like VS Code. These tools provide Syntax Highlighting, which color-codes your code to help you spot errors before you even run the script.

To create your script, open your editor and type the following command:

print("Hello, World!")

The print() function is a Built-in Function that sends data to your console output. Think of it as your primary way of seeing what is happening inside your code.

Note: Never use a word processor like Microsoft Word to write code. Word processors add invisible formatting characters (hidden metadata) that will cause your Python script to crash immediately. Always use a dedicated code editor or a plain-text editor.

Executing Your Code

Once your file is saved as hello.py, it is time to run it. Navigate your terminal to the folder where you saved the file. Use the cd (change directory) command to get there. For example, if your file is on the Desktop, you would type cd Desktop.

Once you are in the correct folder, type python hello.py into your terminal. The interpreter reads the text in your file line by line and executes the commands. If everything is configured correctly, your screen will display:

Hello, World!

Exercise 2True or False
True or False: Using a standard word processor like Microsoft Word to write code is recommended because it helps with spell-checking.

Understanding the Interpreter

Python is an Interpreted Language, meaning it executes code line-by-line rather than translating the entire file into machine code at once. This makes development faster but adds a small amount of overhead during execution. When you run python hello.py, you are invoking the interpreter to parse your source code, convert it into Bytecode, and run it on something called the Python Virtual Machine.

You don't need to worry about the underlying Bytecode yet, but understanding that Python "reads" your file like a recipe is essential. If you have an error on line 5, the program will run lines 1 through 4 perfectly and then stop, providing an Exception (error message).

Exercise 3Fill in the Blank
The ___ function is used to output text to the console in Python.

Key Takeaways

  • The PATH variable allows your operating system to find the Python interpreter regardless of your current working directory.
  • Always save your files with a .py extension and use a plain-text code editor to avoid hidden formatting issues.
  • The print() command is the most fundamental way to communicate with the user via the console.
  • Python is an Interpreted Language, which means errors often appear only when the code reaches the specific line where the problem resides.
Finding tutorial videos...
Go deeper
  • How can I verify if Python is in my PATH already?πŸ”’
  • What should I do if my terminal says command not found?πŸ”’
  • Is there a difference between python and python3 commands?πŸ”’
  • How do I update my existing PATH manually if I forgot?πŸ”’
  • Which code editor do you recommend for writing my first script?πŸ”’