Why a prompt before more scripts?
In Level 1 you wrote a script, saved it and ran it. That works well for finished work, but it is slow when all you want to know is "what does this line do?" For that you use the interactive prompt, also called the REPL (Read, Evaluate, Print, Loop). You type one line, Python runs it and shows you the result straight away.
If you have used spreadsheets, it works like typing a formula into a cell and seeing the answer at once. Experienced developers keep a REPL open all day for exactly this. It is the quickest way to test an idea before it goes into a script.
Starting and leaving the prompt
Open a terminal (the VS Code terminal is fine) and type python on its own. On Windows py also works if that is what you used in Level 1.
PS C:\work> python
Python 3.13.x ... on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> means Python is waiting for you. Try a few things:
>>> 2 + 3 # an expression: Python works it out...
5 # ...and prints the result for you
>>> "invoice" # a piece of text is a value too
'invoice'
In a script you need print() to see anything. At the prompt, Python shows the value of each expression for you. This catches a lot of beginners out: a script that contains only 2 + 3 prints nothing.
What changed in Python 3.13. Python 3.13 brought in a new default prompt:
- Colour: the prompts and error messages (tracebacks) are in colour by default.
- Multi-line editing: press the Up arrow and you get back a whole multi-line block, not one line at a time, and you can edit it before running it again.
exitandquitwork on their own. Before 3.13 you had to typeexit()with brackets.helpworks the same way.- F1 opens help, F2 shows your history without the output, and F3 turns on paste mode, which makes pasting a large block of code easier.
Python 3.14 went further and colours your code as you type it (syntax highlighting). If your terminal shows the colours badly, you can set the PYTHON_BASIC_REPL environment variable to go back to the old prompt.
To leave the prompt:
>>> exit # 3.13+: no brackets needed
PS C:\work>
You can also press Ctrl+Z then Enter on Windows, or Ctrl+D on macOS and Linux, at an empty prompt.
Gotcha: if you type
pythonat the>>>prompt you get an error, because you are already inside Python. Check which prompt you are looking at:PS C:\...>or$is your terminal, and>>>is Python.
Every value has a type
Every value in Python has a type, which tells Python what kind of thing it is and what you can do with it. The built-in type() function tells you the type of any value. Here are the five types you will meet first:
>>> type(42) # a whole number
<class 'int'>
>>> type(19.99) # a number with a decimal point
<class 'float'>
>>> type("Acme Ltd") # text in quotes: a string
<class 'str'>
>>> type(True) # True or False: a boolean
<class 'bool'>
>>> type(None) # "no value at all"
<class 'NoneType'>
In plain terms:
| Type | What it is | Work example |
|---|---|---|
int |
whole number | number of rows, quantity |
float |
decimal number | a price, a percentage |
str |
text | a customer name, a file name |
bool |
True / False |
"is this invoice paid?" |
None |
nothing yet / not known | a manager not yet assigned |
The catch is that the type depends on how the value is written, not on what it looks like to you:
>>> type(42)
<class 'int'>
>>> type("42") # quotes make it text, even though it looks like a number
<class 'str'>
>>> type(42.0) # a decimal point makes it a float
<class 'float'>
>>> type(true) # lower-case: Python doesn't recognise it
NameError: name 'true' is not defined. Did you mean: 'True'?
Why this matters at work: numbers read from files often come in as
str, like"42". When a total comes out wrong,type()is the first thing to check. Note also thatTrue,FalseandNonestart with a capital letter.
Variables: names for values
A variable is a name that points to a value. You create one with =, which means "store this under that name". It does not mean "equals" as in maths.
>>> customer = "Acme Ltd" # create a name and point it at a value
>>> invoice_count = 12
>>> is_paid = False
>>> customer # typing a name shows its value
'Acme Ltd'
>>> type(invoice_count) # type() works on variables too
<class 'int'>
Think of a named cell in a spreadsheet: you refer to invoice_count and you don't have to remember that the value is in B7.
Reassigning points the same name at a new value. The old value is replaced:
>>> status = "draft"
>>> status = "sent" # same name, new value
>>> status
'sent'
>>> status = 3 # Python allows a different type too...
>>> type(status)
<class 'int'> # ...but this makes a script hard to follow
Python lets you change a variable's type, but in real scripts it causes confusion. Keep each name for one kind of thing.
Naming variables so the script reads clearly
The rules (break these and Python stops with an error):
- Use letters, digits and underscores only, and don't start with a digit.
report2is fine,2reportis not. - No spaces or hyphens:
invoice-countwould be read as a subtraction. - Don't use Python's own keywords, such as
if,fororclass. - Capital letters matter:
Totalandtotalare two different names.
The conventions (what experienced Python programmers do):
- Use snake_case: lower-case words joined by underscores, for example
unpaid_total. - Choose names that describe the value:
days_overdueis better thandorx2. - Don't reuse the names of built-in functions such as
type,listorinput. Python lets you do it, but afterwards the built-in no longer works.
>>> type = "invoice" # allowed, but now type() is broken
>>> type(42)
TypeError: 'str' object is not callable
If that happens, exit and start the prompt again to get a clean session.
Exercise: check it yourself
At the prompt, create supplier_name, order_quantity, unit_price, is_approved and approved_by. Give them values of the types str, int, float, bool and None. Then run type() on each one. Check: you should see str, int, float, bool and NoneType, in that order. Next, reassign is_approved to True and type the name to confirm it changed. Then exit.
Key takeaways
python(orpyon Windows) starts the prompt andexitleaves it. You don't need brackets from Python 3.13 on.- Python 3.13 added colour, multi-line editing, F1 help, F2 history and F3 paste mode. Python 3.14 added syntax highlighting.
- At the prompt, expressions show their value without
print(). In a script they don't. type()tells apartint,float,str,boolandNoneType."42"is a string, not a number.=stores a value under a name, and reassigning replaces it.- Use clear snake_case names, and never reuse built-in names like
typeorlist.