Lesson 3 of 3
Write a program with several statements, control what print() displays, add comments, and read error messages.
About 20 minutes
print() statementsprint() is the simplest way to see what your program is doing. Text must be inside quotes. You can use double quotes ("...") or single quotes ('...'), as long as the opening and closing quotes match. Numbers and calculations do not need quotes.
Each call to print() writes one line of output and then moves to a new line, so a program with three print() statements produces three lines.
print("Sample report")print('Temperature in kelvin:')print(25 + 273.15)You can give print() several values separated by commas. It prints them on the same line with a single space between each one. This is a convenient way to label a number.
print("Atoms in an FCC unit cell:", 4)If you want something other than a space between the values, add sep= (short for separator) as the last item inside the brackets. An empty print() prints a blank line, which is useful for spacing out output.
print("Fe", "Cr", "Ni", sep=", ")print()print("Fe", "Cr", "Ni", sep="-")Every language has rules about how statements are written. In programming these rules are called syntax. Python's syntax is fairly relaxed, but a few rules matter from the start:
print works, but Print and PRINT do not.A comment starts with #. Python ignores everything from the # to the end of that line. Comments are notes for people reading the code, including you in six months' time when you have forgotten why you wrote it.
A comment can sit on its own line, usually just above the code it describes, or at the end of a line of code. Good comments explain why something is done or what units a value is in; they don't need to repeat what the code obviously does.
This program reports the density of aluminium. The comments explain where the value comes from and its units.
# Density of aluminium at room temperatureprint("Material: aluminium")print("Density (g/cm³):", 2.70) # handbook valuePython prints 2.70 as 2.7. Trailing zeros after the decimal point don't change a number's value, so Python doesn't show them.
Commenting out code
# in front of a line of code stops it from running without deleting it. This is a quick way to test what happens when a line is removed.Everyone makes mistakes when programming, including experienced developers. When Python can't run your code it stops and shows an error message. Learning to read these messages is one of the most useful skills you can develop. Run this example to see one:
print("Hello, lab)An error message has three useful parts:
SyntaxError, NameError or TypeError.Here the closing quote is missing, so Python thinks the text never ends. Add " before the closing bracket and run it again.
A NameError means Python doesn't recognise a name. It often comes from a typo or the wrong capitalisation:
Print("Hello, lab")| Error | Usual cause |
|---|---|
SyntaxError | The code breaks Python's grammar: a missing quote, bracket or comma. |
NameError | A misspelled name, wrong capitalisation, or using a name before creating it. |
IndentationError | Unexpected spaces at the start of a line. |
TypeError | An operation on the wrong kind of value, such as adding text to a number. |
A simple debugging routine
Choose an answer and press Check answer. You can retry as often as you like.