A program is an ordered list of instructions. The machine reads them one at a time, from top to bottom, and does what each one says.
An algorithm is the recipe: the steps stated in plain language. Code is that recipe rewritten in a language the machine accepts.
a, b, c.a and b. Keep whichever is larger.c. Keep whichever is larger.Python reads your file while it runs, so nothing happens until you ask it to run, and a mistake on line 40 stays quiet until the first 39 lines have already done their work. Notice the first line stores a value and prints nothing at all: only print writes to the console.
A high level, interpreted, dynamically typed programming language created by Guido van Rossum in 1991 and built around readable syntax.
| Python | C / Java | |
|---|---|---|
| Before running | run the file straight away | compile first, then run |
| Types | attached to values at run time | declared in the code |
| Execution speed | slower | faster |
| Lines for a small task | few | more |
| Common use | data, scripting, prototyping, AI | systems, performance critical software |
Python trades raw speed for speed of writing. In most of what you will do here, your own time costs more than the machine's.
Both write the same line. Python asks you to name the action and nothing more, which is one reason it is so often chosen as a first language.
A plain .py file. Text saved on disk, launched from a terminal with python hello.py. The whole file runs from the first line to the last, every time.
An editor built for code, such as VS Code or PyCharm. You still write .py files, and the editor adds colouring, error marks as you type, autocompletion, a debugger, and a run button.
A notebook on your own machine. Code lives in cells and you run one cell at a time. Results and plots appear under the cell, and text cells let you write explanations beside the code.
Run these in either order. Each cell keeps showing its own result until you run it again, which is what "cells can run in any order" meant above.
A notebook in the browser, hosted by Google. Same cell model as Jupyter with nothing to install, files stored in Drive, and free access to a GPU.
| Script (.py) | Notebook (.ipynb) | |
|---|---|---|
| Unit you run | the whole file | one cell |
| Order of execution | always top to bottom | whichever order you click |
| Where results appear | the terminal | under each cell |
| Prose and images | comments only | text cells, plots, tables |
| Version control | clean | messy, since output is stored inside the file |
Explore in a notebook, ship in a script. Most people use both and move code from one to the other.
A built in function that writes its arguments to the console and then moves to a new line.
print("hi") writes hi.print(7 * 6) writes 42.print("2 + 2") writes the text 2 + 2, while print(2 + 2) writes 4.print "hi" is a syntax error in Python 3.Print and PRINT are different names, and Python knows neither.This console understands print statements, which is all today's topic needs. Remove a quote or a parenthesis to see what an error message looks like.
Text inside a program that Python ignores. Everything after a # on a line is skipped, so comments cost nothing when the program runs.
# in front of a line to stop it running while you test.With the box ticked you see the program as the interpreter sees it. Comments are written for the next person reading the file, and that person is often you in three months.
sep and end.