25:00
Focus
Sign in to save your learning paths. Guest paths may be lost if you clear your browser data.Sign in
Lesson 1

Running Your First Python Script

~5 min50 XP

Introduction

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.

The Role of the Python Interpreter

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.

Exercise 1Multiple Choice
What does it mean for Python to be an interpreted language?

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 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.

Executing the Code

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.

Exercise 2Fill in the Blank
To output text to the console in Python, you use the ___ function.

Common Pitfalls for Beginners

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.

Exercise 3True or False
Python is case-sensitive, so 'print' and 'Print' are treated as the same command.

Key Takeaways

  • The Python interpreter reads and executes your code line-by-line, providing immediate feedback.
  • A Python script is a simple text file saved with a .py extension.
  • The print() function is the primary method for displaying output to the console.
  • Tracebacks are your primary tool for debugging; use them to identify the line and reason for your errors.
Finding tutorial videos...
Go deeper
  • What is the main advantage of an interpreted language?🔒
  • How do compiled languages differ from Python?🔒
  • What happens if my terminal doesn't recognize the python command?🔒
  • Can I run Python code without a terminal?🔒
  • Is there a difference between python and python3?🔒