Course topics

By WebNest Studio Engineering Team

Python Tutorial

Setting Up the Python Environment

Before writing any Python code, you need Python itself installed, a way to confirm it is working correctly, and an editor or IDE that makes writing and running scripts comfortable. Unlike compiled languages, Python code can also be run interactively, line by line, which is one of the fastest ways to experiment with the language while learning it.

This lesson covers installing Python from python.org or a version manager, verifying the install from the terminal, choosing an editor, running a script as a file, and using the interactive REPL (Read-Eval-Print Loop) for quick experimentation.

Why it matters

A surprising number of early problems are not about code at all: the wrong Python version runs, a package installs into a different interpreter, or the command is not found. Knowing how to check which interpreter you are using and how to run a file removes these problems before they waste your time.

Installing Python

The most direct route is downloading an installer from python.org for your operating system; on Windows, be sure to check "Add python.exe to PATH" during installation, since forgetting this is the single most common setup mistake. On macOS and Linux, Python 3 is often preinstalled or available via the system package manager, but many developers instead use a version manager such as pyenv, which lets you install and switch between multiple Python versions cleanly — important because different projects may require different Python versions.

  • python.org installer — simplest for a single, system-wide Python install.
  • pyenv (macOS/Linux) or pyenv-win (Windows) — lets you install and switch between several Python versions per project.
  • Package managers (Homebrew on macOS, apt on Ubuntu) — convenient but can lag behind the latest Python release.

Verifying the Installation

After installing, open a terminal and run python --version (or python3 --version on systems where "python" still points to Python 2 or nothing at all). This should print something like Python 3.12.4. If the command is not found, the installation did not add Python to your system PATH, and you will need to either reinstall with that option checked or add the install directory manually.

Choosing an Editor or IDE

Visual Studio Code with the official Python extension is a popular lightweight choice: it adds syntax highlighting, linting, debugging, and an integrated terminal, and it works well whether you are writing a ten-line script or a large project. PyCharm (from JetBrains) is a full-featured Python-specific IDE with deeper refactoring tools, a built-in debugger, and strong support for frameworks like Django, at the cost of being heavier to run. Either is a reasonable starting point; the important thing is having integrated run/debug support so you are not constantly switching between an editor window and a separate terminal.

Running a Script vs. the Interactive REPL

A Python script is a plain text file ending in .py, executed from the terminal with python filename.py; this is how real programs are run. The REPL, started by typing just python with no filename, gives you an interactive prompt (>>>) where you type one expression or statement at a time and see its result immediately — ideal for testing a small piece of syntax, inspecting an object, or checking how a function behaves, without creating a file at all. Many editors also offer an integrated interactive console that behaves the same way.

Example

Verifying the install, running a script file, and using the REPL interactively

Python
# Terminal: verify installation
$ python --version

# hello.py
print("Hello from a Python script")

# Terminal: run the script file
$ python hello.py

# Terminal: interactive REPL session
$ python
>>> 2 + 2
>>> name = "Webnest"
>>> print(f"Hello, {name}!")
>>> exit()
Output
Python 3.12.4
Hello from a Python script
>>> 4
>>> Hello, Webnest!

Common Mistakes

  • Forgetting to check "Add Python to PATH" on Windows during installation, leading to "python is not recognized" errors in the terminal.
  • Running "python" on a system where it still resolves to an old Python 2 install and being confused by print statement syntax errors — use "python3" explicitly when in doubt.
  • Treating REPL experimentation as equivalent to running a real script — REPL state is lost when you close the session, and it is not a substitute for saving code to a file.
  • Installing multiple Python versions without a version manager and losing track of which "python" command points to which install.

Exercise

Write a script that prints the major version number of the Python that is running it, and then whether that Python is version 3.8 or newer.

Expected output: 3 then True

Python · starter code
import sys

# TODO: print the major version number
# TODO: print whether the version is at least 3.8
Show hints
  • sys.version_info holds the version as numbers, and sys.version_info.major is the first of them.
  • It can be compared with a tuple: sys.version_info >= (3, 8).
Show solution
Python · solution
import sys

print(sys.version_info.major)
print(sys.version_info >= (3, 8))

Quick quiz

1. Which command shows the installed Python version?

  • A. python --version
  • B. python version()
  • C. python -run
  • D. pip version python
Show answer

A. On macOS and Linux the command is often python3 --version.

2. What is the REPL?

  • A. A package installer
  • B. An interactive prompt that runs each line as you type it
  • C. A code formatter
  • D. A type of file
Show answer

B. It reads a line, evaluates it, prints the result and waits for the next one.

3. How do you run a saved file named app.py?

  • A. run app.py
  • B. app.py --python
  • C. python app.py
  • D. import app.py
Show answer

C. The interpreter is given the file name as its argument.

Interview questions

What is a virtual environment and why use one?

Show answer

A virtual environment is a folder that holds its own Python interpreter and its own installed packages, separate from the system-wide installation. Each project gets its own, so two projects can depend on different versions of the same package without conflict, and the list of packages a project needs can be recorded and recreated on another machine. It is created with python -m venv .venv.

What is the difference between running a script and using the REPL?

Show answer

A script is a saved .py file that the interpreter runs from top to bottom; it is how real programs are written and kept. The REPL runs one statement at a time and shows the result of each expression immediately, which is useful for trying something out, but nothing typed there is saved.

Key Points to Remember

  • Install Python from python.org or a version manager like pyenv, and always confirm with "python --version".
  • VS Code and PyCharm are the two most common editor/IDE choices for Python development.
  • A .py file is run from the terminal with "python filename.py" for real, saved programs.
  • The REPL (started by running "python" with no file) is for quick, throwaway interactive experimentation, not for building applications.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering