First steps
To begin with, you should have a window on your screen with Python
somewhere in its
title, displaying a line that looks something like this:
>>>
That gt;>> is called a prompt, which means
it's something the computer displays to tell you it's ready for your
instructions. You can type things into that window, and the computer will obey
them when it understands your commands.
Unfortunately, the computer doesn't understand English. If you type in this:
>>> Tell me the sum of twelve and thirteen.
It won't understand it at all. Instead, because the computer doesn't speak our language, you have to talk to it in a special language that is designed to be easy for the computer (and hopefully you) to understand. In fact, there are lots of languages designed for computers and humans to communicate with eachother; the one you're going to learn is called Python. One of the good things about Python is that it's pretty easy for humans to understand too.
Here's how you ask the computer to tell you the sum of twelve and thirteen. Try
it yourself. (You don't need to type in the >>>, but you
do need to hit the key marked Enter after typing the line.)
>>> 12 + 13
Here are some more examples, with the computer's answers shown too.
>>> 1 + 2 + 3 + 4 10 >>> 1 + 2 * 3 - 4 # Use * for multiplication, not x. 3 # If you expected 5, think again! >>> 200 * 300 60000 >>> 12 / 4 # Use / for division. 3
Now, here's a bit of a surprise.
>>> 7 / 3 2
You might have expected it to say 2.3333333 or 2 1/3, but in fact the remainder just gets thrown away. There are ways of getting a more accurate answer; you'll find out about them later.
Try experimenting some more with using Python as a calculator. Maybe try having it do some more complicated things (experiment with more numbers and bigger numbers. Does it slow down at all? Or hesitate? Or is it pretty fast?).
You can use parentheses to group operations (anything you use to manipulate a number, like + and *) in Python just as you do in
mathematics:
>>> (1 + 2) * (3 + 4) 21
Here, Python has calculated (1 + 2) and (3 + 4) (getting 3 and 7), and then multiplied the results together.
Incidentally, if you're still confused about the fact that
1 + 2 * 3 - 4 gives 3 and not 5, the
reason is that multiplication happens before addition. Your
math teacher probably calls this BODMAS, PEMDADS, or something similar. If you're still
confused, try asking your math teacher to explain it to you.
Different types of objects
So far, all the things you've worked with have been numbers. But Python can handle plenty of things besides numbers. For instance, try the following:
>>> 'hello, ' + 'world'
What did you think the computer would do? What did it do?
Things between quotation marks are called strings. As you might guess
from the lines above, you can apply operations like + to strings as well as to numbers. The plus sign
concatenates strings; that is, puts one immediately after the other.
A little more surprising:
>>> 3 * 'hello'
3 * 'hello', so that you remember.
You can surround strings in either single quotes or double quotes; Python doesn't mind.
>>> 'ham' + "mock" 'hammock'
Why would you care about that? Well, suppose you wanted a string containing
the text I'm sorry? Try it.
Python also has lists:
>>> [1, 2, 3] [1, 2, 3] >>> [1, 2, 3] + [7, 8]
Again, I haven't told you what Python says to that last thing. Write it down in your notes.
Giving names to things
Suppose you know that you're going to need to do a lot of calculations involving the number 123456. (Maybe it's your annual salary, or something.) You could just type the number in every time:
>>> 123456 * 3 370368 >>> 123456 / 6 20576 >>> 123456 - 1000 122456
This might get very boring after a while. And if anyone else wanted to read
what you were doing, they might be confused by the mysterious number
123456 and wonder why it appeared so often.
You can solve either of these problems by giving the number a name. To save
typing, give it a short name, like n (short for number
,
maybe). To make it more obvious what it means, give it a longer name, like
salary. Here's how you do that.
>>> salary = 123456 >>> salary * 4 493824 >>> salary / 12 10288 >>> salary 123456
The idea is that, after you've said salary = 123456, you can
always type salary instead of 123456. This is because the symbol = means "is assined to" not "is equal to" like you were tought in math class. So now 123456 "is assigned to" the word salary.
You can name things other than numbers, too. For instance:
>>> MyName = 'Gareth' >>> 'Hello, ' + MyName + '!' 'Hello, Gareth!'
Doing something over and over again
So far, you've done very little that your pocket calculator couldn't do equally well. Here's something your calculator probably isn't so good at. The extra spaces on the second line are important, by the way! (This is explained in more detail in Sheet 2 (Turning the Tables).)
>>> for x in 1, 2, 3, 4, 5: ... print x, x * x # The prompt changes, to tell you Python is expecting more. ... # Just press Enter.
Can you guess what this will do?
... If you guessed that it prints out the numbers from 1 to 5 along with their squares, well done. Notice that Python conveniently puts a space between the two things you've asked it to print.
print command is used when you want to make the computer
display things. The reason you haven't needed it before is that when you type it something that has an answer Python can work out, it automatically displays
that answer. Things with answers are called expressions, for some
reason. But Python doesn't print out every value it computes;
only the values of expressions you type in at the >>>
prompt, and things you tell it to print out using the print
command.
Graphics
You can use Python for drawing pictures. Try this. The first two lines will probably seem rather weird; I'll explain them later.
>>> from gasp import * >>> begin_graphics() >>> Line((100, 100), (200, 200)) >>> Circle((320, 240), 40) >>> Box((400, 300), 100, 85) >>> end_graphics()
It's all gone horribly wrong
At some point when you're using Python, something like this is going to happen.
>>> 3 + 'aardvark' Traceback (innermost last): File "<stdin>", line 1, in ? TypeError: number coercion failed >>>
This looks pretty scary. Don't be scared. It's just Python's way of saying you did something it didn't understand. You can probably ignore everything except the last line. To understand the last line, have a look at Sheet E (Errors), which has a list of common complaints Python might make at you and explanations of what they might mean.
What next?
There are two kinds of worksheets for you to experiment with.
- Activity sheets, each of which takes you through writing a program to do something that might be interesting. These sheets are numbered: Sheet 2 (Turning the Tables), Sheet 3 (Pretty Pictures) and so on. (The sheet you've almost finished reading now is Sheet 1.)
- Information sheets, each of which tells you something useful about Python. These sheets are lettered: Sheet D (Dictionaries), Sheet L (Loops), or whatever. Usually the letters have something to do with what the sheets are about, but that hasn't always been possible.
It's generally a good idea for you to work through the activity sheets in order. Each one will point you to a few information sheets that you'll want to have handy when working through it. The next one is Sheet 2 (Turning the Tables).
This document is part of the Gasp Python Course. You may modify and/or distribute this document as long as you comply with the LiveWires Documentation Licence: a copy of the licence is included as a comment within this document.
For the lore source of these sheets, click here.