Download Klicka för att se sidorna i PDF format

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
03.
03.
How to cook
Introduction to Python
“First shalt thou take out the Holy Pin,
then shalt thou count to three, ...”
(Python program to operate the Holy Hand Grenade of Antioch,
Monty Python and the Holy Grail)
D
uring the 1980s the holy grail of operating system design
was to create one that could manage any number of
connected computers as if they were just one. When the
operating systems guru Andrew Tanenbaum created Amoeba,
a working distributed operating system, at the Vrije Universiteit
in the Netherlands, he probably wouldn’t have thought that the
language designed to administer the system is now used far
more than the system itself. Python was born because Guido
van Rossum didn’t like the Bourne shell traditionally used for
such purposes and wanted a clear, easy-to-read language that
he could extend without difficulty.
Overview
In this section we’re first going to see how each of the things we
described in the section about programming languages applies to
Python; then we’ll go through a short program as an example. It
won’t make you an expert in Python, or even show you all of the
language, but it should allow you to start experimenting yourself.
Preparation
It’s more fun to try out examples as they’re described. In order
to do this we’re going to use two different environments:
the command line for simple examples, and the ‘Geany’
development environment for more complicated ones.
For this Raspberry Pi should be started with a graphical
desktop with a terminal (command-line) interface as was
described in the section about the first-time Raspberry Pi.
30
Raspberry Pi Manual
In Python, as in most programming languages, addition,
subtraction, multiplication and division are all available and
are evaluated just as you were taught in your maths class. The
only difference is that although the addition and subtraction
symbols are the familiar ‘+’ and ‘-’ characters, the multiplication
and division symbols are ‘*’ and ‘/’, not ‘x’ and ‘÷’.
While on the subject of sums, don’t forget the lesson when
your teacher told you that multiplications and divisions are
always done before additions and subtractions.
>>> 6+5*7+1
42
>>> (6+5)*(7+1)
88
Geany is a program that organizes one or more files
containing programs into separate ‘projects’. The program
allows these programs to be edited easily and provides a
simple way to try them out. It can be used both for Python and
for C programs (which are addressed in a later section).
Once you have a graphical user interface Geany can be
run by clicking on the icon in the bottom left of the screen and
selecting ‘Programming’ followed by ‘Geany’. If you don’t see
this on your Raspberry Pi you may need to install it first from
the Internet. To do this type this into a command-line:
Python is typically interpreted on a virtual machine. This means
there’s normally no explicit ‘compilation’ step needed before a
program can be run. In fact you can type a line of program into a
Python interpreter and see the result execute immediately.
Python has few unnecessary language features and a simple
layout. The structure of your program is partly dictated by
how many spaces you use in front of each line. Nonetheless,
it supports many of the features needed to support the best
programming practice.
It’s now been adopted so widely that there are a huge
number of standard libraries available which do all kinds of
useful things requiring virtually no work by the programmer.
Best of all, most implementations are ‘open’ and this has
helped make it very popular. If you invest time learning how to
use the language, you’ll find that you can write your programs
almost everywhere: Python is freely available on all versions of
Linux, and Windows, and Mac OS, and many other operating
systems. On systems that don’t have Python you can install it
yourself with no worries about fees or licensing. Furthermore,
Python is not a cut-down language intended only for teaching:
you can use it even for large and complex projects. On top of that
it’ll continue to be supported by a community of enthusiasts for a
long time, not simply for the lifetime of a company that sells it.
And, most importantly, it was named after Monty Python’s
Flying Circus.
>>> 6*7
42
Geany
Great things about Python
pi@raspberrypi:~$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 21:57:32)
[GCC 4.4.5 20100902 (prerelease)] on linux2
Type “help”, “copyright”, “credits” or
“license” for more information.
>>> print(“Hello, world!”)
Hello, world!
>>>
Following that everything you type will be prompted for by
the characters ‘>>>’ and treated as a python command. If you
just type in an expression Python will evaluate it and print out
the result:
A command-line interface in the desktop.
You’ll find Geany at
http://www.geany.org/
where there are instructions on how to download and install
it. Similarly you can find Python at
http://python.org/download/
Look for a Windows download.
Using Geany
Geany assumes that your program may one day become too
large for a single file and so provides a directory where all the
files in your program can be held together. Initially, though,
you’ll create only one program file.
Because you might be working on more than one program
at once Geany also remembers which ‘project’ you’re working
on and allows you to switch between one and another. This
means that the first time you use Geany you need to:
m Create a new project to work on.
m Create a new Python file to hold your program.
The following provides a step-by-step illustration of how this
can be achieved together with writing a trivial program and
running it. Running Geany for the first time gives:
sudo apt-get install geany
Interactive Python window
To try out simple Python programs and commands just type the
following into a command-line window:
python
(as we did above). Following that, anything you type in is treated
as a Python program which will be run immediately.
You can return to the command-line window by typing Ctrl-D
(for which you must hold down the ‘Ctrl’ and the ‘D’ keys at the
same time).
You can use the command-line interface to try out the
examples in the first part of this section by first typing the
command ‘python’.
pi@raspberrypi ~ $ python
Python 2.7.3rc2 (default, May 6 2012,
20:02:25)
[GCC 4.6.3] on linux2
Type “help”, “copyright”, “credits” or
“license” for more information.
>>>
Running Geany for the first time.
First create a new project using the ‘Project’ menu item.
Running Geany from the GUI.
(You’ll have to have a network connection first.) Once this
completes you should find that Geany is available as above.
You might be interested to hear that Python and Geany are
both available for your Windows computer (if you have one),
so you can try out programs there, as well as on Raspberry Pi,
if you want.
Creating a new project from the menu.
This will ask for the name of the project. Type in a name and
click on the ‘Create’ button.
Raspberry Pi Manual
31