LESSON 2:
One-liners - very simple 'programs'
OK! We have python installed, now what? Well, we program!
And it is that simple (at least for now). Python makes it easy to run single lines of code - one-liner programs. Lets give it a go.
Go to the start menu, find Python, and run the program labelled 'IDLE' (Stands for
Integrated
Deve
lopment
Environment, it isn't literally idle... think that's dumb? it took me a day to figure that out
) If that doesn't work, go to the command line and type:
That starts the IDLE in a command line. You in now? Good.
Now you are in the IDLE environment. Type the following and press enter: (don't type >>> as it should already be there)
Code:
>>> print "Hello, World!"
What happened? You just created a program, that prints the words 'Hello, World'. The IDLE environment that you are in immediately compiles whatever you have typed in. This is useful for testing things, e.g. define a few variables, then test an equation. That will come in a later lesson, though.
Now try typing the stuff in bold. You should get the output shown in blue. I've given explainations in brackets.
Code:
>>> [b]1 + 1[/b]
[color=blue]2[/color]
>>> [b]20+80[/b]
[color=blue]100[/color]
>>> [b]18294+449566[/b]
[color=blue]467860[/color]
(These are additions)
>>> [b]6-5[/b]
[color=blue]1[/color]
(Subtraction)
>>> [b]2*5[/b]
[color=blue]10[/color]
(Multiply, rabbits!)
>>> [b]5**2[/b]
[color=blue]25[/color]
(Exponentials e.g. this one is 5 squared)
>>> [b]print "1 + 2 is an addition"[/b]
[color=blue]1 + 2 is an addition[/color]
(the print statement, which writes something onscreen)
>>> [b]print "one kilobyte is 2^10 bytes, or", 2**10, "bytes"[/b]
[color=blue]one kilobyte is 2^10 bytes, or 1024 bytes[/color]
(you can print sums and variables in a sentence. The commas seperating each section stop a few problems from occuring, which will be discussed later)
>>> [b]21/3[/b]
[color=blue]7[/color]
>>> [b]23/3[/b]
[color=blue]7[/color]
>>> [b]23.0/3.0[/b]
[color=blue]7.6666...[/color]
(division, 2nd ignoring remainder/decimals, 3rd including decimals)
>>> [b]23%3[/b]
[color=blue]2[/color]
>>> [b]49%10[/b]
[color=blue]9[/color]
(the remainder from a division)
As you see, there is the code, then the result of that code. I then explain them in brackets. These are the basic commands of python, and what they do. Here is a table (because tables look cool, and make you feel smarter
)
Code:
Command Name Example Output
+ Addition 4+5 9
- Subtraction 8-5 3
* Multiplication 4*5 20
/ Division 8/2 4
% Remainder 15/4 3
** Exponential 2**3 8
Remember that thing called order of operation that they taught in maths? Well, it applies in python, too. Here it is, if you need reminding:
1) parentheses ()
2) exponents **
3) multiplication *, division \, and remainder %
4) addition + and subtraction -
Here are some examples that you might want to try, if you're rusty on this:
Code:
>>> [b]1 + 2 * 3[/b]
[color=blue]7[/color]
>>> [b](1 + 2) * 3[/b]
[color=blue]9[/color]
In the first example, the computer calculates
2 * 3 first, then adds 1 to it. This is because multiplication has the higher priority (at 3) and addition is below that (at lowly 4)
In the second example, the computer calculates
1 + 2 first, then multiplies it by 3. This is because parentheses (brackets, like the ones that are surrounding this interluding text
) have the higher priority (at 1) and addition comes in later than that.
Also remember that the math is calculated from left to right, UNLESS you put in brackets. Watch these examples:
Code:
>>> [b]4 - 40 - 3[/b]
[color=blue]-39[/color]
>>> [b]4 - (40 - 3)[/b]
[color=blue]-33[/color]
In the first example,
4 -40 is calculated,then
- 3 is done.
In the second example,
40 - 3 is calculated, then it is subtracted from 4.
The final thing you'll need to know to move on to multi-line programs is the comment. Type the following (and yes, the output is shown):
Code:
>>> [b]#I am a comment. Fear my wrath![/b]
>>>
A comment is a piece of code that is not run. In python, you make something a comment by putting a hash in front of it. A hash comments everything after it in the line, and nothing before it. So you could type this:
Code:
>>> [b]print "food is very nice" #eat me[/b]
[color=blue]food is very nice[/color]
(a normal output, without the smutty comment, thankyou very much)
>>> [b]print "food is very nice" eat me[/b]
(you'll get a fairly harmless error message, which is a computer trying to tell you it doesn't know what 'eat me' means)
Comments are important for adding necessary information for another programmer to read, but not the computer. For example, an explanation of a section of code, saying what it does, or what is wrong with it. You can also comment bits of code by putting a # in front of it - if you don't want it to compile, but cant delete it because you might need it later.
There you go! Lesson 2 Completed. That was even shorter than lesson 1!
Next lesson, we make programs with many lines of code, and save them, so we can actually send them to people. That's right, you don't have to retype every program you run! What an amazing innovation!
Thanks to all, Gingerbread Man