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.
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."
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.
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!
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).
.py extension and use a plain-text code editor to avoid hidden formatting issues.print() command is the most fundamental way to communicate with the user via the console.