After this lesson you can write a tiny Python program, run it and read the result. It is short on purpose. The skill you need is the loop of write, run and look, and you will use it for the rest of your programming life.
Hello, shop
The traditional first program prints a greeting. Here is one with a small twist:
print("Welcome to the Corner Shop")Welcome to the Corner Shopprint is a built-in function. It takes whatever you put between the brackets and shows it on the screen. The thing inside the brackets is called an argument. Here the argument is some text.
Text in Python is called a string. A string is written inside quotes so that Python can tell the difference between text and instructions. Without the quotes, Python would try to treat Welcome as an instruction and complain.
You can use double quotes or single quotes, as long as you start and end with the same kind:
print('Open every day')
print("Open every day")Open every day
Open every dayBoth lines do exactly the same thing.
Numbers and calculations
You can print numbers too, and numbers do not need quotes:
print(42)
print(3 + 4)
print(12 * 5)42
7
60The last two lines show that Python will calculate for you. 3 + 4 is not text, so Python works it out and prints the answer. Compare that with quotes around it:
print("3 + 4")3 + 4Now it is a string, and Python prints it exactly as written. Quotes mean "this is text, leave it alone". No quotes mean "this is code, work it out".
Printing several things at once
You can give print more than one argument, separated by commas. Python puts a space between them:
print("Items sold today:", 18)
print("Total in rupees:", 18 * 45)Items sold today: 18
Total in rupees: 810Each call to print starts a new line, so these two statements appear on two lines of output. A statement is one complete instruction. Python runs your statements from the top of the file to the bottom, one after another.
Running the program
Save any of the examples above in a file, for example hello.py, then run it from a terminal opened in the folder that holds the file:
python3 hello.pyOn Windows you may need python hello.py or py hello.py instead. The next lesson covers running code in detail, including an interactive mode.
Your first error
Everyone writes mistakes, and Python reports them. Seeing errors early is useful, because they stop being scary. Here is a string whose quote was never closed:
print("Welcome to the shop)SyntaxError: EOL while scanning string literalThe last line of an error message is the most helpful one. It names the kind of error, here a SyntaxError, and gives a short reason. A syntax error means the code is not written in a form Python understands. The lines above it show roughly where Python gave up.
Another common mistake is a misspelled name:
Print("Hello")NameError: name 'Print' is not definedPython is case sensitive, so Print and print are different names. This time the error is a NameError, meaning Python does not know any such name.
Reading a program like Python does
It helps to imagine yourself as the interpreter. You begin at the first line, do exactly what it says, then move to the next. You never skip ahead, and you never guess what the author meant. If a line is wrong, you stop and report the problem.
This tells you something useful about debugging. When a program misbehaves, walk through it line by line with a pencil and note what each statement does. The place where your picture and the real output stop agreeing is usually where the bug lives. Beginners often try to understand a whole program at once, which is far harder than following it one step at a time.
It also explains why blank lines and spacing between statements do not change what happens. Python ignores blank lines, so you are free to use them to group related steps and make the file easier to read.
Common mistakes
- Forgetting a closing quote or bracket. Every opening quote and every opening bracket needs its partner.
- Using capital letters in print. It must be
printin lowercase. - Leaving out the brackets. In Python 3,
printis a function and needs them. - Using curly or "smart" quotes. If you copy code from a word processor, quotes may be curly. Python only accepts the plain straight kind.
- Starting a line with spaces by accident. Leading spaces carry meaning in Python, so a stray one can cause an error.
Exercise
Write a program that prints three lines: the name of an imaginary shop, a line that says how many customers came today (use the number 25), and the amount they spent in total, which is 25 customers times 40 each, calculated by Python and not by you.
Show one possible answer
print("Corner Shop")
print("Customers today:", 25)
print("Total spent:", 25 * 40)Corner Shop
Customers today: 25
Total spent: 1000The last line lets Python do the multiplication, so if a number changes you only edit that number. Notice that the text has quotes and the calculation does not.
Next: more ways to run Python code.
Key points
- print() shows text, numbers and other values on the screen.
- Text in quotes is a string. Single quotes and double quotes both work, but you must close what you open.
- Python runs statements from top to bottom, one at a time.
- Error messages name the problem and the line. Read the last line first.