Starting python is easy.
cmd
and press Enterpython
and press Enter.Similarly from a terminal in Linux simply type python
and press Enter.
Python 2.4.3 (#1, Oct 2 2006, 21:50:13) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
What you see now is known as the python interactive shell. The first
line tells you what version of python you are running. The second line
gives you some information about how this particular copy of python was
built, and on what system it is running. The third line lists some
commands you can use to get more information. Whilst in the interactive
shell, you can enter python expressions and see their results
immediately. Try it now, type in a simple mathematical expression such
as 4 + 7
.
>>> 4 + 7 11 >>>
As you can see, the answer or result of the expression, is printed
out, and you are returned to the prompt ">>>" Now try
something different; type in a = 2 + 7
.
>>> a = 2 + 7 >>> a 9 >>>
This should be somewhat familiar from the end of the basic concepts section, where we were assigning a value to a name. In this case the name is 'a' and the value is the result of '2 + 7'. Note that no value is printed out immediately after the assignment. The interactive shell always prints out the value of expressions, but not of statements. This means however that the labels to which we assign values, more correctly called variables, are expressions that evaluate to a value.
To quit the interpreter in windows press CTRL-Z, and in Linux press CTRL-D.
Whilst ideal as a calculator and for exploring new ideas quickly, it would be pretty tedious if we had to re-enter our program into the interactive shell every time we wanted to run it. So instead we can, and in fact most often will, save our program code to a file. Program source code is plain raw text. As such word processors like Microsoft Word, Wordperfect, Open Office etc... are not suitable to the task. I honestly and fervently recommend learning to use either Vim or EMACS. Both are available for both Windows and Linux, and are widely regarded as the two most powerful editors ever written. Both, however, have fairly steep learning curves, and it would be unwise to try to learn to program and use a new editor at the same time, so look for Idle which is specifically geared towards creating python programs, and comes with python. Eclipse might also be a comfortable choice, especially if you have used windows IDE's before. If you don't know what an IDE is, don't bother with Eclipse just yet.
So now, instead of running the interactive shell, let's write our first python program. Create a new directory and in it create a new text file, call it 'first.py', and put the following into it
#My first python program 4 + 7 #This should add two numbers and output the result a = 2 + 7 a
Notice the first and second lines. They contain what are called comments. Any text following a hash character on a line in a python program is a comment, including the hash itself. Comments are completely ignored by python, and are there purely to annotate code and make things easier for humans reading the code. We use comments to place small reminders within the code for ourselves, or explain the logic behind particularly tricky sections, etc... As we progress through the course, you will find the code examples used sprinkled more and more liberally with comments explaining how they work.
Now it would be reasonable to expect that if we ran this program we
would get the same results as given to us by the interactive shell. So
let's try it. At your command prompt, change to the directory where you
put 'first.py' and type python first.py
sirlark@hephaestus ~ $ python first.py sirlark@hephaestus ~ $
No output? Nothing? The python interpreter (python), when called without a file name following, starts up in the interactive shell. Only then will it output the results of expressions entered. When invoked with a file name following, and that file contains python code, python will only produce output if explicitly told to do so. So we'll just make a quick modification to 'first.py'
#My first python program 4 + 7 #This should add two numbers and output the result a = 2 + 7 print a
sirlark@hephaestus ~ $ python first.py 9 sirlark@hephaestus ~ $
Ah, that's better. Again, you should recognise the print statement from the end of the basic concepts section. The print statement will be explained in greater detail the next section.
One more important consideration is that python is case sensitive. A variable 'a' and another variable 'A' are not the same, as illustrated below ...
#This program illustrates how python is case sensitive a = 3 #assign a the value 3 A = "Hi" #assign A the value "Hi" #check whether assigning to A has changed a print a #check that A and a are still different print "A = ", A, " and a = ", a
which produces as output
3 A = Hi and a = 3
Consider the following lines code...
a = 9 b = 3 a/b