Computer Science · Ch 5 — Getting Started with Python
Introduction to Python
Introduction to Python
A computer can only act on instructions it is given, so before a computer solves a problem for us, we must spell out the solution as a precise sequence of instructions. Having practised writing algorithms in the previous chapter, the next step is to turn such step-by-step solutions into working programs, and Python 3 (any version) is the language this book uses for that.
Program and programming language
- A program is an ordered set of instructions that a computer executes to perform a specific task. The order matters — the computer carries the instructions out in the sequence given.
- A programming language is the language in which this set of instructions is written for the computer.
Low-level and high-level languages
A computer's own language is made entirely of 0s and 1s. This binary language is called machine language or low-level language. Writing or reading long strings of 0s and 1s is extremely hard for people, and this difficulty is what led to high-level programming languages — languages such as Python, C++, Visual Basic, PHP and Java — which are far easier for humans to write and understand. The trade-off is that a computer cannot execute high-level instructions directly; they must first be translated into machine language.
A program written in a high-level language is called source code. As seen in Chapter 1, language translators — compilers and interpreters — carry out the translation of source code into machine language.
How Python is translated: interpreter vs compiler
Python uses an interpreter to convert its instructions into machine language so the computer can act on them.
- An interpreter works on the program one statement at a time: it translates a statement, executes it, then moves to the next. This translate-and-execute cycle continues until either an error is met or the whole program has run successfully — in both situations, execution stops at that point.
- A compiler, in contrast, translates the entire source code in one go into object code. Only after scanning the complete program does it report any error messages it found.
So the practical difference a beginner notices is this: with an interpreted language like Python, the program runs line by line and halts at the first faulty statement it reaches, whereas a compiled language checks the whole program first and produces the full list of errors before anything runs.
print("Programs run instruction by instruction")
print("An interpreter translates and executes each line in turn")
Programs run instruction by instruction
An interpreter translates and executes each line in turn
The chapter opens with a thought from Donald Knuth, who described computer programming as an art — it applies accumulated knowledge, demands skill and ingenuity, produces objects of beauty, and a programmer who approaches it as an artist both enjoys the work more and does it better.
What this chapter covers
The rest of the chapter builds up Python from its foundations, in this order: an introduction to Python and how to work with it, Python keywords, identifiers, comments, data types, operators, expressions, statements, input and output, type conversion, and finally debugging — the skill of finding and removing errors from programs.