Welcome to your first step into the world of programming with Python! By the end of this lesson, you will understand how to set up your development environment and execute a basic program that communicates directly with your computer.
To understand what happens when you run a program, you must first understand the interpreter. Python is an interpreted language, meaning the Python executable reads your human-readable code and translates it line-by-line into machine instructions that the computer's CPU can understand. Unlike compiled languages, which transform code into an entirely separate executable file before running, Python processes your source code files as they are executed.
Think of the Python interpreter as a translator living inside your machine. When you give it a file ending in .py, it reads the text and performs the actions you have defined. To start, you need to ensure Python is installed. You can verify this by opening your terminal or command prompt and typing python --version (or python3 --version on some systems). If you see a version number, you are ready to begin writing your first script.
A Python script is simply a plain text file saved with a .py extension. While you can write code in any text editor, using an Integrated Development Environment (IDE) or a code-focused editor like VS Code is highly recommended. These tools provide features like syntax highlighting, which color-codes your code to make it easier to read and spot errors.
Let's create a file named hello.py. In this file, you will write a single instruction using the print() function. This is a built-in block of code that sends information to your screen. The syntax for this is simple: place the text you want to display inside quotation marks within the parentheses. If you forget the parentheses or the quotes, the interpreter will encounter a syntax error, which is a formal way of saying the code violates the grammatical rules of the Python language.
Now that your file exists, it is time to move from the editor back to your terminal. To execute your script, you need to navigate into the folder where you saved hello.py. You do this using the cd (Change Directory) command in your terminal. Once you are in the correct folder, you tell the Python interpreter to run the file by typing python hello.py.
When you trigger this command, the operating system hands your file to the Python executable. The interpreter scans the file, finds the print() function, evaluates the string "Hello, World!", and pushes that text to your standard output (your console screen). If you see the message printed back to you, you have successfully completed the core feedback loop of professional software development: write, save, run, repeat.
Always ensure your file is saved before you run the command in the terminal. If you don't save periodically, the interpreter will process the version of the file that exists on your hard drive, not the one you see in your editor.
Even seasoned developers make mistakes when starting out. One common issue is indentation errors or case sensitivity. Python is case-sensitive, meaning Print("Hello") will fail because the interpreter looks for a function named print (lowercase), not Print. Another frequent issue is file naming. Avoid naming your files the same as standard Python libraries (like math.py or random.py), as this confuses the interpreter and leads to import errors.
If you receive an error message, do not be discouraged! Python provides tracebacks, which are detailed logs explaining exactly which file and which line caused the problem. Read the traceback from bottom to top; the bottom line usually explains the specific problem, while the lines above it show the path the interpreter took to get there.
.py extension.print() function is the primary method for displaying output to the console.