No history yet

Python Setup

Getting Python on Your Machine

Before you can write any Python code, you need to install the Python interpreter on your computer. The interpreter is a program that understands and runs your Python code. It's the engine that makes everything work.

For Windows: Head to the official Python website, python.org, and download the latest version. During installation, you'll see a checkbox that says "Add Python to PATH." Make sure you check this box. It allows you to run Python from any command prompt window, which is incredibly useful.

Lesson image

For macOS: macOS often comes with an older version of Python already installed. It's best to install a modern version. You can download an installer from python.org, just like on Windows. Another popular method is using a package manager called Homebrew. Once Homebrew is installed, you can install Python by running brew install python3 in your terminal.

For Linux: Most Linux distributions come with Python pre-installed. You can usually install or update it using your system's package manager. For example, on Debian-based systems like Ubuntu, you would use a command like sudo apt-get install python3.

After installation, open your terminal or command prompt and type one of the following commands to check that everything worked correctly. You should see the Python version number printed back to you.

# On Windows, macOS, or Linux
python --version

# Or, on some macOS and Linux systems
python3 --version

Your Coding Workspace

You can write Python code in any plain text editor, but most programmers use an Integrated Development Environment, or IDE. Think of an IDE as a text editor with superpowers. It's designed specifically for writing code and includes helpful features like syntax highlighting (coloring your code to make it easier to read) and debugging tools.

For beginners, some great options are:

  • Thonny: Built specifically for learning Python. It's simple and has a built-in debugger that's easy to understand.
  • VS Code: A very popular, free code editor from Microsoft. It's lightweight but can be extended with thousands of plugins, including a powerful one for Python.
  • PyCharm Community Edition: A professional-grade IDE with a free version that has everything you need to get started.

Don't worry too much about which one to choose right now. Pick one that looks interesting and give it a try. You can always switch later.

Lesson image

Two Ways to Run Python

There are two primary ways to execute Python code: interactively in a prompt, or by running a script file. Both are useful in different situations.

The interactive prompt is great for testing small snippets of code and exploring ideas. Scripts are for building larger, reusable programs.

The Interactive Prompt

The interactive prompt, also called the Python shell or REPL (Read-Eval-Print Loop), lets you have a conversation with the Python interpreter. You type a line of code, hit Enter, and Python runs it and shows you the result immediately.

To start it, just open your terminal or command prompt and type python or python3. You'll see a >>> prompt, which means Python is waiting for your command.

>>> 5 + 7
12
>>> print("Hello, Python!")
Hello, Python!
>>> name = "Alice"
>>> name
'Alice'

To exit the interactive prompt, you can type exit() and press Enter, or use the keyboard shortcut Ctrl+Z on Windows or Ctrl+D on macOS/Linux.

Writing and Running Scripts

For anything more than a few lines, you'll want to save your code in a file. A Python file is just a plain text file that ends with the .py extension. This is called a script.

Let's create a simple script.

  1. Open your IDE or any plain text editor.
  2. Type the following code: print("This is my first Python script!") print("It has more than one line.")
  3. Save the file as my_script.py. Be sure to save it somewhere you can easily find, like your Desktop or a dedicated projects folder.

Now, to run it, go back to your terminal. First, navigate to the directory where you saved your file. For example, if it's on your Desktop, you might type cd Desktop. Then, run the script by typing python (or python3) followed by the filename.

# Make sure you are in the same directory as your file!
python my_script.py

When you press Enter, you should see the output from your script printed directly to the terminal.

This is my first Python script! It has more than one line.

And that's it! You've successfully set up your environment and run Python code in two different ways. You're now ready to start building more complex and interesting programs.

Quiz Questions 1/6

When installing Python on Windows from the official website, which action is highly recommended to ensure you can run Python easily from the command prompt?

Quiz Questions 2/6

What does REPL stand for in the context of the interactive Python prompt?