Python on your own computer

Part of the free LogicWiz Python course, Module 1: Start coding. The examples and practice run in the app.

Aarav's lab exam is on a lab computer, not on this website. He opens a black window with a blinking cursor and freezes. What do you type there? By the end of this lesson, you will know.

You'll learn to

  • Install Python and check it with python --version
  • Use the >>> prompt, and know how it differs from a saved script
  • Save a .py file, run it with python bill.py, and pick an editor
  • Fix the usual first-day problems

Learn

Install Python, then check it

  1. Go to python.org, open Downloads and get the latest Python 3.
  2. On Windows, on the first screen of the installer, tick Add python.exe to PATH, then click Install Now. (PATH is the list of folders your computer searches when you type a command. Without it, the computer cannot find Python.)
  3. Open a terminal: a window where you type commands instead of clicking. On Windows, search for cmd. On a Mac, open Terminal.
  4. Type this and press Enter:
python --version

If you see something like Python 3.12.4, it works. Any version starting with 3 is fine. On a Mac or Linux, type python3 instead of python.

The >>> prompt: interactive mode

2 + 3
"chai" * 2
price = 15
price * 4
print("Hi")

Type python in the terminal and press Enter. (C:\Users\Aarav> is the terminal's own prompt; you type only what comes after it.) Python shows its version, then >>>, which means I am waiting for one line. Each line runs the moment you press Enter, and if it has a value, Python shows it straight away. price = 15 shows nothing, because storing a name is not a value. Text is shown with its quotes. This is interactive mode: perfect for quick checks, but nothing is saved. Type exit() to leave.

The same lines as a script

2 + 3
"chai" * 2
price = 15
price * 4
print("Hi")
print(price * 4)

Run it. Only Hi and 60 appear. A script is a saved file of code that runs top to bottom. Python still works out 2 + 3 and price * 4, but then throws the answers away. Nothing reaches the screen unless you print() it. Every code box in this course is script mode.

Save a script and run it

# bill.py
item = input("Item: ")
qty = int(input("How many? "))
print(item, "x", qty, "= Rs", qty * 15)
  1. Make a folder, say python on your Desktop.
  2. In your editor, type the code and save it as bill.py in that folder. The .py ending marks it as Python.
  3. In the terminal, move into that folder with cd (change directory). On a Mac it is cd Desktop/python.
  4. Type python bill.py and press Enter.

On your laptop you type the answers yourself: Samosa, Enter, 3, Enter.

Pick an editor

An editor is where you type and save your code. Pick one:

  • IDLE comes free with Python. Choose File → New File, type, save as .py, and press F5 to run. Output appears in the IDLE Shell window, which is a >>> prompt too.
  • VS Code is free and used at work. Install its Python extension, open your folder, and click the Run triangle at the top right. Output appears in the Terminal panel at the bottom.
  • On a phone, keep using this course, or try an online Python editor in the browser, or a Python app such as Pydroid 3 on Android.

Start with IDLE. Move to VS Code when you feel at home.

Where errors appear

print("Bill for Meher")
total = 15 * qty
print("Total:", total)

Bill for Meher prints, then the error stops the program. On your laptop it is the same: the error message appears right below any output, in the terminal (in red in IDLE, in the Terminal panel in VS Code). Its line File "bill.py", line 2 names your file and the line to fix. Read it from the bottom up, as in Your first program.

These boxes are real Python too

import sys
print(f"Python {sys.version_info.major}.{sys.version_info.minor}")
print(sys.platform)

The boxes in this course run real Python 3.12 inside your browser, using a project called Pyodide. That is why sys.platform says emscripten here. On your laptop it says win32, darwin (a Mac) or linux, and the version may be newer. The language is the same: what you write here runs there unchanged.

First-day problems and their fixes

  • 'python' is not recognized…: Python is not on PATH. Install again and tick Add python.exe to PATH, then open a new terminal. On Windows, py bill.py also works.
  • can't open file … No such file or directory: the terminal is in another folder. cd into the folder where you saved the file. dir (Windows) or ls (Mac) lists what is there.
  • The file is bill.py.txt: Notepad added .txt. Save again with Save as type: All files, or use IDLE.
  • Nothing happens, the cursor blinks: the program is waiting at input(). Type an answer and press Enter.
  • SyntaxError on python bill.py: you typed it at >>>. Type exit() first.

Exams love interactive mode vs script mode. Interactive mode is the >>> prompt: one statement at a time, the value of each expression is shown at once, and nothing is saved. Script mode runs a saved .py file from top to bottom, and only print() shows output. A favourite question: what does 2 + 3 show in each mode? (5 at the prompt; nothing at all in a script.)

In one line

Install Python from python.org (tick Add to PATH) and check with python --version; the >>> prompt shows each value at once, while a .py script run with python bill.py shows only what you print().