No history yet

Python Environment Setup

Getting Python Running

Before you can write Python code, you need to install the Python interpreter on your computer. The interpreter is the program that reads your Python code and carries out its instructions. We'll install it directly from the official source to ensure you have the latest stable version.

For Windows:

  1. Go to the official Python website's download page for Windows.
  2. Download the latest installer (it's usually a file ending in .exe).
  3. Run the installer. Important: On the first screen of the setup, make sure you check the box that says "Add Python to PATH." This allows you to run Python from your command prompt from any directory.
  4. Click "Install Now" and follow the prompts to complete the installation.
Lesson image

For macOS:

  1. Visit the Python website's download page for macOS.
  2. Download the installer package (a .pkg file).
  3. Open the downloaded file. Follow the on-screen instructions, agreeing to the license and confirming the installation location. macOS typically handles the PATH configuration for you.

For Linux: Most Linux distributions come with Python pre-installed. To check, open your terminal and type python3 --version. If it returns a version number (like Python 3.10.4), you're all set. If not, you can install it using your distribution's package manager. For Debian/Ubuntu, the command is: sudo apt-get update sudo apt-get install python3

To verify your installation on any operating system, open your terminal or command prompt and type python3 --version (or just python --version on Windows). If you see a version number, the installation was successful.

Your First Conversation with Python

With Python installed, you can now interact with its interpreter directly. This interactive mode is a powerful tool for testing small snippets of code without having to save them in a file. It's often called a .

To start a REPL session, just open your terminal and type python (on Windows) or python3 (on macOS/Linux) and press Enter. You'll see a prompt, usually >>>, indicating Python is ready for your commands.

# Launch the Python REPL
# On Windows:
C:\Users\YourUser> python
# On macOS/Linux:
$ python3

# Once inside, you can execute code
>>> 2 + 2
4
>>> print("This is a direct command!")
This is a direct command!
>>> exit()

The REPL is excellent for quick experiments, but for anything more complex, you’ll want to save your code in a file.

Writing and Running a Script

A Python program is simply a text file containing Python code. By convention, these files have a .py extension. You can create one using any plain text editor, from Notepad to more advanced code editors.

Let’s write the classic "Hello, World!" program. Create a new file named hello.py and add the following line of code.

# This is a comment. The interpreter ignores it.
# The line below prints a message to the console.

print("Hello, World!")

Comments, which start with a # symbol, are notes for human readers. The Python interpreter completely ignores them. They're crucial for explaining what your code does, both for others and for your future self.

To run your script, navigate to the directory where you saved hello.py in your terminal. Then, execute it by passing the filename to the Python interpreter.

# In your terminal, in the same folder as hello.py
# On Windows:
python hello.py

# On macOS/Linux:
python3 hello.py

# The output will be:
Hello, World!

You've just written and executed your first Python program. This process of writing code in a and running it through the interpreter is the fundamental workflow for all Python development.

Now that you have your environment set up and know how to run a basic script, let's check your understanding.

Quiz Questions 1/5

When installing Python on Windows, which crucial step ensures you can run the python command from any directory in your command prompt?

Quiz Questions 2/5

What is the common name for the interactive mode that allows you to test small snippets of Python code directly in your terminal, often indicated by a >>> prompt?

With a working setup, you're ready to start exploring the core components of the Python language.