First Steps: Output, Comments and Errors
Your first program, how to read an error message instead of panicking at it, and the habits that will save you hours later.
Your first program
print("Hello, world!")That is a complete program. Run it and the text appears.
What each part does
printis a function, a built in piece of code that does a job- The brackets hold what you are giving it
- The quotation marks mark the start and end of the text
Printing several things
print("Hello", "world", "again")
print("The answer is", 42)
print("Line one")
print("Line two")A comma puts a space between items and lets you mix text and numbers freely.
Comments
A comment is a note for humans. Python ignores everything after a #.
# Ask the player for their name
name = input("Name: ") # input always gives back textComment why, not what. count = count + 1 # add one is useless. # skip the header row is worth having.
- print() is a function: it displays whatever is inside the brackets.
- Text goes inside quote marks. Text in quotes is called a string.
- Anything after a # is a comment. Python ignores it: it is a note for people.
- Run it and the output appears. Python works from the top, one line at a time.
Reading error messages
Beginners see a red error and panic. Experienced programmers read it, because it usually says exactly what is wrong and on which line.
File "main.py", line 4
print("Hello"
^
SyntaxError: '(' was never closedThat tells you the file, the line, the position and the problem.
The errors you will meet first
| Error | Usual cause |
|---|---|
SyntaxError | A missing bracket, quote or colon |
IndentationError | Lines under an if or a loop are not lined up |
NameError | A variable used before it exists, or a typo in its name |
TypeError | Mixing incompatible types, such as text plus a number |
ValueError | The right type but an impossible value, such as int("hello") |
ZeroDivisionError | Dividing by zero |
Read the last line of the error first. It names the problem. Then read the line number. Fix that line before looking anywhere else.
Three habits worth building now
- Run your code often. After every few lines, not after fifty. Then you know exactly what broke it.
- Name things properly.
total_scoreis better thantsevery single time. - Print things to check. If you are unsure what a variable holds, print it. This is the single most useful debugging technique there is, and professionals still use it daily.
- Syntax error: the code breaks the rules of the language, so it will not run at all.
- Runtime error: the program starts, then crashes when it meets something it cannot do.
- Logic error: it runs without complaint but gives the wrong answer, so only testing finds it.
Key terms you must be able to define
- Function
- A named piece of code that performs a job, such as print.
- Argument
- A value you give to a function inside its brackets.
- String
- Text data, written inside quotation marks.
- Comment
- A note in the code for humans, ignored by Python, starting with a hash.
- Syntax error
- A mistake in how the code is written, which stops it running at all.
- Runtime error
- An error that occurs while the program is running, such as dividing by zero.
Flashcards
Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.
Reach the top grade on this topic
Getting good at Python early is mostly about habits rather than knowledge.
- Run your code every few lines so you always know what broke it
- Read the last line of an error message first, then the line number
- Use full descriptive variable names from the very beginning
- Print variables when you are not sure what they contain
- Comment why you did something, never what the line obviously does
Mistakes that cost marks here
- Forgetting the quotation marks around text.
- Forgetting a closing bracket, which produces a syntax error on the following line.
- Ignoring the error message and just changing things at random.
- Writing fifty lines before running any of it.
The tradition of making Hello World your first program comes from a 1972 tutorial by Brian Kernighan. Over fifty years later it is still the first thing almost every programmer ever writes.
Knowledge check
print("Hello") do?print("Score", 10) display?int("hello")?Exam-style questions
Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.
- LA note for humans reading the code
- LIgnored by the computer when the program runs
A comment is a note written in the code to explain something to a person reading it, such as why a particular approach was taken. It is completely ignored by Python when the program runs, so it has no effect on what the program does.
- LA syntax error breaks the rules of the language
- LSo the program will not run at all
- LA runtime error occurs while the program is running
- LGives valid examples such as a missing bracket and dividing by zero
A syntax error is a mistake in the way the code is written, such as a missing closing bracket or a missing colon at the end of an if statement. Because the code does not follow the rules of the language, Python cannot interpret it at all and the program will not run, so the error appears before anything happens. A runtime error occurs in code that is perfectly valid but which fails while it is executing, for example dividing by zero or trying to convert the text hello into a number. The program starts normally and then crashes at the point the problem occurs.
print("Hello) and gets an error. Identify the error and correct it.2 marks- LThe closing quotation mark is missing
- LCorrect code is print("Hello")
The string has an opening quotation mark but no closing one, so Python reaches the end of the line still expecting the text to continue and reports a syntax error. The corrected line is print("Hello") with a closing quotation mark before the closing bracket.
- LIt shows what the variable actually contains at that point
- LThis may be different from what the programmer assumed
- LIt narrows down which part of the program is wrong
Printing a variable shows exactly what it holds at that moment in the program, which is frequently not what the programmer assumed it held. Many bugs come from a variable containing text when a number was expected, or holding a value from an earlier iteration, and these are invisible just by reading the code. By printing values at several points, the programmer can see where the value first becomes wrong, which narrows the problem down to a small section rather than the whole program.
- LIncludes a comment
- LUses print correctly with quotation marks
- LProduces output on two separate lines
# Display some information about me print("My name is Aisha") print("My favourite subject is Computing") The comment on the first line explains the purpose of the program and is ignored when it runs. Each print statement outputs its text and then moves to a new line, so using two separate print statements produces two lines of output.