python isn't just a snake

4
3-1: Introduction to Python Objectives: Basic familiarity with the IDLE tool Basic data manipulation using Python syntax All the software for this sequence should already be on the Pi, as part of Raspbian. Verify that the IDLE tool is available in the X Windows System. What's the point? Giving the computer instructions, to tell it to do exactly what you want. It can't do what it doesn't know, so we humans have to decide how to teach it, and craft our programs appropriately. Games, web, documents, security, music, virtually anything is possible. Basic data manipulation: Type a number into IDLE and press enter. It examines what you told it, does it, and tell you the result. In this case, it tells you what number you entered...because there is nothing else to do. Try some basic math, e.g. 1 + 1. It tells you the answer is 2. Some basic operators are as follows: + (addition) - (subtraction) * (multiplication) / (division) % (modulo or remainder, as in 10 mod 3 == 1) ** (power, as in 4**2 = 4 squared = 16) Try 10 / 3. Notice that the answer is 3. Now try 10 / 3.0. Notice that the answer is 3.3333333. If you tell it that you care about decimal places, you get decimal numbers. Otherwise, you get integers. Take a few minutes to use Python as a calculator. There are some editing shortcuts that might also help at this point. Ctrl-End will always get you back to the current prompt, if you move the cursor elsewhere. Alt-p will put the previous entry at the prompt again. If you hit it again, it will cycle through entries previous to that as well. Alt-n will go to the next entry. This is particularly useful if you are looking for something in particular with Alt-p, but you pass it. Ctrl-c will interrupt what is currently happening, clear the current programming entry, and put your cursor at the prompt. If you don't know

Upload: geekinlibrariansclothing

Post on 18-Jul-2015

37 views

Category:

Education


1 download

TRANSCRIPT

Page 1: python isn't just a snake

3-1: Introduction to Python

Objectives:

Basic familiarity with the IDLE tool Basic data manipulat ion using Python syntax

All the software for this sequence should already be on the Pi, as part of Raspbian. Verify that the IDLE tool is available in the X Windows System.

What's the point? Giving the computer instruct ions, to tell it to do exactly what you want. It can't do what it doesn't know, so we humans have to decide how

to teach it , and craft our programs appropriately. Games, web, documents, security, music, virtually anything is possible.

Basic data manipulat ion:

Type a number into IDLE and press enter. It examines what you told it , does it , and tell you the result . In this case, it tells you what number you

entered...because there is nothing else to do.

Try some basic math, e.g. 1 + 1. It tells you the answer is 2. Some basic

operators are as follows:

+ (addit ion)

- (subtraction) * (mult iplication)

/ (division)

% (modulo or remainder, as in 10 mod 3 == 1) ** (power, as in 4**2 = 4 squared = 16)

Try 10 / 3. Notice that the answer is 3. Now try 10 / 3.0. Notice that the answer is 3.3333333. If you tell it that you care about decimal places, you get decimal

numbers. Otherwise, you get integers.

Take a few minutes to use Python as a calculator. There are some edit ing

shortcuts that might also help at this point. Ctrl-End will always get you back to the current prompt, if you move the

cursor elsewhere. Alt-p will put the previous entry at the prompt again. If you hit it again, it

will cycle through entries previous to that as well.

Alt-n will go to the next entry. This is part icularly useful if you are looking for something in part icular with Alt-p, but you pass it .

Ctrl-c will interrupt what is current ly happening, clear the current

programming entry, and put your cursor at the prompt. If you don't know

Page 2: python isn't just a snake

what is happening, this can help you reset to a known state.

Copy and paste do work. Be careful of indentation if you choose to paste, and also be careful of the >>> characters.

Variables:

(Attendees who have done some algebra will find this much easier.)

In its basic form, a variable is a box. You can put things in the box, you can take things out of the box, you can forget what's inside, and then look to see what it

is. When you carry it around, though, it 's st ill a box. Boxes come in different shapes and sizes, but all of ours are going to be the same size for now.

Let 's put the number 12 in a variable, by telling Python “x = 12” – not ice that Python responds by telling us absolutely nothing. Now, let 's ask Python what x is:

>>> x

12

>>>

Next, let 's use our variable in some arithmetic expressions. Try things like x + 1,

x**x, 42 / x, etc. Notice that it works just like the number 12.

Now, let 's change x to something else. Try the following:

>>> x = x - 3

>>>

We can change what's in the box without actually looking at it , which is

convenient. Have a look to see what its value current ly is.

>>> x 9

>>>

You can call variables pretty much whatever you like, as long as you use letters

and no spaces. Numbers are okay within a variable name, and mult i-word variable names are usually dist inguished by capitalizing all but the first one. So,

the following are all valid: x, y, xy, george, john, paul, ringo, octopus,

thisIsAVeryLongVariableName, theSong, grocerylist . Be descript ive, and try to label your boxes so you know what's inside them.

Page 3: python isn't just a snake

Strings:

We've seen how Python can act as a calculator with numbers, and how we can use words to name variables. What if we want Python to talk to us? If you try to

tell it “hello” you get a nasty-looking error:

>>> hello

Traceback (most recent call last):

File "<pyshell#22>", line 1, in <module>

hello NameError: name 'hello' is not defined

>>>

Such errors are commonplace and not to be feared. They simply indicate that

something went wrong, and usually provide useful information for fixing the problem. In this case, Python doesn't know anything about hello – it is t rying to

interpret it as a variable or other identifier. We want it to be treated as a literal

st ring of characters, so we have to put it inside quotation marks:

>>> "hello" 'hello'

>>>

Notice that when Python tells us 'hello' it is between a pair of apostrophes, also

called single quotes. Either single or double quotation marks are acceptable, as long as they match.

We can also add strings together, and we can put them in variables. For example:

>>> y = 'hello'

Page 4: python isn't just a snake

>>> y

'hello' >>> y + " world”

'hello world'

Remember how we have the number 9 in x at the moment? It is often useful to

insert the values of numeric variables into strings, but to do so we must use a different character: the backtick. It points toward the right, while the

apostrophe On US keyboards, it is usually on the same key as the t ilde.

>>> y + " " + 'world' + `x`

'hello world9' >>>

This may be a reasonable stopping point – the attention to detail required for

success throughout these examples may take some effort for attendees, especially if you have some who do not have experience with algebra. Pack

up the Pi kits as normal.