Lesson 1 of 3
What a program is, how Python runs your instructions, and why programming is a core skill in modern science.
About 15 minutes
A program is a set of instructions that tells a computer what to do. Each instruction is called a statement. The computer carries out the statements in order and does exactly what they say: it never guesses what you meant, and it never skips a step because it seems obvious.
A useful comparison is a written laboratory procedure. A good procedure lists every step in order, with precise quantities, so that anyone following it gets the same result. A program is the same idea, except the reader is a computer and the steps are written in a language it understands.
That precision is both the strength and the challenge of programming. Once a program is correct, it will repeat the same work perfectly, millions of times if needed. But a small mistake, such as a missing bracket or a misspelled name, stops the program or gives a wrong answer, because the computer follows your instructions literally.
Computers ultimately work with very low-level instructions that are hard for people to read. A programming language lets us write instructions in a form that is much closer to human language and mathematics. The text you write is called source code.
Python is an interpreted language. A program called the Python interpreter reads your source code from top to bottom and carries out each statement as it reaches it. If it finds a statement it cannot understand, it stops and shows an error message explaining what went wrong and on which line.
Research increasingly depends on computation. Programming lets you do things that are slow, error-prone or impossible by hand:
The statement below uses print() to display a message. print is a function: a named piece of code that performs a task. You call a function by writing its name followed by brackets, and you put the information it needs inside the brackets.
print("Hello, science!")The text inside the quotation marks is called a string. The quotes tell Python where the text starts and ends; they are not printed.
Press Try it hands-on under the example to open an editor. Change the message, press Run, and see your new output. You can't break anything: the Reset button restores the original code.
Quotation marks make a big difference. Inside quotes, Python treats characters as text and prints them exactly. Without quotes, Python treats 2 + 3 as a calculation, works out the result and prints that.
print("2 + 3")print(2 + 3)Computers are very good at arithmetic. This program converts the melting point of copper from degrees Celsius to kelvin by adding 273.15.
print(1085 + 273.15)Programs run from top to bottom
Examples use materials science by default. If you work in another field, choose it with the Examples in this lesson selector, or set a default for the whole course in the header.
Choose an answer and press Check answer. You can retry as often as you like.