indusai.co

Installing and running Python

You can run every example on this site without installing anything, but you will want Python on your own computer for real projects. This lesson gets you there in a few minutes.

Check if Python is already installed

Open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux) and type:

terminal
python --version

If you see something like Python 3.12.3, you are done. On macOS and Linux the command may be python3 instead of python.

Installing Python

  • Windows: download the installer from python.org/downloads. On the first screen tick Add python.exe to PATH before clicking Install. This lets you run python from any folder.
  • macOS: the python.org installer works well. If you use Homebrew, brew install python also works.
  • Linux: most distributions ship Python 3 already. On Ubuntu or Debian, sudo apt install python3 python3-pip installs it along with pip.

Three ways to run Python

1. The interactive shell

Type python (or python3) with no file name and you get a prompt that looks like >>>. Type an expression, press Enter, and Python shows the result immediately.

output
>>> 2 + 2
4
>>> "Indus".upper()
'INDUS'
>>> exit()

The shell is perfect for quick experiments. Type exit() or press Ctrl+D (Ctrl+Z then Enter on Windows) to leave.

2. Running a file

Create a file called hello.py with this content:

python
name = "Priya"
print("Hello,", name)

Then run it from the terminal:

terminal
python hello.py
output
Hello, Priya

Python files always end in .py. The interpreter reads the file from top to bottom and executes each statement in order.

3. An editor or IDE

Any text editor works, but a good one helps a lot. Visual Studio Code with the official Python extension is free and the most common choice. PyCharm Community is another excellent free option. Jupyter notebooks are popular for data work because they mix code, output and notes in one document.

The pip package manager

pip installs packages written by other people. It comes with Python.

terminal
pip install requests

We cover pip and virtual environments properly in the pip and virtual environments lesson. For now just know it exists.

Practice

  1. Install Python and confirm python --version prints a 3.x version.
  2. Open the interactive shell and calculate how many seconds there are in a week.
  3. Save the hello.py example with your own name and run it from the terminal.