Python Interactive Shell
Why this matters: Most languages make you write a file, save it and run it before you can see what your code does. Python has a faster mode: a live shell where you type one line and it answers immediately. This is the single best way to learn Python. Open it any time you wonder "what does this do?" or "what happens if...".
What is the REPL?
The Python shell is called a REPL, short for Read-Eval-Print Loop:
- Read what you type
- Evaluate it
- Print the result
- Loop back and wait for more
Open a terminal and type:
python
(or python3 on Mac and Linux). You will see something like:
Python 3.11.5
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> is the Python prompt. It is waiting for you.
Your first commands
Try these one at a time, pressing Enter after each:
>>> print("Hello, World!")
Hello, World!
>>> 5 + 3
8
>>> 10 * 4
40
>>> name = "Lerato"
>>> print(name)
Lerato
>>> "HardCoded".lower()
'hardcoded'
Notice that for expressions like 5 + 3, the shell prints the result automatically. For statements like assignments (name = "Lerato"), nothing is printed.
Python as a calculator
This is genuinely useful, even after you become an experienced developer. Open the Python shell whenever you need to do quick maths:
>>> 100 + 50
150
>>> 25 * 4
100
>>> 100 / 3
33.333333333333336
>>> 100 // 3 # Integer division (drops the decimal)
33
>>> 2 ** 8 # 2 to the power of 8
256
>>> 17 % 5 # Modulus (remainder after division)
2
The last three operators are worth knowing early:
//is floor division (integer division)**is exponentiation (a to the power of b)%is the modulus (remainder)
These come up surprisingly often.
A practical SA example
Open the shell and work through this VAT calculation step by step:
>>> price = 499
>>> vat = price * 0.15
>>> vat
74.85
>>> total = price + vat
>>> total
573.85
You have just written a working VAT calculator without creating a single function or file.
Exiting the shell
When you are done:
>>> exit()
(or press Ctrl+Z then Enter on Windows, Ctrl+D on Mac and Linux)
When to use the shell vs a file
The shell and the file are both first-class ways to write Python. They are for different jobs.
| Use the shell for... | Use a file for... |
|---|---|
| Quick experiments | Writing a real program |
| Trying out a library you have never used | Saving your work |
| Maths on the fly | Anything you want to run more than once |
| Checking syntax | Anything you want to share |
You will use both, often in the same session: prototype in the shell, then move the working code into a file when you are happy with it.
Try it yourself
The interactive code editor below this text lets you write Python right here in the browser. You do not need to leave the page. Try the following:
- Print your own name
- Calculate your age in months (your age times 12)
- Calculate how many seconds are in a day (24 times 60 times 60)
- Save the result of one calculation into a variable, then use that variable in another calculation
What's next
In the next lesson we move on to variables and data types, the building blocks for any real program. You will see how Python decides what type your data is and how to convert between types.