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:
python --versionIf 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
pythonfrom any folder. - macOS: the python.org installer works well. If you use Homebrew,
brew install pythonalso works. - Linux: most distributions ship Python 3 already. On Ubuntu or Debian,
sudo apt install python3 python3-pipinstalls 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.
>>> 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:
name = "Priya"
print("Hello,", name)Then run it from the terminal:
python hello.pyHello, PriyaPython 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.
pip install requestsWe cover pip and virtual environments properly in the pip and virtual environments lesson. For now just know it exists.
Practice
- Install Python and confirm
python --versionprints a 3.x version. - Open the interactive shell and calculate how many seconds there are in a week.
- Save the
hello.pyexample with your own name and run it from the terminal.
