Coding Made Simple 2016
Coding Made Simple 2016
W
NE
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
EDITORIAL TEAM
CONTRIBUTORS
EDITOR
Neil Mohr
ART EDITOR
Efrain Hernandez-Mendoza
EDITOR-IN-CHIEF
Graham Barlow
IMAGES
MANAGEMENT
EDITORIAL DIRECTOR
MARKETING
CIRCULATION
MARKETING MANAGER
Richard Stephens
Juliette Winyard
Phone +44(0)7551 150984
Paul Newman
GROUP ART DIRECTOR
Steve Gotobed
LICENSING
PRODUCTION MANAGER
Mark Constance
Matt Ellis
matt.ellis@futurenet.com
Phone +44(0)1225 442244
PRODUCTION CONTROLLER
Vivienne Calvert
SUBSCRIPTIONS
UK reader order line & enquiries: 0844 848 2852
Overseas reader order line & enquiries: +44 (0)1604 251045
Online enquiries: www.myfavouritemagazines.co.uk
PRINTED IN THE UK BY
William Gibbons on behalf of Future.
Distributed in the UK by Seymour Distribution Ltd,
2 East Poultry Avenue, London EC1A 9PT. Phone: 020 7429 4000
WorldMags.net
WorldMags.net
Welcome!
Learning to code will change the way you think
about the world. Its an exciting journey!
Coding is the new cool.
With the internet
driving a new world of
information exchange,
business start-ups and
online gaming, coders
have suddenly become
the gatekeepers to
these new realms. Combine the huge
interest in learning how to create and
control these worlds with the surge of
cool devices, such as the Raspberry Pi,
and youve got a head of technological
steam the world hasnt seen since the
coding craze of the early 1980s.
Back then, it was more Code Britannia
than Cool Britannia jump forward to
today, and Britain is again firmly at the
heart of a web and coding revolution. A
Brit invented the web, a Brit designed
the Raspberry Pi, and Britain is firmly
How are we doing? Email techbookseditor@futurenet.com and let us know if weve lived up to our promises!
WorldMags.net
WorldMags.net
Contents
Get coding
10
16
19
28
....................................................................................................
.........................................................................................
....................................................................................
.....................................................................................................................
Coding basics
Lists
Functions and objects
Conditionals
Variables
Program structure
Recursion
Sorting algorithms
Integers
Using loops
Compilers
Common mistakes
32
34
36
38
40
44
46
48
50
52
54
..................................................................................................................................................................................
....................................................................................................
..............................................................................................................................................
.............................................................................................................................................................
.................................................................................................................
........................................................................................................................................................
..................................................................................................................
.................................................................................................................................................................
..................................................................................................................................................
........................................................................................................................................................
...............................................................................................................
WorldMags.net
WorldMags.net
Further coding
58
60
62
64
66
68
70
74
76
78
Data types
More data types
Abstraction
Using files
UNIX programs part 1
UNIX programs part 2
UNIX programs part 3
Modules
Persistence
Databases
......................................................................................................................................................
............................................................................................................................
..................................................................................................................................................
........................................................................................................................................................
...................................................................................................
..................................................................................................
...................................................................................................
.................................................................................................................................................................
...................................................................................................................................................
.......................................................................................................................................................
Raspberry Pi
Getting started
Starting Scratch
Further Scratch
Coding with IDLE
Python on the Pi
Python 3.0 on the Pi
Advanced sorting
Hacking Minecraft
Image walls in Minecraft
2048 in Minecraft
82
84
88
92
94
98
102
108
110
114
..................................................................................................................................
............................................................................................................................
...............................................................................................................................
..........................................................................................................................
...........................................................................................................................
..........................................................................................................
..................................................................................................................
..............................................................................................................
......................................................................................
..................................................................................................................
Coding projects
Python 3.0 primer
Build a Gimp plug-in
Image filters
Sound filters
Twitter scanner
Build a GUI
..............................................................................................................
.....................................................................................................
120
124
128
134
138
142
..........................................................................................................................................
........................................................................................................................................
.........................................................................................................................
...............................................................................................................................................
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
Get coding!
Everything you need to
start coding today
Get started with Linux
Get coding in Linux Mint
Join the Coding Academy
How to use an IDE
10
16
19
28
........................................
.............................
........................
........................................................
WorldMags.net
WorldMags.net
GET STARTED
WITH LINUX
It runs on most desktop and
laptop PCs, its free and you
dont even need to install it.
Lets look at Linux!
WorldMags.net
WorldMags.net
VirtualBox
Get VirtualBox
Create a machine
Up and running
A big reason why Linux makes such a good
development platform is that it was created by
Getting apps
With Windows, in the past youve been used to
getting programs by downloading them from
here, there and everywhere. More recently, the
introduction of the Windows Store has at least
centralised where software comes from and
removed the worry of getting infected by
viruses and malware. The fact is that with
Linux, the key way of getting new tools and
Top tip
Whats a distro?
Unlike Windows and Mac OS X, because
Linux is free software, anyone can take it and
effectively create their own OS to distribute.
In the Linux world, these are called distros for
short, and there are literally hundreds out
there not all good, not all maintained, but
hundreds nonetheless.
WorldMags.net
WorldMags.net
Easy ways to run Linux
a DVD. When you first turn on a PC, you can
usually get it to boot from alternative media by
pressing F11/F12, or hold down C on the Mac
some PCs boot from a suitable optical disc by
default.
Another option is to install VirtualBox from
www.virtualbox.org (see previous page). Install
and run this it looks complex, but creating a
virtual PC is pretty easy if you stick to the default
settings. The main stumbling block is ensuring
under Storage that you add the ISO file to the
could wish for. Its not why were here but you
can also download Valves Steam gaming client
and take advantage of over 1,700 Linux games
it has to offer.
Drivers
Were not really here to talk about using Linux
in every detail but there are a few standard
questions that often crop up when people
move over from Windows. One key one is
where are all the drivers? The cool thing with
Linux is that, on the whole, theres no need to
worry about drivers theyre built into the
Linux kernel. That isnt to say you cant add
drivers, but theyre generally not required.
There are a couple of exceptions: certain more
obscure laptop wireless cards can cause
issues, while if you want maximum 3D gaming
performance, you need to install the dedicated
graphics driver from your cards manufacturer.
The Terminal
If youve heard of Linux, then one area you
might fear or just wonder about is a thing
UNetbootin Linux
Install Mint
WorldMags.net
WorldMags.net
Debian
www.debian.org
One of the older distributions
on the block, Debian is also
technically the father of the
most number of spin-off distros, including one
of the most popular in the world Ubuntu (see
below). Debian itself can be thought of a barebones distro, because it comes with just
enough to get it installed and up and running.
Its really designed for servers and experts, but
its very stable and has complete repositories
of software, which means that it is also very
easy to extend.
Ubuntu
www.ubuntu.com
This is arguably the most
popular or at least the most
widely known Linux distro
in the world. As we mentioned above, Ubuntu
was spun out of the Debian project to create an
easy-to-use distribution that would be suitable
for anyone and everyone to use. Over the years,
a huge number of spin-offs have been created
both official, such as Ubuntu Server, but also
unofficial because it became such an easy
base to start from, with an easy installation,
easy interface and easy software centre.
Mint
www.linuxmint.com
Based on Ubuntu, Mint
took the crown of the most
popular distro after
Ubuntu moved to a more
modern, touch-like desktop, whereas most
Linux users wanted a traditional keyboard/
mouse design, with a program menu and
desktop icons. Mint offers the simplicity of
installation, ease of use and the large software
base of Ubuntu, but with optimised versions
for older, slower computers, and a more fancy
version thats known as Cinnamon.
Red Hat
www.redhat.com
Its highly unlikely youll
come across Red Hat as
its the big-business distro
used by enterprises and corporations. It is,
however, worth mentioning because it funds
a couple of high-quality distros that can be
freely used. Red Hat is a billion-dollar business
and is, unusually, a paid-for Linux distro, but
there are ways of effectively getting the same
technology with a freely available distro.
Fedora
http://getfedora.com
The Fedora project was
created by Red Hat as a test
platform for cutting-edge
Linux technology. Features are tested in Fedora
and, when theyre deemed stable enough, they
are merged into the Red Hat distribution. This
isnt as scary as it sounds, as Fedora only gets
these features when theyre stable. Its an
excellent way of testing the latest technologies
because Fedora tends to get them before most
other distros.
OpenSUSE
www.opensuse.org
Another businessorientated distro that
also has a long-standing pedigree. It might not
be the sexiest distro on the block but its widely
used and often ranked in the top five distros, as
its super-stable and can be put to many uses,
from standard desktop use to server. This is
largely because its another corporatesponsored project and is much admired by
developers and software vendors.
Mageia
www.mageia.org
Showing you how fluid the
Linux distro world is, Mageia was
created off the back of a long-
Arch
www.archlinux.org
A super-advanced version of
Linux that you almost have
to build from scratch. This
means you need to be an expert to have any
luck with it, but it also means you get an
awesome OS thats exactly what you want. A
big advantage is that Archs active community
of experts delivers the latest builds of software
before anyone else gets them.
Manjaro
https://manjaro.github.io/
Arch Linux but without all the
complexity. The Manjaro
community has taken the base
of Arch and created a pre-built Linux distro that
is constantly updated, also called a rollingrelease distro. While it does lean to the more
technical side of distro releases, the developers
have gone out of their way to try to make it
accessible to all levels of users.
SteamOS
http://store.
steampowered.
com/steamos
Heres more of
an example of how Linux is used in a fully
commercial way. SteamOS is created by Valve
Software, the company behind the PC digital
distribution system called Steam. SteamOS is
based on Debian and is a custom OS that
drives a front for the Steam Big Picture mode,
integrating controllers, home streaming and
the store into one experience that you can
enjoy from a box under your TV. Q
WorldMags.net
WorldMags.net
Subscribe to
Choose your
Print 63
For 12 months
Every issue
comes with
a 4GB DVD
packed full
of the hottest
distros, apps,
games and a
lot more.
package
Digital 45
For 12 months
The cheapest
way to get Linux
Format. Instant
access on your
iPad, iPhone
and Android
device.
&
On iOroSid!
And
Bundle 77
For 12 months
WorldMags.net
SAVE
48%
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
Mint comes
bundled with
some pretty
wallpapers, which
you peruse by
right-clicking
the desktop
and choosing
Change Desktop
Background.
WorldMags.net
Get WorldMags.net
the UKs best-selling
Linux magazine
OUT
NOW!
WorldMags.net
WorldMags.net
WorldMags.net
Get with the program
T
The IDLE development environment is purpose-built for Python. You can install it on
Ubuntu (or in this case, ElementaryOS) with apt-get install idle .
WorldMags.net
WorldMags.net
>>> 3/2
>>> 1
>>> 3/2.
>>> 1.5
Funny the difference a dot can make.
Note that we have been lazy here in typing
simply 2. when we mean 2.0 . Python is
all about brevity. (Besides, why make life
hard for yourself?) Sooner or later, youll
run into rounding errors if you do enough
calculations with floats. Check out the
following doozy:
>>> 0.2 * 3
0.6000000000000001
Such quirks arise when fractions have a
non-terminating binary decimal expansion.
Sometimes, these are of no consequence,
but its worth being aware of them. They can
be worked around either by coercing floating
point variables to ints, or using the round()
function, which will give you only the number
of decimal places you require. Well see this
in practice when we program our Gomoku
game later.
Going loopy
Often, programmers desire to do almost the
same thing many times over. It could be
appending entries to a list, adding up totals
for each row in a table, or even subtracting
energy from all enemies just smitten by a
laser strike. Iterating over each list item,
table row or enemy manually would be
repetitive and make for lengthy, hard-to-read
code. For this reason, we have loops, like the
humble for loop below. When you hit Enter
after the first line, the prompt changes to
because the interpreter knows that a
discrete codeblock is coming and the line(s)
following the for construct belong to it.
Such codeblocks need to be indented,
usually using four spaces, though you can
use as many as you like so long as youre
consistent. If you dont indent the second
line, Python shouts at you. Entering a blank
line after the print statement ends the
codeblock and causes our loop to run.
There are all kinds of Pygame-powered games. This one, You Only Get One Match, features
lots of fireworks but limited means of ignition. Check it out at http://bit.ly/LXF202-onematch.
WorldMags.net
WorldMags.net
list, our while loop keeps going over its code
block until a condition ceases to hold. In the
following example, the condition is that the
user claims to have been born after 1900 and
before 2016.
>>> year = 0
>>> while year < 1900 or year >= 2015:
year = int(year)
Again the loop is indented, and again youll
need to input a blank line to set it running.
Weve used the less than ( < ) and greater than
or equal to ( >= ) operators to compare values.
Conditions can be combined with the logical
operators and , or and not . So long as year
has an unsuitable value, we keep asking. It is
initialised to 0, which is certainly less than
1900, so we are guaranteed to enter the loop.
Weve used the input() function, which returns
whatever string the user provides. This we
store in the variable year , which we convert to
an integer so that the comparisons in the
while line do not fail. It always pays to be as
prudent as possible as far as user input is
concerned: a malicious user could craft some
weird input that causes breakage, which while
not a big deal here, is bad news if its done, say,
on a web application that talks to a sensitive
database. You could change 1900 if you feel
anyone older than 115 might use your program.
Likewise, change 2015 if you want to keep out
(honest) youngsters.
WorldMags.net
WorldMags.net
Finer points
From here onwards, well refer to the actual
code, so any snippets we quote wont work in
isolation theyre just there to highlight things.
Youll notice that the FONT variable isnt
defined with the other constants; this is
because we cant use Pygames font support
until after the Pygames init() method has
been called. Lets look at the main game loop
right at the end of the code. The introductory
clause while True: suggests that this loop will
go on for ever. This is largely correct we want
to keep checking for events, namely mouse
clicks or the user clicking the exit button, until
the game is done. Obviously, we exit the loop
when the application quits clicking the
button triggers a QUIT event, which we react
to with the exit() function from the sys
package. Inside the main loop, the first thing
we do is call the updatePlayer() function,
which youll find on line 32. This updates the
text at the top of the screen that says whose go
it is, drawing (blitting) first a solid rectangle so
any previous text is erased.
Next we loop over the events in the events
queue; when the player tries to make a move,
the tryCounterPlace() function is called, with
the mouse coordinates passed along. To keep
WorldMags.net
WorldMags.net
Languages: An overview
O
ne of technologys greatest
achievements was IBMs Fortran
compiler back in the 1950s. It
allowed computers to be programmed using
something a bit less awkward than machine
code. Fortran is still widely used today and,
while some scoff at this dinosaur, it remains
highly relevant, particularly for scientific
computing. That said, nobody is going to
start learning it out of choice, and there are
all manner of other languages out there.
Traditionally, you have had the choice
between hard and fast languages such as
Java, C and C++ or easy and slower ones,
such as Python or PHP. The fast languages
tend to be the compiled ones, where the code
has to be compiled to machine code before it
can be run. Dynamic languages are converted
to machine code on the fly. However, on some
level, all programming languages are the same
there are some basic constructs such as
loops, conditionals and functions, and what
makes a programming language is simply how
it dresses these up.
For those just starting
coding, its simply baffling.
Opinions are polarised on what
is the best language to learn
first of all, but the truth is that
there isnt one, though for very
small people we heartily recommend Scratch.
Any language you try will by turns impress and
infuriate you. That said, we probably wouldnt
recommend C or Haskell for beginners.
There is a lot of popular opinion that favours
Python, which we happily endorse, but many
are put off by the Python 2 versus 3
fragmentation. Python has a lot going for it: its
probably one of the most human-readable
languages out there. For example, you should,
for readability purposes, use indentation in
your code, but in Python its mandatory. By
forcing this issue, Python can do away with the
Beginner-friendly
Other languages suitable for beginners are
JavaScript and PHP. The popularity of these
comes largely from their use on the web.
JavaScript works client-side (all the work is
done by the web browser), whereas PHP is
server-side. So if youre interested in
programming for the web, either of these
languages will serve you well. Youll also want to
learn some basic HTML and probably CSS, too,
so you can make your programs output look
nice, but this is surprisingly easy to pick up as
you go along. PHP is cosmetically a little
messier than Python, but soon (as in The
Matrix) youll see right through the brackets
WorldMags.net
WorldMags.net
Coding in the classroom
I
Learning opportunities
Criticism and mockery aside, were
genuinely thrilled that children as young as
five are, even as we speak, learning the dark
arts of syntax, semantics and symbolism.
Fear now, ye parents, when
your progeny hollers at you
(thats what kids do,
apparently), Whats the
plural of mongoose? and
then follows through seeking
clarification on the finer
points of recursion and abstraction. Rightly
or wrongly, many private firms will benefit
from the concerned parents and confused
kids resulting from the new computing
curriculum. But there are more wholesome
directions in which one can seek help.
For one thing, youll always find great
tutorials monthly in magazines such as
Linux Format https://linuxformat.com.
There are also many free resources on the
web. Some of them (such as the official
Python documentation) are a little dry for
kids, but wed encourage adults to learn
these skills alongside their offspring.
Refurbishing an old machine with a clean
Linux install will provide a great platform to
WorldMags.net
WorldMags.net
of the device and the Pi has a great deal of
potential but there exists some scepticism
over whether anything like the excitement
generated by the 1982 launch of BBC Micro will
be seen again. Back then, computers were new
and exciting, while these days kids expect them
to provide a constant barrage of entertainment
in the form of six-second cat videos or
140-character social commentary. Not all
of them are going to be thrilled at having to
program them. But anything that lowers, if not
entirely obliterates, any entry barrier to getting
into coding is fine by us. We also look forward
to neer-do-well students hacking each others
Micro:bits or engaging them in a collaborative
DDOS attack on their schools infrastructure.
Code Clubs
There are also over 2,000 volunteer-run code
clubs across the UK. Code Club, armed with
100,000 courtesy of Google, provides the
material, and schools (or other kind venues)
provide space and resources for this noble
extracurricular activity.
The Code Club syllabus is aimed at 9- to
11-year-olds and consists of two terms of
Scratch programming, then a term of web
design (HTML and CSS), concluding with a final
term of grown-up Python coding. Projects
are carefully designed to keep kids interested:
theyll be catching ghosts and racing boats in
Scratch, and doing the funky Turtle and keeping
tabs on the latest Pokmon creatures in
Python, to name but a few.
If you have the time and some knowledge,
its well worth volunteering as an instructor.
All the tutorials are prepared for you and if you
can persuade a local primary school to host
you, youll get a teacher to maintain order
and provide defence against any potential
pranksters. You will require a background
check, though. Code Club also provides three
specialist modules for teachers whose duty it is
to teach the new Computing curriculum.
This kind of community thinking is very
much in the spirit of open source, providing
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
Get started
with IDEs
Before you start, you need to install Geany from Mints repositories.
WorldMags.net
WorldMags.net
The Geany interface
Run button
The Run button (or F5) will
save and execute your Python
program in a terminal. When the
program finishes or breaks, you
can study any output or error
messages here before closing it.
Tabs
By default, Geany opens your
recent files in tabs. So if youre
working on a more involved,
multi-file project, related code is
just one click away.
Symbols
The Symbols tab lists all the
functions, variables and
imports in the current file,
together with the line on
which they are declared.
Clicking will take you
straight there.
Code folding
Code editor
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
Coding basics
Now you have the tools, its time
to grasp the basics of coding
Lists
Functions and objects
Conditionals
Variables
Program structure
Recursion
Sorting algorithms
Integers
Using loops
Compilers
Common mistakes
32
34
36
38
40
44
46
48
50
52
54
......................................................................................................................
........................................
..................................................................................
.................................................................................................
.....................................................
............................................................................................
.....................................................
.....................................................................................................
......................................................................................
............................................................................................
...................................................
WorldMags.net
WorldMags.net
If youre
learning
Python, the
tab completion
in the Eric
programming
environment is
a great helper.
POP
With a queue,
the values you
put on the list
first are the
first out (FIFO).
PUSH
A QUEUE
WorldMags.net
WorldMags.net
This will add soup to our shopping list. When weve added
it to our shop, it can be removed easily with pop:
>>> shoppinglist.pop()
If youre running these commands from the interpreter,
youll see the contents of the last value in the list as its
popped off the stack, in our case the word soup. If youve got
processing limitations in mind, you might wonder why there
isnt an option to pop the first value off the stack, too.
This would require the same amount of relinking as
popping off a value from the end. Its perfectly possible, and
turns a list into something called a queue the values you
put on the list first are first out, known as FIFO. This is in
contrast to a stack, which is LIFO.
There are many ways to use a queue, but the most
obvious is to preserve the order of incoming data. As we
explained, the only difference between a queue and a stack is
where the pop value comes from, which is why Python
doesnt have any special commands for accessing the first
value, it just uses an argument for the pop command. The
following will remove the first item in the list, and should
output milk from the interpreter:
>>> shoppinglist.pop(0)
Modern lists
Lists are no longer limited by processor speed to stacks and
queues, and most modern languages and frameworks
provide a vast number of handy functions for dealing with
them. These offer a big advantage over the original primitive
operations, and can turn lists into super-flexible data types.
Python is particularly good at this, and includes plenty of
built-in functions for manipulating lists without having to write
your own code. You can output a value from any point in the
list by treating it as an array. Typing shoppinglist[1], for
instance, will return the second value in the list. As with nearly
A STACK
POP
PUSH
4
3
2
1
With a stack, the values you put
on the list last are the first out (LIFO).
WorldMags.net
Quick
tip
Were using Python
for our examples
because its
a great language
for beginners. But
every concept we
discuss works with
other languages,
too its just a
matter of finding
the correct syntax
for the functionality.
WorldMags.net
Understanding
functions & objects
After introducing the concept of lists, its time for us to tackle the
fundamental building blocks behind any application.
eve already covered a concept called a list. Lists
have been around since people started to write
one line of code after another because, in essence,
they simply store one value after another.
But what must have come soon after lists in the mists of
language creation is the idea of a function, because a function
is a kind of list for code logic. Its not built at runtime, but while
youre constructing your project, and much like their
mathematical counterparts, functions are independent
blocks of code that are designed to be reused.
Functions are fundamental for all kinds of reasons. They
allow the programmer to break a complex task into smaller
chunks of code, for instance, and they allow you to write
a single piece of code that can be reused within a project. You
can then easily modify this code to make it more efficient, to
add functionality or to fix an error. And when its one piece of
code thats being used in lots of places, fixing it once is far
easier than trawling through your entire project and fixing
your same broken logic many times.
Functions also make program flow more logical and easier
to read, and you can keep your favourite functions and use
them again and again. When you break out of single-file
projects, functions become the best way of grouping
functionality and splitting your project into different source
files, naturally breaking complex ideas into a group of far
easier ones. If you then work with a team of programmers,
they can work on functions and files without breaking the
operation of the application, and its this idea that led to the
creation of objects. These are a special kind of function that
bundle both the code logic and the data the logic needs to
work into completely encapsulated blocks. But lets start at
the beginning. If your first programming project was from
about 30 years ago, and written on a home computer, then
BASIC wasnt
always put to
the best uses.
WorldMags.net
WorldMags.net
>>> def helloworld():
... print (Hello World)
... return
...
As ever with Python, you need to be very careful about
formatting. After creating a new function called helloworld()
with the def keyword, the interpreter precedes each new line
with .... Any lines of code you now add will be applied to the
function, and youll need to press [Tab] at the beginning of
each to indent the code and make sure Python understands
this. Other languages arent so picky. After the return
statement, which signals the end of the function, press
[Return] to create a blank line. Youve now created a function,
and you can execute it by typing helloworld() into the
interpreter. You should see the output youd expect from a
function with a name like helloworld(), and youve just
jumped forward 30 years.
Passing arguments
Theres a problem with our new function: its static. You cant
change anything. This can be solved by opening the function
up to external influence, and you can do that by enabling it to
pass and process arguments. If you alter the function
definition to def helloworld( str ), for instance, Python will
expect to find a string passed to the function whenever its
called. You can process this string within the function by
changing print (Hello World) to print str, but you should
obviously be doing anything other than simply printing out
whats passed to the function. It can now be executed by
typing helloworld(something to output) and youll see
the contents of the quotes output to the screen.
To show how easy it is to create something functional, well
now build on the idea of lists from the previous tutorial.
Instead of a shopping list, well create a list of prices and then
write a function for adding them together and returning the
total value. Heres the code:
>>> def addprices( plist ):
... return (sum (plist))
...
>>> pricelist = [1.49, 2.30, 4.99, 6.50, 0.99]
>>> addprices(pricelist)
16.27
Python is very forgiving when it comes to passing types to
functions, which is why we didnt need to define exactly what
plist contained. Most other languages will want to know
exactly what kind of types an argument may contain before
its able to create the function, and thats why you often get
definitions before the program logic, and why types are often
declared in header files because these are read before the
main source code file.
Working out what your language requires and where is
more difficult than working out how to use functions. And
thats why they can sometimes appear intimidating. But
Python is perfect for beginners because it doesnt have any
convoluted requirements apart from its weird tab
formatting. All weve done in our code is say theres going to
be some data passed as an argument to this function, and
wed like Python to call this plist. It could even have had the
same name as the original list but that might look confusing.
The only slight problem with this solution is that youll get
an error if you try to pass anything other than a list of
numbers to the function. This is something that should be
checked by the function, unless you can be certain where
those numbers come from. But it does mean you can send
a list of values in exactly the same way you define a list:
>>> addprices([10, 12, 13, 18])
53
The biggest challenge comes from splitting larger
problems into a group of smaller ones. For a programmer, this
task becomes second nature, but for a beginner, its an
intimidating prospect. The best way is to first write your
application, regardless of functions or programming finesse.
Often, its only when youre attempting to write your first
solution that better ones will occur to you, and youll find
yourself splitting up ideas as the best way to tackle a specific
issue within your project. The best example is a GUI, where its
common sense to give each menu item its own function,
which can be launched from either the icons in the toolbar or
by selecting the menu item. Many developers consider their
first working copy to be a draft of the final code, and go to the
trouble of completely rewriting the code after proving the idea
works. But dont worry about that yet. Q
WorldMags.net
Quick
tip
When youre using
other packages,
only the functions
are exposed. You
dont need to know
how they work, just
what input theyre
expecting and what
output they return.
Thats functional
programming in
a nutshell.
WorldMags.net
IF condition...
THEN
ELSE
Consequent
action
Alternative
action
print X is ten
somefunction()
else:
if x == 10:
print X is ten
else:
print X is NOT ten
print Program finished
Here, we create a new variable (storage place for
a number) called X, and store the number 5 in it. We then use
an if statement a conditional to make a decision. If X
contains 10, we print an affirmative message, and if not (the
else statement), we print a different message. Note the
double-equals in the if line: its very important, and well come
on to that in a moment.
Here, were just executing single print commands for the
if and else sections, but you can put more lines of code in
there, providing they have the indents, Python style:
if x == 10:
anotherfunction()
In this case, if X contains 10 we print the message as
before, but then call the somefunction routine elsewhere in
the code. That could be a big function that calls other
functions and so forth, thereby turning this into a long branch
in the code.
There are alternatives to the double-equals weve used:
if x > 10 If X is greater than 10
if x < 10 If X is less than 10
if x >= 10 If X is greater than or equal to 10
if x <= 10 If X is less than or equal to 10
if x != 10 If X is NOT equal to 10
These comparison operators are standard across most
programming languages. You can often perform arithmetic
inside the conditional statement, too:
if x + 7 == 10:
print Well, X must be 3
def falsefunc():
return 0
print Execution begins here...
if truefunc():
WorldMags.net
WorldMags.net
print Yay
if falsefunc():
print Nay
The first four lines of code define functions (subroutines)
that arent executed immediately, but are reserved for later
use. Theyre really simple functions: the first sends back the
number 1, the second zero. Program execution begins at the
print line, and then we have our first if statement. Instead of
doing a comparison, we call a function here, and act on the
result. If the if statement sees the number 1, it goes ahead
with executing the indented code, if not, it skips past it. So
when you run this, youll see Yay but not Nay, because if here
only does its work if it receives the number 1 back from the
function it calls. In Python and many other languages, you can
replace 1 and 0 with True and False for code clarity, so you
could replace the functions at the start with:
def truefunc():
return True
def falsefunc():
return False
and the program will operate in the same way.
Clearing up code
In more complicated programs, a long stream of ifs and elses
can get ugly. For instance, consider this C program:
#include <stdio.h>
int main()
{
int x = 2;
if (x == 1)
puts(One);
else if (x == 2)
puts(Two);
else if (x == 3)
puts(Three);
}
C, and some other languages, include a switch statement
which simplifies all these checks. The lines beginning with if
here can be replaced with:
switch(x) {
case 1: puts(One); break;
case 2: puts(Two); break;
case 3: puts(Three); break;
}
Assignment vs comparison
In some languages, especially C, you
have to be very careful with
comparisons. For instance, look at this
bit of C code try to guess what it does,
and if you like, type it into a file called
foo.c, run gcc foo.c to compile it and
then ./a.out to execute it.
#include <stdio.h>
int main()
{
int x = 1;
if (x = 5)
puts(X is five!);
}
If you run this program, you might be
surprised to see the X is five message
appear in your terminal window. Shurley
shome mishtake? We clearly set the
WorldMags.net
WorldMags.net
Variable scope of
various variables
Caution! That variable youre accessing might not be what you think it is. Why?
Because variables are variable. Have we said variable enough now?
Most welldocumented
languages have
explanations
of variable
scope, so once
youve got the
basics from this
article, you can
explore specific
implementations.
and therefore its a good thing to get right in the early days of
your programming journey.
Lets start with a bit of Python code. Try this:
def myfunc():
x = 10
print x
x=1
print x
myfunc()
print x
If youre totally new to Python: the def bit and the
following two lines of code are a function which we call later.
Program execution begins with the x = 1 line. So, we create a
variable called x and assign it the number 1. We print it out to
confirm that. We then call a function which sets x to 10, and
prints it. As control jumps back to the main chunk of our
code, we print x again and its back to 1. How did that
happen? Didnt we just set x to 10 in the function?
Well, yes, but the function had its own copy of the variable.
It didnt do anything with the x variable that was declared
outside of it. The function lives happily on its own, and doesnt
want to interfere with data it doesnt know about. This is
essential to keeping code maintainable imagine if all
variables were accessible everywhere, and you were writing a
routine to be dropped inside a 50,000-line software project.
Youd be totally terrified of choosing variable names that
might be in use elsewhere, and accidentally changing
someone elses data.
Change of routine
So most programming languages provide this level of
protection. Still, there are legitimate reasons why you might
want to make a variable accessible to other routines, and in
Python you can do this by inserting the following line into the
start of the myfunc() routine:
global x
This makes all the difference. Previously, the version of x
we were using in myfunc() was a local variable that is, it
affects only the code located inside the function. By adding
the global command, we access x as a global variable that
is, its the same one as we used in the main body of the code.
So if we run this Python code now, we see that x is set to 1 at
the start, but then the myfunc() routine grabs that x as a
global variable and sets it to 10, and it stays that way back in
the main code.
How variable scope is handled varies from language to
language, so lets look at another implementation here, this
WorldMags.net
WorldMags.net
time in C. Whereas Python is pretty flexible about using
variables that you havent defined previously, C is a little more
strict. Consider this:
#include <stdio.h>
void myfunc();
int main()
{
int x = 10;
printf(x is %d\n, x);
myfunc();
printf(x is %d\n, x);
}
void myfunc()
{
int x = 20;
printf(x is %d\n, x);
}
Here, because were explicitly creating variables with int,
its somewhat clearer that myfunc() has its own version of
the variable. But how do you go about making the variable
global in this particular instance? The trick is to put the
variable declaration outside of the functions. Move int x = 10;
from inside main() to after the void myfunc(); line at the top,
and now the variable has become global that is, its
accessible by all functions in the file.
However, thats not the only job you need to do. Inside the
myfunc() routine, we still have a line that says int x = 20;.
The int here is still creating its own version of the variable, so
take it away so that the line just reads x = 20;. Run the
program, and youll see the intended result, that x is global
and can be modified everywhere.
Note that while putting the x declaration outside of
functions makes it global to the current source code file, it
doesnt make it global absolutely everywhere in all of the
source code. If youve got a big project with multiple C files,
youll have to use the extern keyword. So if you have int x =
10; in foo.c and you want to use that in bar.c, youd need the
line extern int x; in the latter file.
All of this leads to one final question: when should you use
global variables, and when local? Ultimately, thats a matter of
Automatic variables
Most local variables are automatic
that is, they are only created in RAM
when program execution reaches the
point of their initialisation. Once the
routine containing the local variable has
finished, that variable will no
longer be useful, because outside
routines cant access it. So, the compiler
will typically reclaim memory used by
that variable, making room for other bits
and bobs.
There is, however, a way to stop that,
and thats by declaring a static variable.
This is still a local variable that can be
manipulated only by code inside the
current function, but it retains its state
every time the function is called. For
instance, look at the following C code:
#include <stdio.h>
void myfunc();
int main()
{
myfunc();
myfunc();
myfunc();
}
void myfunc()
{
int x = 0;
x = x + 1;
printf(x is %d\n, x);
}
Here, we call myfunc() three times,
which creates a variable x containing
zero, adds 1 to it and prints it out. Youve
no doubt already guessed that running
this produces 1 with each call of
myfunc(), because x is being created
each time. But what happens if you
change the declaration in myfunc() to
static int x = 0;? Well, the output
becomes this:
x is 1
x is 2
x is 3
The static keyword here tells the
compiler that it should preserve the
state of the variable between calls. So
on the very first call it sets up x as zero,
but in future calls it doesnt initialise the
variable as new again, but retrieves its
state from before. The compiler doesnt
ditch it at the end of the function, as it
does with automatic variables. So, using
static variables is
a great way to get some of the benefits
of global variables (retaining its state
throughout execution) without exposing
it to the rest of the program.
Remember to initialise!
By default, most C compilers dont
set newly-created automatic
variables to zero. This is especially
true in the case of automatic
variables, because they might be
created somewhere in RAM that was
used previously by other data.
So if you have a program like this:
#include <stdio.h>
int main()
{
int x;
printf(x is %d\n, x);
}
then compile and run it, and then
compile and run it again, the number
will probably be different each time
you do. The moral of the story is:
WorldMags.net
WorldMags.net
Building proper
programs
Learn to break problems down into manageable chunks, as we show you how
to design a well-sculpted program.
n this article, were going to do something a little different.
Instead of looking at a specific feature that shows up in
many programming languages, were going to look at the
art of designing programs.
Knowing about if clauses, for loops, lists and functions
(and much more) is vital if you want to learn to program. But
you can get to a point where you know all of the elements, but
you dont have the faintest clue where to get started when
faced with a new programming challenge.
There are, however, some set processes recipes of
a kind that you can follow that will help to direct your
thinking and make solving a problem much easier.
Some of the Python features we use to solve problems
might be unfamiliar, but thats not really the important thing
in this tutorial (you can always look those up on the internet
yourself) the important thing is the thought process that we
must go through.
To demonstrate, we need an example problem, and weve
settled on a simple mortgage calculator. Lets say that Mike
needs a mortgage of 150,000 to buy a nice house in
Innsbruck. A bank has agreed to loan him the money at a rate
of 6% per annum, with Mike paying it back over 25 years at a
fixed monthly rate. Another bank agrees to lend him the
money, but at 4% per annum, paying it back over 30 years.
Mike wants to know what his monthly repayments will be in
each situation, and how much interest hell have paid in the
end. He wants us to help him. The first step when tackling a
programming problem is to closely examine the specification.
Read through the above text carefully and pick out all the
types of data that are described. As youre doing this,
WorldMags.net
WorldMags.net
information about how to calculate mortgage repayments, a
quick Google search turned up this handy and entirely
relevant formula on Wikipedia:
c = rP / (1 - (1 + r)^N)
Here, c is the monthly payment, r the monthly interest as
a decimal, P the amount borrowed and N the number of
monthly payments. Notice how all the variables needed to
calculate c in this formula are already provided to our
function as arguments?
Mike wants to live here, and he needs us to figure out whether he can afford it!
(From Wikipedias Innsbruck page.)
Designing programs
So, at the end of all this, what did our whole development
process look like?
We began by critically reading the problem specification,
and identifying the input and output data involved. By looking
closely at this information, we managed to find
a way to split the problem in two, making it easier and more
manageable. We called this top-down development. We then
developed solutions to each of these as separate functions,
beginning with sketches and testing each as we progressed.
This was called incremental development.
Also remember that, where we had to, we looked up
specific knowledge
which allowed us to
solve the problem.
Finally, with
working solutions
to all of the
subproblems, we
combined them to
come up with a solution to the original, larger problem.
The steps are simple, but this is exactly what you would do
to tackle almost any programming problem. For some, you
might have to further divide the subproblems, but you would
just keep going until you reached a level that was easily
solvable and then work your way back, solving each level as
and when you reached it. Q
WorldMags.net
E
R T T
O A N M
M RE E O
G NT3.C
O
C T
AT
WorldMags.net
WorldMags.net
Not your average technology website
www.gizmodo.co.uk
twitter.com/GizmodoUK
facebook.com/GizmodoUK
WorldMags.net
WorldMags.net
Recursion: round
and round we go
Jump down the ultimate rabbit hole of programming ideas, but be careful
you may well land on your own head.
ecursion is one of those concepts that sounds far
more intimidating than it really is. Honestly. To make
use of it, you dont need to grasp complicated
mathematical ideas; or start thinking in four dimensions; or
invade one level after another of someones dreams to
implant an idea into their subconscious; or even understand
the meaning of recursive acronyms, such as GNU (GNUs
not Unix).
For the most part, many uses of recursion are well
documented and explored. You can then just copy and paste
them into your code without necessarily understanding how
they work, or why they do what they do. But if you do this,
youre missing out, because recursion is cool. Its one of the
best ways of letting the CPU do all the hard work while you
write a few lines of code; and as a result, it can save you from
bending your brain around what might be a more complex
problem without recursion.
This is because recursion is simply a way of solving a more
complex problem by repeating a simple function. The trick is
that this function repeats from within itself. Rather than being
run manually from another function, it calls another instance
of itself from within its own code. Its the programming
equivalent of pointing two mirrors together to create a
seemingly infinite corridor, or connecting to the same virtual
desktop from within a virtual desktop.
X factorial
In programming, one of the classic, and easiest, examples of
recursion is a function to give a numbers factorial. The
factorial of a positive integer (we have to say this because the
function would change otherwise) is the product achieved by
multiplying the whole numbers it contains against one
another. The factorial of 4, for example, is 4x3x2x1, which
equals 24.
If we wanted to approach this problem in the same way
24
To calculate the factorial of a number, we can use a function that calls itself
and returns the correct answer. Thats recursion!
WorldMags.net
WorldMags.net
the return keyword, so that the code that called the function
can use the value. You can see that while its harder to
understand the logic of how the final value is calculated,
theres a lot less code than with the iterative approach,
making this solution more efficient and less prone to errors.
This is true of the majority of recursive solutions.
As long as the incoming value is greater than 1, factorial
calls itself with a new value thats one less than the one it was
called with, and multiplies this by the value thats returned. If
you call factorial(4), for example, factorial() will be executed
from within itself with the values 3, 2 and then 1. When it hits
1, the return values start to trickle back because factorial() is
no longer being called recursively. 1 is returned and multiplied
by 2, which itself returns (2x1) to the previous call, which
returns (3x2x1) which, in turn, returns (4x3x2x1). If you want
to see this as Python code so you can try it out, type the
following into the Python interpreter:
>>> def factorial(c):
... if c > 1:
... return c * factorial(c-1)
... else:
... return 1
...
>>> factorial(4)
24
>>> factorial(5)
120
>>>
The last two lines in that example are simply executing the
factorial() function with the value we want to calculate. When
the function finishes and returns the final calculation, the
Python interpreter prints this out without us needing to do
anything else, which is why you see the results under this line.
Congratulations youve solved the problem using recursion!
You should now be able to see how recursion can be used in
all sorts of places that require an indeterminate amount of
repetition, such as many sorting algorithms, other
mathematical solutions and filesystem searches. Even if you
cant think of a solution yourself, if you have an idea that
something can be approached using recursion, then a simple
Google search will reveal whether it can and what the code, or
pseudo code, should look like.
Generating fractals
But if you want to visualise recursion in action, which is one of
the quickest ways to understand the concept, theres one
particularly effective set of functions that can be used to
illustrate both its advantages and its complexities, and these
are fractals. Fractal algorithms use recursion to add infinite
levels of detail to whats a much simpler algorithm. Some of
the most famous were documented by the French
mathematician Benot B Mandelbrot in his book, The Fractal
Geometry of Nature, but there are many easier algorithms
that can be created with just a few lines of code, which is what
were going to do using Python.
Before we start, though, you will need to make sure you
have two dependencies installed. The first is Pythons Turtle
graphics module, but it should be part of any default Python
installation. We use this to draw and display lines with as little
code as possible. The Turtle module itself needs you to install
tk-8.5 for its drawing routines, which means if your
distribution doesnt add this automatically, youll have to
install it yourself. Using the Turtle graphics module means we
We generate this fractal with just 16 lines of code. But its complexity is
potentially infinite.
can also see exactly what our script is doing as its doing it,
because the cursor moves slowly along the path as our fractal
is being drawn.
The fractal were going to create is called a star fractal.
This is a five-pointed star, with another star drawn on to the
end of each of its limbs, and a further one on each of those
limbs, and so on, and on. But to start with, were going to
create a function to draw a star, then expand this to draw
another star at the correct point. Drawing with Turtle graphics
is perfect for fractals because it doesnt draw lines with
co-ordinates, but instead uses the relative position of
a cursor and an angle to draw the path with the cursor as it
moves forward.
import turtle as trtl
def fractal(length=100):
if length < 10:
return
trtl.fd(length)
trtl.left(144)
trtl.fd(length)
trtl.left(144)
trtl.fd(length)
trtl.left(144)
trtl.fd(length)
trtl.left(144)
trtl.fd(length)
trtl.left(144)
return
Quick
tip
If you want your
Python application
to pause before
quitting, or doing
anything else, add
from time import
sleep to the top of
your script and use
the sleep (seconds)
command within
your code. This is
very useful if you
want to see the
final Turtle graphic
rendering before the
window closes.
WorldMags.net
WorldMags.net
Super sorting
algorithms
Lets take our coding further and introduce you to the unruly world of
algorithms, efficiency and sorted data.
ython is a great language. Youre happily writing a little
program, and youve got a huge list that you need to
put in order. What do you do? Easy, just call the sort()
method on the list, and youre away. It works amazingly
quickly, and it doesnt even care how big the list is it could
be millions of elements long and still take virtually the same
amount of time to return the sorted list.
This is really handy, but have you ever stopped to wonder
how on earth it works? Its a fascinating question, and in the
next two articles were going to introduce you to some of the
techniques that are used to solve this kind of problem. Along
the way, youll also get a gentle introduction to algorithms and
how to think about their performance.
So, where do we start? Sorting a list with a computer, like
many programming problems, seems incredibly abstract and
difficult when you first begin. If you start off trying to think
about how to sort a Python list with a million items in it, you
wont get anywhere very quickly.
Well, its pretty simple: youd look at them, and if the card
on the left was worth more than the card on the right, youd
switch them around; if the card on the right was worth more,
youd leave them as they were.
What would that look like in Python?
cards = [8,2]
if card[0] > card[1]:
card[0] = card[1]
card[1] = card[0]
print cards
That seems pretty straightforward. Theres a list of cards,
with the values 8 and 2. We then check to see whether the
first card is more valuable than the second, and if it is, copy
each card to the correct location in the list.
Run that code and see what happens. You should find that
you dont get [2,8], but [2,2]. Thats obviously not what we
want, so what happened?
Because theres no operator in Python that enables us to
switch the position of two elements, we did what seems most
natural to us we used the assignment operator to copy the
value of the two cards. The problem is that after the first copy
statement, we ended up with both cards having the same
value. When we tried to do the second assignment, we just
copied two identical cards.
8 5
Bubble sort
works by
comparing
adjacent
elements in the
list. As it does
so, the largest
element bubbles
its way to the
end of the list.
8 2
WorldMags.net
9
9
WorldMags.net
The way to get around this is to store a copy of the first
cards value outside of the list, and before we do any copying.
Take a look at this code:
cards = [8,2]
card0 = cards[0]
if card[0] > card[1]:
card[0] = card[2]
card[1] = card0
print cards
If you run that, you should get the correct answer.
Because we stored a copy of the first cards value when we
overwrote it with the first assignment statement, we could
use the copy to set the second card to the correct value.
In the end, its not quite how we would do things in real life,
but its pretty close.
Bubble sort
OK, so sorting a list two elements long isnt particularly
impressive. But we can extend the same technique to sort a
list that has an infinite number of elements this is known as
bubble sort.
The idea is that we loop over the entire list, comparing
(and swapping, if necessary) each set of adjacent elements
in turn. The element with the greatest value will always end up
on the right-hand side of the comparison, so will always be
compared in the next pair of elements. Eventually, it will end
up at the very end of the list, where it belongs.
Notice that, if we were to loop over the list only once, wed
get only the largest element in the correct position. The way
to get around this is to keep looping over the list; on each
loop, well encounter another largest element that always
ends up on the right-hand side of each comparison that is,
until it reaches the largest element from the previous
iteration. At this point, it wont move any further, because its
now in the correct position.
Well know the list is sorted when we run a loop in which
nothing gets swapped. This code shows how a bubble sort
might be implemented in Python:
cards = [8, 3, 7, 4, 2, 1]
swapped = True
while swapped: #sort until swapped is False
swapped = False #assume nothing is swapped
for i in range(len(cards) - 1): #loop entire list
cur = cards[i]
j=i+1
if cards[i] > cards[j]:
cards[i] = cards[j]
cards[j] = cur
swapped = True #reset swapped to True if anything
is swapped
print cards
Speed matters
Its pretty clever, right? You can now use this program to sort
any size list and it will get the job done. However, if you try to
use it on a large list, youll find that its painfully slow even
on a fast computer.
To understand why this is, lets think a little about how
much work our bubble sort algorithm has to do. When we had
just two cards, bubble sort had to do two comparisons once
to get the cards in the correct place, and then again to check
that theres nothing else to sort. In total, it had to perform just
two operations.
Bubble sort
is an algorithm
with O(n2)
performance
that means
it takes
increasingly
longer for every
element added.
Big O
What this means is that the amount of work bubble sort does
increases in polynomial time for every item added to the list.
That is to say the increase in the amount of work becomes
more and more rapid for every element added to the list. This
is why its so slow for large lists a 10-element list may take
only 90 operations, but a 1,000-element list will take 999,000
operations, and it will simply get worse for every card from
there on.
In the general case given above, the n2 term is always
going to be far larger than the n term, and hence will always
have a much larger influence on how well the algorithm
performs. Because of this, when discussing the performance
of an algorithm, in computer science its only ever the largest
term thats considered.
The terminology used for this is big O notation, and we
say that bubble sort is a O(n2) algorithm. Bubble sort is
actually considered to be one of the worst-performing sorting
algorithms, and its definitely not how Python does it. That
said, dont discard it entirely. Its easy to implement, and
works all right for small lists on fast computers. There may be
times when you just want to get the job done, and bubble sort
makes the most sense here.
If you fancy looking at sorting algorithms that are much
faster, albeit harder to implement, jump to page 102, these
are much closer to how Python does it. Q
WorldMags.net
WorldMags.net
Hidden secrets
of numbers
Allow us to channel the living spirit of Dan Brown, and uncover the hidden
meaning behind 1, 2 and -1b1010101.
If you want
to see binary
registers update
in real time,
KCalc has a
handy display
mode for up to
64 bits of data.
2s compliment
Consider the following question: what does 10101010
represent in 2s compliment? You might think, after the last
couple of paragraphs, that the binary value represented here
is 170 (2+8+32+128). But youd be wrong. The clue is the
reference to 2s compliment. This is, in fact, a scheme for
representing negative values in binary, and it changes the
meaning of the bits and what they represent. It does this by
reversing the configuration of bits, so that 1 becomes 0, a
process known as flipping, and adding 1 (there is a single
exception to this).
So, the negative of 00000010 is first 11111101, then with 1
added 11111110, which is the 2s compliment representation
of -2. Using this logic with the above question, we first
subtract 1 from 10101010 to get 10101001 and flip the bits to
01010110, which gives a decimal value of 86, so the answer
to the question is -86. The reason this method was chosen
rather than swapping the most significant bit is because most
binary arithmetic can still be performed on these negative
values in exactly the same way theyre performed on positive
values. Adding 1 to 10101010 (-86), say, yields 10101011,
which is -85 the correct value.
While were in the binary zone, its worth mentioning
a couple of other numeral systems sometimes used when
programming. The first is octal, a base 8 system that uses the
numerals 0-7 to hold 8 values. 7+1, for example, is 10 in octal,
WorldMags.net
WorldMags.net
just as 9+1=10 in decimal. It takes exactly three binary bits to
represent a value in octal, which is why its often used when
you play with binary values. Similarly, hexadecimal, which is
base 16, uses the characters 0-9 and a-f to represent decimal
values 0-15, and thats the equivalent of four bits of data. So,
the number 6c in hex is 0110 (6) and 1100 (12 = c) in binary,
which as an 8-bit value equates to 108. Hex is a more
readable equivalent to binary, which is why its often used for
memory and binary editors, and for storing raw binary data
within your program.
You can avoid using these numeral systems in your code
and your projects wont suffer. But understanding a little
about what they mean and how they relate to one another
can help you when looking at other projects, and how the
underlying systems that take your code and make it run work.
If you want to play with numeral systems, you can do so using
Pythons interpreter, and this is a great way of getting familiar
with how the numbers work. From version 2.6 onwards, for
example, you can enter a binary value using the 0b prefix
thats a zero followed by the b. Typing a binary number will
output the decimal value:
>>> 0b01010110
86
You can convert values to binary using the bin function:
>>> bin(86)
0b1010110
>>> bin(-85)
-0b1010101
As you can see with the last example, this also works with
negative integers, returning a 2s compliment binary. If you
want to input a 2s compliment binary, you need to prefix the
data with -0b. You can do similar things with octal by
replacing the 0b with 0c and with hexadecimal by using 0x,
and both of these numeral types have supporting functions
for converting integers:
>>> hex(85)
0x55
>>> oct(85)
0o125
Data types
In many programming languages, you need to specify the
type of a variable before you can use it. This can cause
confusion in the beginner, because they wonder how theyre
supposed to know what they need to use before they write
the code. But, in reality, you write the declaration for each
variable as you come to use them.
The compiler or interpreter needs to know about type
because it wants to assign only enough memory and
processing to handle that number, and no more. If you know
your variable is never going to have a value greater than 255
(the value held by an 8-bit value), theres no need for the
compiler to ensure more than this amount of storage is
available. An 8-bit value has become known as a byte.
Although the number of bits in a byte used to vary, it has
settled on 8, because this is typically used to store a single
character. A bytes worth of data is still used to store
characters to this day, which you can view through ASCII
codes with any reasonable binary editor.
But computer hardware has moved on, through the
16/32-bit era of the Atari ST and Amiga, to the pure 32-bit
PCs of the last decade and to today, where most machines
have a processor capable of handling 64-bit instructions. For
BITWISE OPERATORS
NOT
1 1
= 1 1
OR
1 1 1
1 1 1
=1 1 1 1
AND
1 1 1
1 1 1
= 1 1
XOR
1 1 1
1 1 1
= 1 1
WorldMags.net
Binary and
bitwise operators
bring logic to
your programs.
Quick
tip
Understanding
how bits and bytes
hold together is
essential if you want
to read or write to
binary formats, or
create a utility that
communicates with
external hardware.
WorldMags.net
If you want to
build your own
C++ application,
add the source
code to a text file
and build it with
the command
g++ helloworld.
cpp -o helloworld.
Just run ./
helloworld to see
the results.
WorldMags.net
WorldMags.net
between compiled languages such as C/C++ and interpreted
languages such as Python and JavaScript), the output is
identical to our Python example from earlier. In the C++ code,
the variable i is declared as an integer (int) and incremented
(i++) one value at a time for as long as it remains less than 5
(<5). This is exactly what the Python loop is doing, and the
cout line is simply printing out the value of i during each
iteration. You should be able to see from this that both the
value of i and the value checked for by the condition can be
changed, so you could get the value of i to count down
instead by changing the loop to:
for ( int i=5 ; i > 0 ; i--){
cout << i << endl;
}
To do the same in Python, we need to take a closer
look at the range keyword used in the first example. Range
is a function in Python that takes a start point, a stop point
and a step size, much the same as the inputs for a C++ for
loop. Its also much more versatile, as you dont need to
restrict yourself to numbers range can use indices of
a sequence. If you put on one value in the range, as we
did in the original loop, the function counts from zero up to
the value -1. However, anything else will create a different
effect. The advantage of farming it out to a function is that
youre no longer restricted to using it in just the for loop, and
youll find many programmers use range for convenience
throughout their code. If you wanted to count down in Python,
for example, you could modify the for loop, as follows:
for i in range(5, 0, -1):
print (i)
Infinite loop
If you dont need the iteration of a for loop, the best
alternative is a while loop. This includes only the conditional
check, and youre free to make that check positive in any way
you choose. You could easily emulate the behaviour of a for
loop, for example, by making the while loop wait for a variable
to reach a certain value. Then, within your block of code,
make sure your calculations attain that value at some point.
In Python, we could recreate the previous for loop using
while, like this:
>>> i = 5
>>> while i > 0:
... i -= 1
... print (i)
...
4
3
2
1
0
Its simpler this way, and by using while you can make the
conditional checks for leaving a loop as complex as you need,
rather than adhering to those required by for. Another trick in
other languages, but not Python, is to place the conditional
checking at the end of the block of code. This is normally
called a do...while loop, because the do is used to start the
section and the while is used to close the section.
Most importantly, the while statement at the end of the
loop is checking the condition, such as do the washing up
while there are still some dirty plates. This can be helpful in
certain circumstances, such as when you want the loop to
take some input and then only test against that input at the
end of the test. In C++, this function would look like this:
char c;
do {
cin >> c;
} while (c != x);
This example is oversimplified, and isnt useful in itself, but
it illustrates one example where do...while works well, and
thats taking input. If you want to check the state of some
input, whether its from a file or from someone typing on the
keyboard, you need to have first grabbed the input. You could
do this in a normal while loop by asking for the input before
you enter the loop, but its slightly more efficient and elegant
if you use a do...while loop to encapsulate the entire function.
This is all the above loop is doing in C++. It grabs some input
from the keyboard using cin, and waits for that input to
consist only of the x character, after which it quits the loop.
Another common use for while is to create whats known as
an infinite loop simply a loop that never satisfies its exit
condition. This might sound like insanity if you ever want the
processor to skip over the remainder of your code, but there
are certain times, and languages, where the use of an infinite
loop is the best way of building your application. One example
is if you want to build a tool to monitor the state of a server or
some other service.
You also find infinite loops watching hardware inputs or
controlling the rendering within a game engine. Using an
infinite loop means you can be sure your application is
running, and hasnt exited under some other condition, and
using while is probably the easiest way of creating one. while
(true) {} is all thats needed. However, many languages also
provide an escape from loops like this, and thats using
something called the break command. break can be used to
escape from an infinite loop, as well as leaving other kinds of
loops early if you need to. You might want to escape from a
for loop quickly, for example if you have detected that the
user wants to quit the application. Using break, the execution
of your code continues after the loop section, and youd
normally use this section of code to tidy up files, processes
and memory before an exit. Of course, theres a good case for
arguing against infinite loops, and building into your code the
ability to escape on a specific condition, but this can take
more effort and might not be as efficient, especially for
smaller applications. Q
WorldMags.net
Python is still
a great language
to learn with
because it lets
you experiment
and see results
as soon as youve
typed your code.
Quick
tip
Nearly all languages
count zero as a
value, such as the
first position in an
array. This seems
illogical at first,
because were used
to thinking of zero
as nothing, but its
just something you
have to get used to.
WorldMags.net
The magic of
compilers
Try to read
a binary
executable file in
a text editor, and
youll just see
a load of messedup characters
like this.
Dissecting a program
First up, consider this simple C program:
#include <stdio.h>
int main()
{
puts(Hello, world!);
}
This simply prints the message Hello world! to the
screen. If youve never seen C before, you might be a bit
puzzled by some of
the text, so heres a
brief explanation:
the #include line
says that we want to
use standard input
and output routines
(stdio). Then main
says that this is the main part of our program (that is, where
execution should begin), and the puts command means put
a string.
Enter this text into a file called foo.c and then run the
following commands:
gcc foo.c
./a.out
The first command uses GCC to compile foo.c from the
source code into a binary executable file called a.out, and the
second line runs it. You can get more information about the
resulting file by running file a.out youll see output similar
to the following:
a.out: ELF 32-bit LSB executable, Intel 80386, version 1
(SYSV), dynamically linked (uses shared libs), for GNU/
Linux 2.6.15, not stripped
If you try to examine the file yourself, using less a.out for
instance, then youll just see gobbledygook, as in the
screenshot shown on the left. This is because we mere
mortals cant read binary, and so less is trying to convert it
into plain text characters.
This final, resulting file isnt intended for human
consumption, but we can peek into some of the stages in the
compilation process. First of all, enter this command:
WorldMags.net
WorldMags.net
gcc -E foo.c > foo.p
With the -E switch, we tell GCC that we dont want a binary
executable file just yet; instead, we want only to see what
happens after the first stage of processing. Open foo.p in a
text editor and youll see that its much longer than the
original program, because the contents of the stdio.h file
have been included in it. Its only right at the bottom that you
can see the main function and our puts instruction.
In larger, more complicated programs, the compiler does
much more work in this pre-processor stage. It expands
macros, handles #define statements and places additional
information into the file for debugging purposes. This file is
then complete, with all the information that GCC needs to
start converting it into machine code.
Assembly
But wait! Theres an intermediate step between the C code
and binary, and its called assembly language. Assembly is a
human-readable way of representing the instructions that the
CPU executes. Its much lower-level than typical languages
such as C and Python, where you can simply say print a
string for me. In assembly, you have to move memory around
into the video buffer for instance, telling the CPU exactly
which instructions it should execute. So, now enter this:
gcc -S foo.c
This produces a file called foo.s that contains the
assembly language version of our program. In other words,
its a list of CPU instructions written in a slightly more
readable form. Most of it will look like complete gibberish if
you have never touched assembly before, but these are the
two most important lines:
movl $.LC0, (%esp)
call puts
Essentially, this places the location of our Hello world!
text string into a register (a little like a variable), and then calls
the puts (put string) function from the C library. These two
lines are exact CPU instructions as programmers, there is
no way that we can break them down into smaller pieces.
Given that modern CPUs execute billions of instructions per
second, it can be quite humbling to see single instructions laid
bare like this.
In the normal process of compilation (that is, without the
-E or -S switches), GCC now runs an assembler to convert
those human-readable instructions into their binary
equivalents. It then adds extra information to the resulting file
(a.out) to make it a valid Linux executable, and we have a
program thats ready to run.
Jump back!
We can take the a.out file and go one step backwards in the
process, to the assembly language stage, using the objdump
command, like so:
objdump -d a.out > list
Have a look at the list file now its a disassembly of the
binary file, and therefore full of assembly language
instructions. Most of them are unrelated to our program, and
are used to tell the Linux program loader some useful things
and set up the environment. But tucked away in there, youll
find these lines:
movl $0x8048490,(%esp)
call 80482f0 <puts@plt>
Those are the exact same instructions as the ones we saw
a moment ago, expressed in a slightly different way. So, thats
the process of how human-readable source code is converted
into CPU-understandable instructions. The inner workings of
On the left, the output from gcc -S, and on the right a disassembled binary.
Weve highlighted the areas showing identical instructions.
WorldMags.net
WorldMags.net
Avoiding common
coding mistakes
Bug reports are useful, but you dont really want to cause too many.
Heres what to avoid and how to avoid it.
t doesnt matter how much care you put into writing your
code. Even if youve had four cups of coffee and triplecheck every line you write, sooner or later you are going to
make a mistake. It might be as simple as a typo a missing
bracket or the wrong number, or it could be as complex as
broken logic, memory problems or just inefficient code. Either
way, the results will always be the same at some point, your
program wont do what you wanted it to. This might mean it
crashes and dumps the user back to the command line. But it
could also mean a subtle rounding error in your tax returns
that prompts the Inland Revenue to send you a tax bill for
millions of pounds, forcing you to sell your home and declare
yourself bankrupt.
The IDLE
Python IDE has a
debug mode that
can show how
your variables
change over time.
WorldMags.net
WorldMags.net
you put into a comment the better, but dont write a book.
Adding comments to code can be tedious when you just want
to get on with programming, so make them as brief as you
can without stopping your flow. If necessary, you can go back
and flesh out your thoughts when you dont feel like writing
code (usually the day before a public release). When you start
to code, youll introduce many errors without realising it. To
begin with, for example, you wont know what is and isnt a
keyword a word used by your chosen language to do
something important. Each language is different, but Pythons
list of keywords is quite manageable, and includes common
language words such as and, if, else, import, class and
break, as well as less obvious words such as yield, lambda,
raise and assert. This is why its often a good idea to create
your own variable names out of composite parts, rather than
go with real words. If youre using an IDE, theres a good
chance that its syntax highlighting will stop you from using a
protected keyword.
Undeclared values
A related problem that doesnt affect Python is using
undeclared values. This happens in C or C++, for instance, if
you use a variable without first saying what type its going to
be, such as int x to declare x an integer. Its only after doing
this you can use the variable in your own code. This is the big
difference between compiled languages and interpreted ones.
However, in both languages, you cant assume a default value
for an uninitialised variable. Typing print (x) in Python, for
instance, will result in an error, but not if you precede the line
with x = 1. This is because the interpreter knows the type of a
variable only after youve assigned it a value. C/C+ can be
even more random, not necessarily generating an error, but
the value held in an uninitialised variable is unpredictable until
youve assigned it a value.
Typos are also common, especially in conditional
statements, where they can go undetected because they are
syntactically correct. Watch out for using a single equals sign
to check for equality, for example although Python is pretty
You have to be
careful in Python
that the colons
and indentation
are in the correct
place, or your
script wont run.
But this does
stop a lot of
runtime errors.
Comment syntax
Different languages mark comments differently, and there seems to be
little consensus on what a comment should look like. However, there are
a couple of rules. Most languages offer both inline and block comments,
for example. Inline are usually for a single line, or a comment after a piece
Bash
BASIC
C
C++
HTML
# A hash is used for comments in many scripting languages. When # is followed by a ! it becomes a shebang # and is used to
tell the system which interpreter to use, for example: #!/usr/bin/bash
REM For many of us, this is the first comment syntax we learn
/* This kind of comment in C can be used to make a block of text span many lines */
// Whereas this kind of comment is used after the // code or for just a single line
<!-- Though not a programming language, weve included this because youre likely to have already seen the syntax, and
therefore comments, in action -->
Java
/** Similar to C, because it can span lines, but with an extra * at the beginning */
Perl
= heading Overview
As well as the hash, in Perl you can also use something called Plain Old Documentation. It has a specific format, but it does
force you to explain your code more thoroughly
=cut
Python
As well as the hash, Python users can denote blocks of comments using a source code literal called a docstring, which is
a convoluted way of saying enclose your text in blocks of triple quotes, like this
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
Further coding
Now youve got the basics down,
its time to advance your skills
Data types
More data types
Abstraction
Using files
UNIX programs part 1
UNIX programs part 2
UNIX programs part 3
Modules
Persistence
Databases
58
60
62
64
66
68
70
74
76
78
.........................................................................................
...............................................................
.....................................................................................
...........................................................................................
.......................................
......................................
.......................................
.....................................................................................................
......................................................................................
...........................................................................................
WorldMags.net
WorldMags.net
Different types of
Python data
Functions tell programs how to work, but its data that they operate
on. Lets go through the basics of data in Python.
n this article, well be covering the basic data types in
Python and the concepts that accompany them. In later
articles, well look at a few more advanced topics that
build on what we do here: data abstraction, fancy structures
such as trees, and more.
What is data?
While were
looking only at
basic data types,
in real programs
getting the
wrong type can
cause problems,
in which case
youll see
a TypeError.
WorldMags.net
WorldMags.net
element of a list or the last character in a string, you can use
the same notation with a -1 as the index. -2 will reference the
second-to-last character, -3 the third, and so on. Note that
when working backwards, the indexes dont start at 0.
Methods
Lists and strings also have a range of other special
operations, each unique to that particular type. These are
known as methods. Theyre similar to functions such as
type() in that they perform a procedure. What makes them
different is that theyre associated with a particular piece of
data, and hence have a different syntax for execution.
For example, among the list types methods are append
and insert.
>>> list.append(chicken)
>>> list
[banana, cake, tiffin, chicken]
>>> list.insert(1, pasta)
>>> list
[banana, pasta, cake, tiffin, chicken]
As you can see, a method is invoked by placing a period
between the piece of data that youre applying the method to
and the name of the method. Then you pass any arguments
between round brackets, just as you would with a normal
function. It works the same with strings and any other data
object, too:
>>> word = HELLO
>>> word.lower()
hello
There are lots of different methods that can be applied to
lists and strings, and to tuples and dictionaries (which were
about to look at). To see the order of the arguments and the
full range of methods available, youll need to consult the
Python documentation.
Variables
In the previous examples, we used the idea of variables to
make it easier to work with our data. Variables are a way to
name different values different pieces of data. They make it
easy to manage all the bits of data youre working with, and
greatly reduce the complexity of development (when you use
sensible names).
As we saw above, in Python you create a new variable with
an assignment statement. First comes the name of the
variable, then a single equals sign, followed by the piece of
data that you want to assign to that variable.
From that point on, whenever you use the name assigned
to the variable, you are referring to the data that you assigned
to it. In the examples, we saw this in action when we
referenced the second character in a string or the third
element in a list by appending index notation to the variable
name. You can also see this in action if you apply the type()
function to a variable name:
>>> type(word)
<type str>
>>> type(list)
<type list>
The Python
interpreter is a
great place to
experiment with
Python code and
see how different
data types work
together.
Looping sequences
One common operation that you may want to perform on any
of the sequence types is looping over their contents to apply
an operation to every element contained within. Consider this
small Python program:
list = [banana, tiffin, burrito]
for item in list:
print item
First, we created the list as we would normally, then we
used the for in construct to perform the print function
on each item in the list. The second word in that construct
doesnt have to be item, thats just a variable name that
gets assigned temporarily to each element contained within
the sequence specified at the end. We could just as well
have written for letter in word and it would have worked
just as well.
Thats all we have time to cover in this article, but with the
basic data types covered, well be ready to look at how you
can put this knowledge to use when modelling real-world
problems in later articles.
In the meantime, read the Python documentation to
become familiar with some of the other methods that it
provides for the data types weve looked at before. Youll find
lots of useful tools, such as sort and reverse! Q
WorldMags.net
WorldMags.net
More Python
data types
Hardly
surprisingly,
our counting
program, after
being sorted,
finds the to
be the most
common word
in The Time
Machine, by
HG Wells.
tm = open(timemachine.txt, r)
In this example, open() is passed two variables. The first is
the name of the file to open; if it were in a different directory
from the Python script, the entire path would have to be
given. The second argument specifies which mode the file
should be opened in: r stands for read, but you can also use
w for write or rw for read-write.
Notice weve also assigned the file to a variable, tm, so we
can refer to it later in the program.
With a reference to the file created, we also need a way to
access its contents. There are several ways to do this, but
today well be using a for in loop. To see how this works,
try opening timemachine.txt in the interactive interpreter
and then typing:
>>> for line in tm:
print line
...
The result should be every line of the file printed to the
screen. By putting this code in to a .py file, say cw.py, weve
got the start of our Python program.
Cleaning up
The program description also specified that we should
exclude punctuation marks, consider the same word but in
different cases as one word, and that were counting
individual words, not lines. As it stands, we have been able to
read only entire lines as strings, however, with punctuation,
strange whitespace characters (such as \r\n) and different
cases intact.
Looking at the Python string documentation (http://
docs.python.org/library), we can see that there are four
methods that can help us convert line strings into a format
closer to that specified by the description: strip(),
translate(), lower() and split().
Each of these are methods, and as such theyre functions
that are applied to particular strings using the dot notation.
For example, strip(), which removes specified characters
from the beginning and end of a string, is used like this:
>>> line.strip()
When passed with no arguments, it removes all
whitespace characters, which is one of the jobs we needed to
get done.
The function translate() is a method that can be used for
removing a set of characters, such as all punctuation marks,
from a string. To use it in this capacity, it needs to be passed
two arguments, the first being None and the second being
the list of characters to be deleted.
>>> line.translate(None, !#$%&\()*+,-./:;<=>?@[\\]^_`{|}~)
lower() speaks for itself, really it converts every character in
WorldMags.net
WorldMags.net
a string to lower-case. split() splits distinct elements inside a
string in to separate strings, returning them as a list.
By passing an argument to split(), its possible to specify
which character identifies the end of one element and the
start of another.
>>> line.split( )
In this example, weve passed a single space as the
character to split the string around. With all punctuation
removed, this will create a list, with each word in the string
stored as a separate element.
Put all of this in the Python file we started working on
earlier, inside the for loop, and weve made considerable
progress. It should now look like this:
tm = open(timemachine.txt, r)
for line in tm:
line = line.strip()
line = line.translate(None, !#$%&\()*+,-./:;<=>?@
[\\]^_`{|}~)
line = line.lower()
list = line.split( )
Because all of the string methods return a new, modified
string, rather than operating on the existing string, weve
re-assigned the line variable in each line to store the work of
the previous step.
Uniqueness
Phew, look at all that work weve just done with data! By using
the string methods, weve been able to remove all the bits of
data that we werent interested in. Weve also split one large
string, representing a line, into smaller chunks by converting
it to a list, and in the process got to the exact, abstract
concept were most interested in: words.
Our stunning progress aside, there is still some work to be
done. We now need a way to identify which words are unique
and not just in this line, but in every line contained within
the entire file.
The first thing that should pop into your head when
thinking about uniqueness is of a dictionary, the key-value
store we saw in the previous article. It doesnt allow duplicate
keys, so by entering each word as a key within a dictionary,
were guaranteed there wont be any duplicates.
Whats more, we can use the value to store the number of
times each word has occurred, incrementing it as the
program comes across new instances of each key.
Start by creating the dictionary, and ensuring that it
persists for the entire file not just a single line by placing
this line before the start of the for loop:
dict = {}
This creates an empty dictionary, which is ready to receive
our words.
Next, we need to think about a way to get each word into
the dictionary. As we saw last time, ordinarily a simple
assignment statement would be enough to add a new word
to the dictionary. We could then iterate over the list we
created above (using another for loop), adding each entry to
the dictionary with a value of 1 (to represent that it has
occurred once in the file).
for word in list:
dict[word] = 1
But remember, if the key already exists, the old value is
overwritten and the count will be reset. To get around this, we
can place an if-else clause inside the loop:
if word in dict:
count = dict[word]
count += 1
dict[word] = count
else:
dict[word] = 1
This is a little bit confusing because dict[word] is being
used in two different ways. In the second line, it returns the
value and assigns it to the variable count, while in the fourth
and seventh lines, count and 1 are assigned to that keys
value, respectively.
Notice, too, that if a word is already in the dictionary, we
increment the count by 1, representing another occurrence.
Pythons
Standard Library
reference,
http://docs.
python.org/
library, is an
invaluable source
for discovering
what methods
are available and
how to use them.
Putting it together
Another data type wrestled with, another step closer to our
goal. At this point, all thats left to do is insert some code to
print the dictionary and put it all together and run the
program. The print section should look like this and be at the
very end of the file, outside of the line-looping code.
for word,count in dict.iteritems():
print word + : + str(count)
This for loop looks different to what youve seen before.
By using the iteritems method of the dictionary, we can
access both the key (word) and value (count) in a single loop.
Whats more, weve had to use the str() function to convert
count, an integer, into a string, as the + operator cant
concatenate an integer and a string.
Try running it, and you should see your terminal screen
filled with lines like:
...
other: 20
sick: 2
ventilating: 2
...
Data everywhere!
Thats all we planned to achieve in this particular tutorial and
its actually turn out to be quite a lot. As well as having had a
chance to see how several different types of data and their
methods can be applied to solve a real problem, we hope
youve noticed how important it is to select the appropriate
type for representing different abstract concepts.
For example, we started off with a single string
representing an entire line, and we eventually split this into
a list of individual strings representing single words. This
made sense until we wanted to consider unique instances, at
which point we put everything in to a dictionary.
As a further programming exercise, why not look into
sorting the resulting dictionary in order to see which words
occur most frequently? You might also want to consider
writing the result to a file, one entry on a line, to save the fruits
of your labour.Q
WorldMags.net
WorldMags.net
Reliability by
abstraction
Think your code is solid? Perhaps its a bit too lumpy. Creating
abstractions can make code much easier to maintain.
Square roots
To get our heads around the concept of abstraction, lets start
by thinking about square roots and different techniques for
finding them. One of these was discovered by Newton, and is
thus known as Newtons method.
It says that when trying to find the square root of a
number (x), we should start with a guess (y) of its square
root; we can then improve upon that result by averaging our
guess (y) with the result of dividing the number (x) by our
guess (y). As we repeat this procedure, we get closer and
closer to the square root. In most attempts, well never reach
a definite result, well only make our guess more and more
accurate. Eventually, well reach a level of accuracy that is
good enough for our needs and then give up. Just to be clear
about what is involved, take a look at the table below for how
you would apply this method to find the square root of 2 (for
example, x).
Its a lot of work just to
find the square root of
a number. Imagine if when
you were in school, every
time you had to find a square
root you had to do all these
steps manually. Solving
problems involving Pythagoras theorem, for instance, would
be much more unwieldy.
Luckily, assuming you were allowed to use calculators
when you were at school, theres another, much easier
method to find square roots. Calculators come with a button
marked with the square root symbol, and all you have to do is
2/1 = 2
(2 + 1)/2 = 1.5
1.5
2/1.5 = 1.33
1.4167
2/1.4167 = 1.4118
WorldMags.net
WorldMags.net
created, leaving them for you to fill in.
import math:
def square(x):
...
def closeEnough(x, guess):
...
def improveGuess(x, guess):
...
def sqrt(x, guess):
...
def pythag(a, b):
a2b2 = square(a) + square(b)
return sqrt(a2b2)
Here, weve split the code in to several smaller functions,
each of which fulfils a particular role. This has many benefits.
For starters, how much easier is the pythag() function to
read? In the first line, you can see clearly that a2b2 is the
result of squaring two numbers, and everything below that
has been consolidated in to a single function call, the purpose
of which is also obvious.
Whats more, because each part of the code has been split
into a different function, we can easily test it. For example,
testing whether improveGuess() was doing the right thing
would be very easy come up with a few values for x and
guess, do the improvement by hand, and then compare your
results with those returned by the function.
If pythag() itself was found not to return the correct
result, we could quickly test all these auxiliary functions to
narrow down where the bug was.
And, of course, we can easily reuse any of these new
functions. If you were finding the square root of a number in
a different function, for instance, you could just call the sqrt()
function six characters instead of four lines means theres
far less opportunity to make mistakes.
One final point: because our sqrt code is now abstracted,
we could change the implementation completely, but so long
Abstraction
Java
C
Assembler
Object code
There are layers of abstraction underneath everything
you do on a PC you just dont often think of them.
Layers of abstraction
Hopefully, this example has demonstrated how powerful a
technique abstraction is. Bear in mind that there are many
layers of abstraction present in everything you do on a
computer that you never think of.
For instance, when youre programming do you know how
Python represents integers in the computers memory? Or
how the CPU performs arithmetic operations such as
addition and subtraction?
The answer is probably no. You just accept the fact that
typing 2 + 3 in to the Python interpreter returns the correct
result, and you never have to worry about how it does this.
You treat it as a black box.
Think how much longer it would take you to program if you
had to manually take care of what data went in which
memory location, to work with binary numbers, and translate
alphabetic characters in to their numeric representations
thank goodness for abstraction!Q
WorldMags.net
WorldMags.net
When you
read a file, most
languages will
step through its
data from the
beginning to the
end in chunks
you specify. In
this example,
were reading a
line at a time.
Environment variables
Dealing with paths, folders and file locations can quickly
become complicated, and its one of the more tedious issues
youll face with your own projects. Youll find that different
environments have different solutions for finding files, with
some creating keywords for common locations and others
leaving it to the programmer.
This isnt so bad when you only deal with files created by
your projects, but it becomes difficult when you need to know
where to store a configuration file or load a default icon.
These locations may be different depending on your Linux
distribution or desktop, but with a cross-platform language
such as Python, theyll also be different for each operating
system. For that reason, you might want to consider using
environment variables. These are similar to variables with a
WorldMags.net
WorldMags.net
global scope in many programming languages, but they apply
to any one users Linux session rather than within your own
code. If you type env on the command line, for instance, youll
see a list of the environmental variables currently set for your
terminal session. Look closely, and youll see a few that apply
to default locations and, most importantly, one called HOME.
The value assigned to this environmental variable will be the
location of your home folder on your Linux system, and if we
want to use this within our Python script, we first need to add
a line to import the operating system-specific module. The
line to do this is:
import os
This command is also opening a file, but not in the same
way we opened list.txt. This file is known as a module in
Python terms, and modules like this import functionality,
including statements and definitions, so that a programmer
doesnt have to keep re-inventing the wheel.
Modules extend the simple constructs of a language to
add portable shortcuts and solutions, which is why other
languages might call them libraries. Libraries and modules
are a little like copying and pasting someones own research
and insight into your own project. Only its better than that,
because modules such as os are used by everyone, turning
the way they do things into a standard.
Binary files
have no context
without an
associated file
type and a way of
handling them.
Which is why you
get the raw data
output when you
read one.
The os module
Getting back to our project, the os module is designed to
provide a portable way of accessing operating systemdependent functionality so that you can write multi-platform
applications without worrying about where files should be
placed. This includes knowing where your home directory
might be. To see what we mean, add the following piece of
WorldMags.net
WorldMags.net
n the next few pages, thats what were aiming to do: get
you writing real programs. Over the next few tutorials,
were going to create a Python implementation of the
popular Unix tool cat. Like all Unix tools, cat is a great target
because its small and focused on a single task, while using
different operating system features, including accessing files,
pipes and so on.
This means it wont take too
long to complete, but will also
expose you to a selection of
Pythons core features in the
Standard Library, and once
youve mastered the basics, its
learning the ins-and-outs of your chosen languages libraries
that will let you get on with real work.
Python files
The final program well be implementing. Its not long, but it makes use of a lot
of core language features youll be able to re-use time and again.
Lets start with the easiest part of the problem: displaying the
contents of a file, line by line, to standard out. In Python, you
access a file with the open function, which returns a fileobject that you can later read from, or otherwise manipulate.
To capture this file-object for use later in your program, you
need to assign the result of running the open function to a
variable, like so:
file = open(hello.txt, r)
This creates a variable, file, that will later allow us to read
the contents of the file hello.txt. It will only allow us to read
from this file, not write to it, because we passed a second
argument to the open function, r, which specified that the file
should be opened in read-only mode.
With access to the file now provided through the newlycreated file object, the next task is to display its contents, line
by line, on standard output. This is very easy to achieve,
because in Python files are iterable objects.
Iterable objects, such as lists, strings, tuples and
dictionaries, allow you to access their individual member
elements one at a time through a for loop. With a file, this
means you can access each line contained within simply by
putting it in a for loop, as follows:
for line in file:
print(line)
The print function then causes whatever argument you
pass to it to be displayed on standard output.
WorldMags.net
WorldMags.net
If you put all this in a file, make it executable and create
a hello.txt file in the same directory, youll see that it works
rather well. There is one oddity, however theres an empty
line between each line of output.
The reason this happens is that print automatically adds
a newline character to the end of each line. Because theres
already a newline character at the end of each line in hello.txt
(there is, even if you cant see it, otherwise everything would
be on one line!), the second newline character leads to an
empty line.
You can fix this by calling print with a second, named
argument such as: print(line, end=). This tells print to put
an empty string, or no character, at the end of each line
instead of a newline character.
Passing arguments
This is all right, but compared to the real cat command,
theres a glaring omission here: we would have to edit the
program code itself to change which file is being displayed to
standard out. What we need is some way to pass arguments
on the command line, so that we could call our new program
by typing cat.py hello.txt on the command line. Since Python
has all batteries included, this is a fairly straightforward task,
as well.
The Python interpreter automatically captures all
arguments passed on the command line, and a module called
sys, which is part of the Standard Library, makes this
available to your code.
Even though sys is part of the Standard Library, its not
available to your code by default. Instead, you first have to
import it to your program and then access its contents with
dot notation dont worry, well explain this in a moment.
First, to import it to your program, add:
import sys
to the top of your cat.py file.
The part of the sys module that were interested in is the
argv object. This object stores all of the arguments passed on
the command line in a Python list, which means you can
access and manipulate it using various techniques weve
seen in previous tutorials and will show in future ones.
There are only two things
you really need to know
about this. They are:
The first element of the list
is the name of the program
itself all arguments follow
this.
To access the list, you need to use dot notation that is to
say, argv is stored within sys, so to access it, you need to type
sys.argv, or sys.argv[1] to get the first argument to
your program.
Knowing this, you should now be able to adjust the code
we created previously by replacing hello.txt with sys.argv[1].
When you call cat.py from the command line, you can then
pass the name of any text file, and it will work just the same.
The output of the real Unix command, cat, and our Python re-implementation,
are exactly the same in this simple example.
Many files
Of course, our program is meant to accept more than one file
and output all their contents to standard output, one after
another, but as things stand, our program can only accept
one file as an argument.
To fix this particular problem, you need to loop over all the
files in the argv list. The only thing that you need to be careful
of when you do this is that you exclude the very first element,
WorldMags.net
WorldMags.net
Enhance your
UNIX program
Our tour of the Python programming language continues, as we
continue our clone of the Unix cat command.
The Python language comes with all the bells and whistles you need to write
useful programs. In this example, you can see the replace method applied to
a string in order to remove all white space in the tutorial, we used the rstrip
method for a similar purpose.
WorldMags.net
WorldMags.net
The Python 3
website provides
excellent
documentation
for a wealth of
built-in functions
and methods.
If you ever
wonder how to
do something in
Python, http://
docs.python.
org/3/ should
be your first port
of call.
Options:
-h, --help show this help message and exit
-E
Show $ at line endings
-n
Show line numbers
Just like a real program!
To get started with OptionParser, you first need to import
the necessary components:
from optparse import OptionParser
You may notice that this looks a bit different from what we
saw before. Instead of importing the entire module, were only
importing the OptionParser object. Next, you need to create
a new instance of the object, add some new options for it to
detect with the add_option method, and pass it a usage
string to display:
usage = usage: %prog [option]... [file]...
parser = OptionParser(usage=usage)
parser.add_option(-E, dest=showend, action=store_
true, help=Show $ at line
endings)
parser.add_option(-n, dest=shownum, action=store_
true, help=Show line numbers)
The %prog part of the usage string will be replaced with
the name of your program. The
dest argument specifies what
name youll be able to use to
access the value of an
argument once the parsing has
been done, while the action
specifies what that value should
be. In this case, the action store_true says to set the dest
variable to True if the argument is present, and False if it is
not. You can read about other actions at
http://docs.python.org/3/library/optparse.html.
Finally, with everything set, you just need to parse the
arguments that were passed to your program and assign the
results to two array variables:
(options, args) = parser.parser_args()
The options variable will contain all user-defined options,
such as -E or -n, while args will contain all positional
arguments left over after parsing out the options. You can call
these variables whatever you like, but the two will always be
set in the same order, so dont confuse yourself by putting the
WorldMags.net
WorldMags.net
Finish up your
UNIX program
Our guide to the Python programming language continues.
This tutorial, were going to finish our clone of cat.
Objects
Last time, we ended by saying that there are many ways we
could implement the line counting option in our program.
Were going to show you how to do it in an object-oriented
style, because it gives us an excuse to introduce you to this
aspect of Python programming. You could, however, with a
little careful thought, implement the same function with some
Python objects
Just to prove that it works, heres our cat implementation, with all of the
options being put to use.
WorldMags.net
WorldMags.net
earth that was about. Well, it is the main distinguishing feature
between methods and ordinary functions.
Methods, even those which have no other arguments,
must have the self variable. It is an automatically populated
variable, which will always point to the particular instance of
the object that youre working with. So self.count is a count
variable that is exclusive to individual instances of the
catCommand object.
The completed program isnt very long, but it has given us a chance to
introduce you to many different aspects of the Python language.
for a in args:
f = open(a, r)
c.run(f, options)
else:
c.run(sys.stdin, options)
We havent filled in the object parsing code from the
previous tutorial, because that hasnt changed. What is new is
the c = catCommand() line. This is how we create an
instance of a class how we create a new object. The c object
now has a variable, count, which is accessible by all its
methods as the self.count variable. This is what will enable us
to track the line numbers.
We then check to see whether any arguments have been
passed. If they have, then we call the run method of the
object c for each file that was passed as an argument,
passing in any options extracted by OptParse along the way.
If there werent any
arguments, though, we
simply call the run method
with sys.stdin instead of a
file object.
The final thing we need
to do here is actually call
the main function when the program is run:
if __name__ == __
main__:
main()
These last two lines are the strangest of all, but quite
useful in a lot of circumstances. The name variable is special
when the program is run on the command line, or otherwise
as a standalone application, it is set to main; however, when it
is imported as an external module to other Python programs,
its not.
In this way, we can automatically execute main when run
as a standalone program, but not when were importing it as
a module. Q
WorldMags.net
WorldMags.net
Helping you live better & work smarter
www.lifehacker.co.uk
twitter.com/lifehackeruk
facebook.com/lifehackeruk
WorldMags.net
WorldMags.net
THE GADGET WEBSITE
INTRODUCING
THE ALL-NEW T3.COM
NOW
LIVE
www.T3.com
WorldMags.net
WorldMags.net
Neater code
with modules
Untangle the horrible mess of code that youve made and add
coherent structure to your programs.
WorldMags.net
WorldMags.net
By splitting
your code up
in to smaller
chunks, each
placed in its
own file and
directory, you
can bring order
to your projects
and make future
maintenance
easier.
print food
def show_choc():
food = [snickers, kitkat, dairy milk]
print food
show_choc()
print food
If you run that, youll see that outside the function the
variable food refers to a list of fruit, while inside the function,
it refers to a list of chocolate bars. This demonstrates two
different scopes: the global scope of the current module, in
which food refers to a list of fruit, and the local scope of the
function, in which food refers to a list of chocolate.
When looking up a variable, Python starts with the
innermost variable and works its way out, starting with the
immediately enclosing function, and then any functions
enclosing that, and then the modules global scope, and then
finally it will look at all the built-in names.
In simple, single-file programs, it is a bad idea to put
variables in the global scope. It can cause confusion and
subtle errors elsewhere in your program. Using modules can
help with this problem, because each module has its own
global scope. As we saw above, when we import a module, its
contents are all stored as attributes of the modules name,
accessed via dot notation. This makes global variables
somewhat less troublesome, although you should still be
careful when using them. Q
Python style
While many people think of Python as
a modern language, its actually been
around since the early 1990s. As with any
programming language thats been
around for any length of time, people who
use it often have learned a lot about the
best ways to do things in the language
in terms of the easiest way to solve
WorldMags.net
WorldMags.net
Embrace storage
and persistence
Deal with persistent data and store your files in Python to make your
programs more permanent and less transient.
Writing to files
Suppose youre writing your own RSS application to deal with
your favourite feeds. Youve already got some way to ask
users to enter in a list of feeds (perhaps using raw_input(), or
perhaps using a web form and CGI), but now you want to
store that list of feeds on disk so you can re-use it later when
youre checking for new updates. At the moment, the feeds
are just stored in a Python list:
feeds = [http://newsrss.bbc.co.uk/rss/newsonline_uk_
edition/front_page/rss.xml, http://www.tuxradar.com/rss]
To get the feeds into the file is a simple process. Just use
the write method:
for feed in feeds:
file.write({0}\n.format(feed))
Serialising
Working with files would be much easier if you didnt have to
worry about converting your list (or dictionary, for that
matter) into a string first of all for dictionaries in particular,
this could get messy. Fortunately, Python provides two tools
to make this easier for us.
The first of these tools is the pickle module. Pickle
accepts many different kinds of Python objects, and can then
convert them to a character string and back again. You still
have to do the file opening and closing, but you no longer
have to worry about figuring out an appropriate string
representation for your data:
import pickle
...
with open(lxf-test.txt, a) as file:
pickle.dump(feeds, file)
WorldMags.net
WorldMags.net
If youre interested in persistent data in Python, a good next stopping point is the ZODB object database. Its much
easier and more natural in Python than a relational database engine (www.zodb.org).
Shelves
Of course, some code bases have many different objects that
you want to store persistently between runs, and keeping
track of many different pickled files can get tricky. Theres
another Python standard module, however, that uses pickle
underneath, but makes access to the stored objects more
intuitive and convenient: the shelve module.
Essentially, a shelf is a persistent dictionary that is to
say, a persistent way to store key-value pairs. The great thing
about shelves, however, is that the value can be any Python
object that pickle can serialise. Lets take a look at how you
can use it. Thinking back to our RSS reader application,
imagine that as well as the list of feeds to check, you wanted
to keep track of how many unread items each feed had, and
which item was the last to be read. You might do this with a
dictionary, for example:
tracker = { bbc.co.uk:
{ last-read: foo,
num-unread: 10, },
tuxradar.co.uk: { last-read: bar,
num-unread: 5, }}
You could then store the list of feeds and the tracking
details for each in a single file by using the shelve module,
like so:
import shelve
shelf = shelve.open(lxf-test)
shelf[feeds] = feeds
shelf[tracker] = tracker
shelf.close()
There are a few important things that you should be aware
of about the shelve module:
The shelve module has its own operations for opening and
closing files, so you cant use the standard open function.
To save some data to the shelf, you must first use a
standard Python assignment operation to set the value of a
particular key to the object you want to save.
As with files, you must close the shelf object once finished
with, otherwise your changes may not be stored.
Accessing data inside the shelf is just as easy. Rather than
assigning a key in the shelf dictionary to a value, you assign a
value to that stored in the dictionary at a particular key: feeds
= shelf[feeds]. If you want to modify the data that was
stored in the shelf, modify it in the temporary value you
assigned it to, then re-assign that temporary value back to
the shelf before closing it again.
Thats about all we have space for this tutorial, but keep
reading, because well discuss one final option for persistent
data: relational databases (for example, MySQL). Q
WorldMags.net
WorldMags.net
Data organisation
and queries
Lets use SQL and a relational database to add some structure to
our extensive 70s rock collection. You do have one, right?
Relational databases
are used to ask complex
questions about data
Relationships
Lets start by thinking about the information we want to store
in our music collection. A logical place to start might be
thinking about it in terms of the CDs that we own. Each CD is
a single album, and each album can be described by lots of
other information, or attributes, including the artist who
created the album and the tracks that are on it.
We could represent all this data in one large,
homogeneous table like the one below which is all well
Duplicated data
Album
Free At Last
Free At Last
Artist
Free
Free
Track
Travellin Man
Track time
2:34
3:23
Album time
65:58
65:58
Year
1972
1972
Band split
1973
1973
Relational database
Album name
Free At Last
Running time
65:58
Year
1972
Artist_id
and good, but very wasteful. For every track on the same
album, we have to duplicate all the information, such as the
album name, its running time, the year it was published, and
all the information about the artist, too, such as their name
and the year they split. As well as being wasteful with storage
space, this also makes the data slower to search, harder to
interpret and more dangerous to modify later.
Relational databases solve these problems by letting us
split the data and store it in a more efficient, useful form. They
let us identify separate entities within the database that
would benefit from being stored in independent tables.
In our example, we might split information about the
album, artist and track into separate tables. We would then
only need to have a single entry for the artist Free (storing the
name and the year the band split), a single entry for the
album Free At Last (storing its name, the year it was
published and the running time), and a single entry for each
track in the database (storing everything else) in each of their
respective tables.
All that duplication is gone, but now all the data has been
separated, what happens when you want to report all the
information about a single track, including the artist who
produced it and the album it appeared on? Thats where the
relational part of a relational database comes in.
Every row within a database table must in some way be
unique, either based on a single unique column (for example,
a unique name for an artist, or a unique title for an album), or
a combination of columns (for example, the album title and
year published). These unique columns form what is known
as a primary key. Where a natural primary key (a natural set
of unique columns) doesnt exist within a table, you can easily
add an artificial one in the form of an automatically
incrementing integer ID.
We can then add an extra column to each table that
references the primary key in another table. For example,
consider the table above. Here, rather than giving all the
information about the artist in the same table, weve simply
specified the unique ID for a row in another table, probably
called Artist. When we want to present this album to a user, in
conjunction with information about the artist who published
WorldMags.net
WorldMags.net
it, we can get the information first from this Album table, then
retrieve the information about the artist, whose ID is 1, from
the Artist table, combining it for presentation.
SQL
That, in a nutshell, is what relational databases are all about.
Splitting information into manageable, reusable chunks of
data, and describing the relationships between those chunks.
To create these tables within the database, to manage the
relationships, to insert and query data, most relational
databases make use of SQL, and now that you know what a
table and a relationship is, we can show you how to use SQL
to create and use your own.
After logging into the MySQL console, the first thing we
need to do is create a database. The database is the top-level
storage container for bits of related information, so we need
to create it before we can start storing or querying anything
else. To do this, you use create database:
create database lxfmusic;
Notice the semi-colon at the end of the command all
SQL statements must end with a semi-colon. Also notice that
weve used lower-case letters: SQL is not case-sensitive, and
you can issue your commands in whatever case you like.
With the database created, you now need to switch to it.
Much as you work within a current working directory on the
Linux console, in MySQL, many commands you issue are
relative to the currently selected database. You can switch
databases with the use command:
use lxfmusic;
Now to create some tables:
create table Album (
Album_id int auto_increment primary key,
name varchar(100)
);
create table Track (
Track_id int auto_increment primary key,
title varchar(100),
running_time int,
Album_id int
);
The most obvious things to note here are that we have
issued two commands, separated by semi-colons, and that
we have split each command over multiple lines. SQL doesnt
care about white space, so you can split your code up
however you like, as long as you put the right punctuation in
the correct places.
As for the command itself, notice how similar it is to the
create database statement. We specify the action we want
to take, the type of object that were operating on and then
the properties of that object. With the create database
statement, the only property was the name of the database;
with the create table statement, weve also got a whole load
of extra properties that come inside the parentheses and are
separated by commas.
These are known as column definitions, and each commaseparated entry describes one column in the database. First,
we give the column a name, then we describe the type of data
stored in it (this is necessary in most databases), then we
specify any additional properties of that column, such as
whether it is part of the primary key.
The auto_increment keyword means you dont have to
worry about specifying the value of Track_id when inserting
data, as MySQL will ensure that this is an integer that gets
incremented for every row in the database, thus forming a
primary key. You can find out more about the create table
statement in the MySQL documentation at http://dev.
mysql.com/doc/refman/5.5/en/create-table.html.
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
Raspberry Pi
Discover how you can develop on
the low-cost, micro-PC system
Getting started
Starting Scratch
Further Scratch
Coding with IDLE
Python on the Pi
Python 3.0 on the Pi
Advanced sorting
Hacking Minecraft
Image walls in Minecraft
2048 in Minecraft
82
84
88
92
94
98
102
108
110
114
.....................................................................
................................................................
...................................................................
.............................................................
...............................................................
..............................................
.....................................................
..................................................
..........................
......................................................
WorldMags.net
WorldMags.net
Welcome to Pi
Terminal velocity
That updates Raspbians local list of all available packages. In
the next step, the list of current packages installed on the
system is checked against this, and where newer packages
are available, these are offered for upgrade:
$ sudo apt-get
upgrade
If theres lots
of packages to
upgrade, the
process may take
some time. The
bottleneck is mostly
due to the speed at which data can be written to the SD card
you may want to make a cup of tea. You should update your
packages pretty regularly; youll get important security
updates and the latest features or fixes for all your software.
Python is a particularly good first programming language.
The syntax is clean and concise, there are no awkward
The new
Epiphany browser
works well; it can
even play HTML5
YouTube videos,
such as this one
featuring a tiny
arcade cabinet.
WorldMags.net
WorldMags.net
Launch bar
Frequently used applications
can be added here just rightclick it and select Application
Launch Bar Settings. The black
screen icon in the centre opens
the terminal.
File manager
The PCManFM file manager is
lightweight but thoroughly
capable. It opens at your home
folder and is also capable of
browsing Windows and other
network shares.
Terminal
Network
From here you can configure your wired and wireless network
connections. Some wireless adapters work better with the Pi
than others, so its worth doing some research before you buy.
>>> 60 * 60 * 24 * 365
As mentioned elsewhere, it makes more sense to save
your programs in text files. So well now leave the interpreter
by hitting Ctrl+D, and make our first Pi Python program. At
this point, you have a choice: you can proceed as in the Mint
tutorial and use the nano text editor, or you can use the
graphical one located in the Accessories menu. Before you
make that choice, though, well create a directory for storing
our Python programs. Keeping on top of your filing is A Good
Thing, too.
$ mkdir ~/python
Now type the following into your chosen editor
print(Hello World)
and then save the file. Assuming youre using the graphical
editor, choose, File > Save As, and then navigate to the
recently created Python folder, and save it as helloworld.py.
If youre using nano, use Ctrl+X to save and exit.
Now we can run this and see what we have created:
$ cd ~/python
$ python3 helloworld.py
Its not going to win any awards, but its a fully working
program and something you can build around in further
programming adventures. Q
WorldMags.net
WorldMags.net
Starting Scratch
Discover how to build a cat and mouse game using this straightforward
beginners programming language.
ou can use a wide range of programming languages
with the Raspberry Pi, and the little learning
computer is our focus in this series of articles,
although Scratch can be downloaded from https://scratch.
mit.edu whether youre using Windows, Mac or Linux. These
tutorials will be equally applicable whatever your platform.
Its a great beginners language, because it introduces
many of the concepts of programming while at the same time
being easy to use. Its especially good for creating graphical
programs such as games. If youve never programmed
before, or if youre new to graphical programming, well ease
you gently in, and hopefully have a little fun along the way.
Without further ado, lets get started.
Youll find Scratch on the desktop in core Raspberry Pi
operating system Raspbian, so theres no need to install
anything just click on the icon to get started. Its best to use
Raspbian rather than one of the other distros for the Pi for
this, because not many of the others support Scratch.
The main window is split up into sections. Broadly
speaking, the bits you can use to make your programs are on
the left, while you make your programs in the middle, and the
programs run on the right. Each program is made up of a
number of sprites (pictures) that contain a number of scripts.
Its these scripts that control what happens when the
program is running.
In the top-left corner, youll see eight words: Motion,
Looks, Sound, Pen, Control, Sensing, Operations and
Variables. Each of these is a category that contains pieces
that you can drag and drop into the scripts area to build
programs. Good luck!
Scratch comes with a range of images that you can use for sprites.
Click on New Sprite From File, or Costumes > Import to see them.
WorldMags.net
WorldMags.net
Variables and messages
Sooner or later, youre going to want to get your
program to remember something. It might be
a number, a piece of text, or anything. You can do
this using variables. These are little pieces of the
computers memory that your program can
place pieces of data in. In step 3, we create a pair
of these to store some numbers in, although we
could also put text in them. Note that in some
programming languages, you have to create
different types of variables if you want to store
Messages
If you create a number of scripts, you may need
Step by step
Click on Variables in the top-left (see boxout above for more details on
what they are). Click on Make A Variable and enter the variable name
as score. Repeat the process to create a variable called over.
Set keys
Under the script When r Key Presses, add the lines show (from
Looks), Go To X:100, Y:100 (from Motion, dont forget to change 0s
to 100s), Set Score To 0 and Set Over to 0 (both from Variables).
WorldMags.net
WorldMags.net
Add broadcast
Add the block Broadcast to the bottom of the When r Key Pressed
script. Once its there, click on the drop-down menu and select New...
and give the message the name start. Well use this to let the other
sprite know that the game has started.
Inside the Repeat Until Over = 1 block, add Change score By 1 (from
Variables), Move 7 Steps (from Motion) and If On Edge, Bounce
(also from Motion). These three pieces of code will be constantly
repeated until the variable over gets set to 1.
Once the game has finished (and the cat has got the mouse), the
Repeat Until loop will end and the program will continue underneath
it. Drag Hide (from Looks) under the loop, so the mouse disappears
when this happens.
10
Select Choose New Sprite From File > Cat 4, and shrink the sprite
down to an appropriate size, as we did with the mouse. Each sprite has
its own set of scripts. You can swap between them by clicking on the
appropriate icon in the bottom-right.
Create a loop
We can create loops that cycle through the same code many times.
Continue the script with Repeat Until (from Control), and then
drag and drop = (from Operators), then drag Over (from
Variables) into the left-hand side of the = and enter 1 on the right.
In the scripts for the new sprite, start a new script with When I Receive
start (from Control), and Go To X:-100 Y:-100. This will move the cat
over to the opposite corner of the screen from the mouse. (0,0) is the
middle.
WorldMags.net
WorldMags.net
11
12
As with the mouse, the cat also needs a loop to keep things going. Add
Repeat Until (from Control), and then in the blank space add
Touching Sprite 1 (from Sensing). This will keep running until the cat
(sprite 2) catches the mouse (sprite 1).
13
14
The loop will finish when the cat has caught the mouse the game is
over, so we need to stop the script on Sprite 1. We do this by adding
Set over To 1 (from Variables) underneath the Repeat Until block.
This will cause Sprite 1s main loop to finish.
15
Display a score
We now want to let the player know that the game is over. We will
do this in two ways: with audio, and on screen. Add Play Drum 1
for 1 Beats (from Sound), then Say Mmmm Tasty For 1 Secs
(from Looks).
16
Finally, we can let the player know their score. We increased the
variable score by one every loop, so this will have continued to go up.
Add Say You Scored For 1 Secs, then drag another Say for 1
Secs block and then drag score (from Variables) into the blank field.
Inside the Repeat Until block, add Point Towards Sprite 1 (from
Motion) and Move 4 Steps (also from Motion). The amount the cat
and mouse move in each loop affects the difficulty of the game. We
found 4 and 7, respectively, to work well.
Press [r] and play! You can use the up and down arrows to move the
mouse around. You can make it easier or more difficult by changing
the size of the sprites and the amount they move each loop. Good luck
and happy gaming!Q
WorldMags.net
WorldMags.net
Further Scratch
Weve dived in, but lets now look at the fundamentals of Scratch to
understand how our programs work and set up a straightforward quiz.
Scratch uses a
three-column UI.
From left to right:
Block Palette,
Script Area and
The Stage.
to version 2.
As mentioned, Scratch uses a block-based system to
teach users how different functions work in a program. This
can be broken down into the following groups and colours:
Motion (dark blue) This enables you to move and control
sprites in your game.
Control (orange) These blocks contain the logic to control
your program (loops and statements) and the events needed
to trigger actions, such as pressing a key. In Scratch 2.0, the
events stored in Control have their own group, which is called,
naturally enough, Events.
Looks (purple) These blocks can alter the colour, size or
costume of a sprite, and introduce interactive elements, such
as speech bubbles.
Sensing (light blue) For sensing handles, the general
input needed for your program, for example, keystrokes,
sprite collision detection and the position of a sprite on
the screen.
Sound (light purple) Adds both music and sound effects to
your program.
Operators (green) This enables you to use mathematical
logic in your program, such as Booleans, conditionals and
random numbers.
Pen (dark green) This is for drawing on the screen in much
the same way that logo or turtle enable you to do so.
Variables (dark orange) Creates and manipulates
containers that can store data in your program.
By breaking the language down into colour-coded blocks
Scratch enables anyone to quickly identify the block they
Scratch online
Scratch is available across many platforms. It
comes pre-loaded on every Raspberry Pi running
Raspbian and is available for download from
http://scratch.mit.edu. But did you know that
there is an online version that provides all of the
functionality present in the desktop version, but
requires no installation or download? Head over
to the MIT website (above) and youll be greeted
WorldMags.net
WorldMags.net
need. Children typically work
via the colour-coding system at
first, and then through the
natural process of playing they
understand the link between
each of the blocks and how
they work together.
The environment
Scratch uses a clear and
structured layout, which is
divided into three columns.
The first column is:
Block palette This is where
our blocks of code are stored
and sorted by function.
Fig 1.1 The code used
Script area In the second
for Matt to receive
column is an area where you
broadcasts.
can drag our blocks of code
from the block palette to add code to our program.
The stage The third and final column shows the results of
your programming and can be used to interact with the game
world. At the bottom of this column you will also find the very
handy Sprites pane. This shows the sprites and assets that
belong to your particular program. Clicking on a sprite will
change the focus to that sprite, enabling you to write code for
that sprite only.
WorldMags.net
Quick
tip
WorldMags.net
Quick
tip
Blocks that can be
linked together will
have a white halo
indicating they can
be connected.
correct, Neil will say that the answer is correct, play a sound
to reward the player, then alter the variable score by 1 point
and, finally, broadcast support to Matt, who will say
something nice. If the player provides an incorrect answer,
then Neil will say Incorrect, play a gong sound effect, and
then increment the guesses variable by 1 and send a
broadcast to Matt who will taunt the player.
Part two of the code is exactly the same as the main loop
of part one and the reason why is because we duplicated the
code and changed the question and expected answer. To
duplicate code in Scratch, you can simply right-click on the
blocks of code and select Duplicate. Hey presto you have
doubled your code in just one click.
The first section of the third part is exactly the same as
the main loop from the previous two parts. So lets move
down to the last four blocks of code (see Fig 1.6, below). Our
first block is a broadcast score to Matt this will trigger Matt
to tell us the score. We then pause for three seconds to allow
Matt to finish speaking. Then we send another broadcast to
Matt, who will then run thorough the end of game code
associated with that broadcast. Lastly, we use stop all to stop
any scripts in the game.
The Green Flag event is the code that controls the
number of guesses that the player has. We use conditional
logic to say that when the number of guesses is equal to 3,
Fig 1.7 The Stage contains the sprites but it can also
have its own scripts.
The stage
As well as being the home of our sprites, the stage can also
contain its own scripts (see Fig 1.7, above). For our game we
have two sections of code on the stage. Both sections of code
are triggered by the click on Green Flag event. The first part
resets two variables called guesses and score. We then ask
the player to provide their name, which is then broadcast to
Matt and Neil, and starts the main code loop assigned
to Neil. The second section of code is an infinite loop that
will play the loop DrumMachine continuously and set its
volume to 50%.
As we mentioned earlier on, variables are a great way to
store data. But before we can use one, we need to create it. To
create a variable, we need to use the Variables button from
the block palette. In there you will find the Make a variable
button. Click on it and you will see Fig 1.9 (see right).
In our game we used two variables score and guesses
and we want them both to be available for all sprites, so that
Matt and Neil can use them both. Once created, we can easily
Programming concepts
Using Scratch is great fun but did you realise that
you are also learning to code? No matter what
language you use, the underlying concepts of
coding provide a firm foundation. And once
learnt they can be applied to any coding project.
The main concepts are:
Sequences A series of tasks required to be
completed in a certain order. For example, the
steps needed to solve a maze.
Loops A way to repeat a sequence. They can
be run for ever (while true) or controlled using a
for statement (for x in range(0,3)). We have
used many loops to control the players progress
in our game.
WorldMags.net
WorldMags.net
drop these variables into our code, enabling us to reuse their
value many times in the game, see Fig 1.8 below for the
example that we made for our game.
Pseudo code
When trying to understand the logic of our game, we like to
write pseudo code. Pseudo what? we hear you say. This is
when you write down the logic of how your program will work.
Lets look at a simple example:
a has the value of 0
while a is less than 10:
print on the screen the value of a
increment a by 1
So we have our pseudo code, but how do we express this
in a programming language? First, lets do this with Scratch
followed by Python. In Scratch, our code will look like this:
[When Green Flag is clicked]
Set variable a to 0
forever if a < 10
say a for 2 secs
change a by 1
This gives the variable a the value of 0. We then create a
conditional loop that only loops while a is less than 10. Inside
the loop, we ask Scratch to print the value of a for 2 seconds,
then increment the value of a by 1. This loop will continue until
we reach a = 9 and then it will stop as the next value, 10, is
not less than 10. In Python our code looks like this
a=0
while a < 10:
print a
a=a+1
So why did we include that piece of Python code in a
Scratch tutorial? It simply illustrates that there isnt a lot of
difference in the logic between the two languages, and that
Scratch can be used to help understand the logic that powers
many applications. In both Scratch and Python, we create a
variable called a and set its value to be 0, from there we
create a loop that will continue to iterate round until it reaches
9 (which is less than 10). Every time we go round the loop, we
iterate a by 1, allowing us to count the number of times that
we have been around the loop.
Fig 1.8 As you can see, there are two variables in our
game: guesses and score.
Weve done it! Weve made a game. So now lets play our
game. The flow of the game should be as follows:
Click on green flag.
You will be asked for your name.
Matt says hello and Neil says hello and your name.
They both welcome you to the quiz.
Matt prompts Neil to ask the first question.
Neil asks a question.
A box will appear for you to type your answer.
If answer correct, then Neil will say so, Matt will also say
something nice.
Your score will increase by one.
Else if your answer is wrong, you will be taunted by Matt
and the number of guesses will increase by 1, leaving you
with only 2 guesses left. You will then have another chance
to answer the question.
If you guess correctly, you will move on to the next question,
and this will happen twice as there are three questions.
If you answer all the questions correctly, Matt will tell you
your final score and then say Game Over.
The Game Over sprite will appear on screen and all of the
scripts in the game will be turned off.
If the number of guesses made reaches 3 at any point in the
game, then the game will automatically skip to the Game
Over screen.
Were going leave Scratch for now, though this is just
scratching the surface of what you can do with it. Were going
to move back to using the more advanced Python on the
Raspberry Pi over the next twenty or so pages. We hope
youve enjoyed learning Scratch; its a great tool to
understand coding but its also great fun to learn.Q
WorldMags.net
WorldMags.net
Coding in IDLE
Make Python coding more convenient and start hacking in Minecraft.
IDLE hands
Well put Minecraft aside for the moment as we introduce
IDLE. Press Tab to release the mouse cursor from the game,
and click on the Raspbian menu. Youll notice that it rather
unhelpfully appears behind the Minecraft window. This is
because of the selfish way the game accesses the GPU, in
effect ignoring anything else on the screen and summarily
drawing on top of it. So either minimise the Minecraft window
or put it somewhere out of the way. We need it open so that
IDLE can talk to it.
Youll find Python 2 and 3 versions of IDLE in the
Programming menu. The Minecraft Python API has been
updated to support version 3, so use that one. Theres a quick
guide to the interface opposite note that only the
Interpreter window
opens initially.
Select File >
Open, then navigate
to the python/
folder and open the
helloworld.py file
we made in the
previous tutorial. The Editor window opens to display our
code, complete with helpful syntax highlighting. From the Run
menu, select Run Module or press F5. The Interpreter window
now springs to life with its greeting. If you edit the code
Steve feels
catastrophically
compelled to
introduce his
sword to the
TNT. Its actually
harmless
because its not
live. This can
be changed,
however...
WorldMags.net
WorldMags.net
Interpreter
We can use this shell exactly
as we did earlier when we ran
Python from the command line.
Output from programs run from
the Editor appear here.
Class browser
This is where the functions,
classes or methods of any
code we have open appear.
You can access it from the
Window menu.
Run menu
Editor
without saving it, the Run Module helpfully asks if you want to
save it first you cant run unsaved code. If your code
contains mistakes, you get an error message and the
offending bit of code is highlighted in red.
Close the helloworld.py file and start a new one by
choosing File > New from the Interpreter window. Well start
our new program with the boilerplate code required to get
Python talking to our Minecraft game:
from mcpi.minecraft import Minecraft
mc = Minecraft.create()
The first line imports the Minecraft module into our code
dont worry too much about the awkward syntax here, but
do worry about getting the capitalisation correct. The second
line sets up a new object, mc (short for minecraft), using the
create() function that we imported in the first line. The mc
object acts as a conduit between our code and the Minecraft
game. Now lets add a third line, so that our program actually
does something:
mc.postToChat(Hello Minecraft World)
Save this file in the python/ folder as hellomc.py. Now
arrange windows so that both your code and Minecraft are
visible, and run your program. If all goes according to plan, a
message should appear in the game. A few seconds later, it
will vanish. Weve manipulated the games chat feature to
display messages not from other users, but from our own
program. And we can do an awful lot more.
As you [you mean Steve Ed.] wander around the
Minecraft world, youll notice that some numbers in the topleft change. These give your position in terms of a 3D
co-ordinate system. Dont worry if the idea of doing geometry
is terrifying or offensive for now, all you need to know is that
the x and z directions are parallel to the worlds surface, and
the y direction determines your height.
The API contains functions for working with the players
position: we can use mc.Player.getPos() to get the players
WorldMags.net
WorldMags.net
Python 3: Go!
>>> quit()
However, your first lines of code really ought to be more
positive than that, so lets instead do a cheery:
>>> print('Hello world.)
Having pressed Enter and greeted our surroundings, we
can get down to some proper coding. Its useful to accept and
work with user input, which is accomplished as follows:
>>> name = input(State your name )
Note the space before the closing quote. The command
prints the given prompt and waits for something to be typed,
which will appear alongside. This means that when the user
starts typing their name (or whatever else they want), it is
separated from our curt demand. We could also have used a
print() line to display this prompt, and then instead used a
blank input call as follows:
>>> name = input()
This means that input is accepted on a new line. Either
way, the users input is stored as a string in a variable called
name. From the interpreter, we can see the values of any
variable just by typing its name however, we can also use
the print function:
>>> print(Greetings, name, enjoy your stay.)
The print() function can work with as many arguments as
you throw at it, each one separated by a comma. Note that
we dont need to put spaces around our variable, as we did
with the input function; by default, print separates
arguments with a space. You can override the behaviour by
specifying a sep parameter to the function for example, the
following code uses a dot:
>>> print(Greetings, name, enjoy your stay.,sep=.)
On the other hand, using sep= instead gives no
separation at all. Besides separators, print() also issues a
newline character, represented by the string \n after the
final argument. This can be easily changed, though, by
specifying the end parameter, which is sometimes desirable,
depending on the circumstances.
Besides welcoming people, we can use the interpreter as a
calculator. It understands + (addition), - (subtraction), *
(multiplication), / (division) and ** (exponentiation), as well
as many more advanced maths functions if you use the
maths module. For example, to find the square root of 999
times 2015 and store the result in a variable x:
>>> import math
>>> x = math.sqrt(999 * 2015)
One of many things that Python helpfully takes care of for
us is data types. Our first variable name was a string, while
the recently defined x is a floating point number (or float for
short). Unlike typed languages, where we would have to
explicitly specify the type of a variable where it is defined,
Python is smart enough to figure out that sort of information
for itself, saving us the trouble. Also, Python variables do not
have fixed types, so we could actually recast x above to an
integer with:
>>> x = int(x)
WorldMags.net
WorldMags.net
Raspberry Pi-thon
If youre following this tutorial on a Raspberry Pi
(1 or 2) and using the Raspbian distribution,
theres good news: you dont need to perform
any additional steps to get Python working. In
fact, you get the pleasure of a ready-to-roll
development environment called IDLE. You might
find this more fun to work with than the
command-line interpreter we use throughout
this tutorial, and such things certainly become
advantageous when working with larger projects.
The IDLE environment is ideal [ha, ha Ed] for developing larger Python
projects. Its included in the Raspbian distribution, too.
WorldMags.net
WorldMags.net
This Pythonic
creed is worth
studying.
There are all
kinds of bad
programming
habits, which
are best to avoid
from day zero.
print(iteration #, count)
Enter a blank line after the print statement to see the
stunning results of each iteration in real time.
The variable count takes on the values 0 to 4 from the
range iterator, which we met earlier. The result is that we
issue five print statements in only two lines of code. If you
want to iterate over different values for count, then you can
use a different range or even a list you can loop over any
objects you like, not just integers.
Another type of loop is the while loop. Rather than
iterating over a range (or a list), our wily while loop keeps
going over its code block until some condition ceases to hold.
We can easily implement for loop functionality with this
construct, as shown here:
>>> count = 0
>>> while count < 5:
print(count)
count += 1
The print statement belongs to the loop, thanks to the
indentation. The code still runs if you dont indent this last
line, but in that case the print call is not part of the loop, so
only gets executed at the end, by which time the value of
count has reached 5. Other languages delineate code blocks
using brackets or braces, but in Python its all colons and
judicious use of white space. That while loop was rather
trivial, so lets look at a more involved example:
>>> year = 0
>>> while year < 1900 or year >= 2015:
year = int(year)
We met the input statement on the first page, so this code
just keeps asking you the same thing until an appropriate
value is selected. We use the less than (<) and greater than or
equal to (>=) operators conjoined with an or statement to
test the input. So long as year has an unsuitable value, we
keep asking. It is initialised to 0, which is certainly less than
1900, so we are guaranteed to enter the loop. You could
change 1900 if you feel anyone older than 115 might use your
program. Likewise, change 2015 if you want to keep out
(honest) youngsters.
Whenever you deal with user input, you must accept the
possibility that they will enter something not of the required
form. They could, for example, enter their grannys name, or
the first line of The Wasteland, neither of which can be
interpreted as a year. Fortunately, the last line of our example
does a good job of sanitising the input if we try to coerce a
string consisting of anything other than digits to an int, then
we end up with the value 0, which guarantees that we
continue looping the loop. Note that the input function
always returns a string, so even good input for example,
1979 needs some treatment; trying to compare a string
and an int results in an error.
Grown-up coding
Its time for us to level up and move on from the interpreter
even though its awfully handy for checking code snippets, it
is not suitable for working on bigger projects. Instead well
save our code to a text file with a .py extension, which we can
either import into the interpreter or run from the command
line. You need to find a text editor on your system you can
always use nano from the command line, but you may prefer
to use a graphical one, such as Gnomes gedit, KDEs kate or
the lightweight Leafpad, which comes with Raspbian. You may
even wish to use a development environment such as IDLE
(which comes with Raspbian and is designed especially for
the language) but we dont require any of its many features
for this introduction, so just a simple text editor will suffice.
Having found such a thing, enter the following listing:
#! /usr/bin/env python3
# My first Python 3 code.
# The # symbol denotes comments so anything you put here
doesnt matter
import datetime
def daysOld(birthday):
today = datetime.date.today()
ageDays = (today - birthday).days
return(ageDays)
if __name__ == __main__':
weekdays = [Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday]
birthyear = int(input(Enter your year of birth: ))
birthmonth = int(input(Enter your month of birth: ))
birthdate = int(input(Enter your date of birth: ))
bday = datetime.date(birthyear, birthmonth, birthdate)
dayBorn = weekdays[bday.weekday()]
print(Hello, you were born on a, dayBorn)
WorldMags.net
WorldMags.net
print(and are, daysOld(bday),days old.)
Save the file as ~/birthday.py (Python programs all have
the .py extension). Before we analyse the program, you can
dive straight in and see it in action. Almost, anyway first we
must make it executable by running the following command
from the terminal, then we can call it into action:
$ chmod +x ~/birthday.py
$ ~/birthday.py
If you entered everything correctly, then the program
works out how many days old you are and on which weekday
you were born. Due to leap years, this would be some pretty
fiddly calendrics if we had to work it out manually (though
John Conways Doomsday algorithm provides a neat trick).
As well as having a simple core language structure, Python
also has a huge number of modules (code libraries), which
also strive for simplicity. This example uses the datetime
module, which provides all manner of functions for working
with dates and times, oddly enough.
Well do a quick rundown of the code, because there are a
couple of things that we havent seen before. The first line just
tells Bash (or whatever shell you use) to execute our script
with Python 3. Next, after the comments, we import the
datetime module and then begin a new codeblock. The def
keyword is used to define a function weve used plenty of
functions in this tutorial, but now were making our own. Our
function takes one parameter, birthday, and uses the
datetime module to do all the hard work, returning the
number of days the user has been alive.
The strange if statement is one of Pythons uglier
constructions. The special variable __name__ takes on the
special value __main__ when the code is run from the
command line, as opposed to being import-ed into another
program or the interpreter. We have written a bona fide
Python module here, but when we import it, everything in the
if block is ignored.
The next four lines are similar to what weve come across
before. To keep things simple, we havent bothered with list
comprehension for the weekdays, and we trust the user to
provide sensible dates. You are free to remedy either of these
using your newfound skills.
The datetime module provides a special data type
datetime.date and our next line (using the input that we
have just harvested) sets our variable bday to one of this
species. Date objects have various methods, including
weekday(), which we use next. This returns an integer
between 0 and 6, with 0 corresponding to Monday and 6 to
Sunday, so using this as an index into our list weekdays in the
next line gives the required string. The final line calls our
function from inside the print() function. Besides methods
You might
run into this
apparently
spooky action at
a distance when
working with
lists. Its not the
work of Chthonic
demons, however,
its just how
Python rolls.
List comprehensions
Lists and the various constructs weve just
introduced can join forces to form one of
Pythons most powerful features: list
comprehensions. These are a great
demonstration of Pythons laconic concision.
Consider the following example:
>>> daysShort = [Mon, Tues, Wednes,
Thurs, Fri, Satur, Sun]
>>> days = [j + day for j in daysShort]
Very often, coders use short variable names,
commonly i, j and k, for ephemeral variables,
such as those which are used in loops or
WorldMags.net
WorldMags.net
Astro Pi and
Minecraft
We conclude our Python exposition on a recreational note,
combining Minecraft and space exploration.
Astro Pi sits snugly atop Terrestrial Pi, connecting via the GPIO pins.
WorldMags.net
WorldMags.net
The mission
On Thursday 19 November 2015, two Raspberry
Pis (one with the infrared filtered Pi-Noir camera
module, and one with the normal Pi-Cam) will
join astronaut Major Tim Peake aboard a Soyuz
rocket, blasting off from Baikonur cosmodrome
in (glorious nation of) Kazakhstan.
A nationwide coding competition was
launched for primary and secondary school
pupils, with the winners having Tim run their
code when he arrives at the International Space
Station. Space, industry and education sectors
Quick
tip
Were assuming
you use Python 3
throughout this
series. This project
still works with
Python 2, but you
may need to install
the Pillow imaging
library with sudo
pip install Pillow.
ap.set_pixels(question_mark)
It is probably more desirable to type this code into a text
editor and then import it into Python, rather than work in the
interpreter. So you need to put the first two lines from our E.T.
phone home snippet at the top of the program, so that our ap
object is correctly set up. You can then import it into the
interpreter or run it from the command line if you prefer. As
well as dealing with individual pixels, we can also load images
Space case. This carefully machined and very strong case (its made of 6063
grade aluminium, dont ya know) will house the Pi on its space odyssey.
WorldMags.net
WorldMags.net
directly on to the array by using the load_image() function as
follows:
ap.load_image("~/astro-pi-hat/examples/space_invader.png")
Bear in mind the low resolution here images with lots of
detail dont work very well, but its excellent for displaying
pixel art and the like. Also, the LEDs are only capable of
displaying 15-bit colour depth (five bits for red, six for blue,
and five for green), so colours are dithered accordingly.
We can also query the Astro Pis many inputs using the
self-explanatory functions get_humidity(), get_
temperature() and get_pressure(). The gyroscope,
accelerometer and magnetometer are all integrated into a
so-called Inertial Measurement Unit (IMU). We wont be doing
any inertial measuring, but you can find all the relevant API
calls in the documentation at https://github.com/astro-pi/
astro-pi-hat/blob/master/docs/index.md. Youll also find
some great examples in the ~/astro-pi/examples directory,
which show off everything from joystick input to displaying a
rainbow pattern. Run these with, for example:
$ cd ~/astro-pi-hat
$ sudo python3 rainbow.py
Sword-wielding
Steve stands
ominously close
to a USB port.
Something
about knives
and toasters...
WorldMags.net
WorldMags.net
dimensions: the button toggles whether it moves in the
horizontal or vertical plane.
Bring TNT
to life with
the magic of
the Minecraft
Python API.
This probably
isnt a safe
place to stand.
for j in range(5):
xedge_lo = x - 5 + j
xedge_hi = xedge_lo + 10 - 2 * j
zedge_lo = z - 5 + j
zedge_hi = zedge_lo + 10 - 2 * j
mc.setBlocks(xedge_lo, y+ j + 2, zedge_lo,
xedge_hi, y + j + 2, zedge_hi, 46, 1)
The magic is all in that final [code] 1 [/code].
This sets the TNT to be live, so that if you leftclick it with your sword (or anything), then it
begins to flash and pulsate ominously. At this
point, you ought to migrate to a safe distance.
Also, for the older, single-core Pis, the resulting
chain reaction will be very taxing.
WorldMags.net
WorldMags.net
Sorting lists
faster in Python
We havent got time for this, its time to sort out that most
fundamental of algorithm classes: sorting (sort of).
W
Quick
tip
If youre using
a new version
of Matplotlib
with Gnome, you
might find that
the animation
described in the
box displays only
a blank window.
A workaround
is to add pylab.
pause(0.001) after
each draw() call.
WorldMags.net
WorldMags.net
minor details here, but that will all be sorted out when we
write our actual code.
SelectionSort(list):
for each position j in list
find minimum element after position j
if minimum element is smaller than element at j
swap them
Selection Sort is a fairly simple algorithm, but in
simplicity lies beauty. The first loop iteration simply finds the
smallest element in our list and places it, rightfully, at the
beginning. We then start at the second item and see whether
there is a smaller one further down the list, swapping if so.
And so it goes on, until the work is done. Note that we swap
elements here rather than insert them, this is so that we are
only modifying two of our list items; inserting an item would
require the whole list to be shunted down between the
insertion and deletion points, which in the worst case would
mean modifying every item in the list a costly procedure if
said list is long. We met for loops way back on page 50, and
we can tell that SelectionSort will loop over items in the list
at least once. But in each loop iteration we must find a
minimum element, which for the first few loops (where j is
small) involves checking most of the list items individually. We
can approximate how long an algorithm takes to run by
looking at these details. In Selection Sorts worst case we
will pretty much have two loops one nested inside the other,
each going over our whole list. We use so-called big O
notation to express the functions complexity as a function of
the input size. In our case, we suppose our list has n items
and the crude analysis above says that Selection Sort runs
with complexity O(n^2). This isnt saying that Selection
Sorting a list of one item will take one second (actually it
takes 0 seconds) or a list of five items will take 25 seconds.
What we can say is that the relationship between input size
and sorting time, as n gets large, will be less than some
constant (which depends on the machine) times n^2. This is
slightly disappointing news in laymans terms, Selection
Sort gets pretty slow pretty quickly.
Here is another naive sorting algorithm, which is called
Insertion Sort:
InsertSort(list):
for each position j in list
k=j
while number at position k is less then that at position
k-1
swap numbers at positions k and k - 1
k=k-1
If youre looking for an excellent resource for learning and debugging your
code, visit http://pythontutor.com. Here we can see Selection Sort in action.
Quick
tip
Very often, the
simple algorithms
outperform
Quicksort for small
lists. As such, you
can create a hybrid
sorting algorithm
which uses
Quicksort initially,
but as the list is
divided, reverts to
Selection Sort,
for example.
WorldMags.net
WorldMags.net
Elements
undergoing
Insertion Sort
trickle one at a
time from right
to left. Watch it
at night-time as
an alternative to
counting sheep.
Coding it up
Its time to translate our three algorithms into Python. We saw
briefly how to define functions in the prequel, start the block
with a def keyword, and then indent all the code that belongs
to that function. In our pseudocode we referred to our list as,
rather unimaginatively, list. Unfortunately, thats one of not
terribly many reserved words in Python, so well instead use l
(for llama). Selection sort then looks like:
def selectionsort(l):
for j in range(len(l)):
minelt = min(l[j :])
mindex = l.index(minelt)
if minelt < l[j]:
l[mindex] = l[j]
l[j] = minelt
For simplicity, were doing an inplace sort here weve
modified the original list so we dont need to return anything.
The Python code is quite different from the pseudocode, but
much of this is cosmetic. We use slicing to find the minimum
element on or after position j and then use the index()
method to find that minimums position in the list. The if
statement is actually superfluous here (weve left it in to
better match the pseudocode), because if the condition is
false (which would mean the minimum element was at
position j), then it would harmlessly swap a list item with itself.
Theres no explicit swap operation, so we have to do that
manually in the last two lines. Incidentally, these can be
further condensed to a single
WorldMags.net
WorldMags.net
Optional arguments and speed considerations
Because our Quicksort() function needs the
low and high parameters, the initial call looks
like quicksort(l, 0, len(a) - 1). Python does
allow default values for arguments to be
specified you simply suffix the argument with
an =. However, we cant use, for example, high =
len(l) because were not allowed to use internal
variables. The workaround would be to make the
first part of the function look like:
def quicksort(l, low = 0, high = None):
if high is None:
high = len(l)
You need some reasonably-sized lists to see
Quicksorts benefits we discovered that it
could sort 10,000 items in about 0.3 seconds,
whereas the others took much longer,
particularly Insertion Sort, which took about 14
seconds. Exact timing depends on the particular
list, of course, but its also worth noting that
(much) faster implementations of these
l[mindex],l[j] = l[j],l[mindex]
This means that the whole algorithm is done in four lines.
Swapping variables without this neat Pythonic trick would
involve setting up a temporary variable to hold one of the
values. Save this file as ~/sorting.py or similar, then start the
Python 3 interpreter in your home directory. You should be
able to import your module like so:
>>> import('sorting')
Assuming you didnt see any error messages or make any
typos, you can then apply your algorithm to a shuffled list.
Well use the random module to shuffle a list from 0 to 9 and
test this:
>>> import random
>>> l = list(range(10))
>>> random.shuffle(l)
>>> sorting.selectionsort(l)
>>> l
Voil a freshly sorted list. Of course, its nice to watch the
algorithm progress. So add a print(l) statement at the end of
the loop, with the same indentation so it is still within said
loop. To reload the module and apply the changes, we need to
use the importlib modules reload() function, the standard
import statement wont notice changes on already loaded
modules. Or you can just exit (Ctrl+D) and restart the
interpreter. The screenshot on page 103 shows the output for
the list [6, 9, 2, 4, 5, 3, 8, 7, 1, 0]. We can see the list becoming
sorted from left to right, and that the last stage of the loop
doesnt do anything.
Moving on to Insertion Sort, we translate the pseudocode
into Python code, which does not look drastically different.
Add this to the sorting.py file.
def insertionsort(l):
for j in range(1, len(l)):
k=j
while k > 0 and l[k - 1] > l[k]:
l[k - 1], l[k] = l[k], l[k - 1]
k -= 1
We start the outer loop at 1 so that the l[k-1] comparison
in the while statement doesnt break things. We also add k >
0 to the while condition because the loop should stop before
k gets to zero, rather than try (and fail) to retreat past the
beginning of the list. Weve used our swapping shortcut
from before and in the final line use the -= notation, which
is short for k = k - 1.
Again, add print statements to either the for or while
loops to see the algorithms progress. Smaller elements
are swapped to the beginning of the list one place at a time
until the list is sorted.
The Quicksort implementation is a bit more involved. For
the partition operation, were going to take the bold step of
just using the first element for the pivot, so we dont need to
Besides
animations,
sorting
algorithms can
be visualised as
wave diagrams.
Find out more
at http://
sortvis.org.
WorldMags.net
WorldMags.net
IT INSIGHTS FOR BUSINESS
www.techradarpro.com
twitter.com/techradarpro
facebook.com/techradar
WorldMags.net
WorldMags.net
THE EASY WAY TO
LEARN WINDOWS
100%
JARGON
FREE
WorldMags.net
Minecraft:
Start hacking
Use Python on your Pi to merrily meddle with Minecraft.
WorldMags.net
WorldMags.net
Now without further ado, lets make our first
Minecraftian modifications. Well start by
running an interactive Python session
alongside Minecraft, so open another tab in
LXTerminal, start Minecraft and enter a world
then Alt-Tab back to the terminal and open up
Python in the other tab. Do the following in the
Python tab:
import minecraft.minecraft as minecraft
import minecraft.block as block
mc = minecraft.Minecraft.create()
posVec = mc.player.getTilePos()
x = posVec.x
y = posVec.y
z = posVec.z
mc.postToChat(str(x)+ + str(y) + + str(z))
Behold, our location is emblazoned on the
screen for a few moments (if not, youve made
a mistake). These co-ordinates refer to the
current block that your character occupies, and
so have no decimal point. Comparing these
with the co-ordinates at the top-left, you will
see that these are just the result of rounding
down those decimals to integers (e.g. -1.1 is
rounded down to -2). Your characters
co-ordinates are available via mc.player.getPos(), so in
some ways getTilePos() is superfluous, but it saves three
float to int coercions so we may as well use it. The API has
a nice class called Vec3 for dealing with three-dimensional
vectors, such as our players position. It includes all the
standard vector operations such as addition and scalar
multiplication, as well as some other more exotic stuff that
will help us later on.
We can also get data on what our character is standing
on. Go back to your Python session and type:
curBlock = mc.getBlock(x, y - 1, z)
mc.postToChat(curBlock)
Here, getBlock() returns an integer specifying the block
type: 0 refers to air, 1 to stone, 2 to grass, and you can find all
the other block types in the file block.py in the ~/picraft/
minecraft folder we created earlier. We subtract 1 from the y
value since we are interested in whats going on underfoot
calling getBlock() on our current location should always
return 0, since otherwise we would be embedded inside
something solid or drowning.
As usual, running things in the Python interpreter is great
for playing around, but the grown up way to do things is to
put all your code into a file. Create the file ~/picraft/gps.py
with the following code.
import minecraft.minecraft as minecraft
import minecraft.block as block
mc = minecraft.Minecraft.create()
oldPos = minecraft.Vec3()
while True:
playerTilePos = mc.player.getTilePos()
if playerTilePos != oldPos:
oldPos = playerTilePos
x = playerTilePos.x
y = playerTilePos.y
z = playerTilePos.z
t = mc.getBlock(x, y 1, z)
mc.postToChat(str(x) + + str(y) + + str(z) + +
str(t))
Now fire up Minecraft, enter a world, then open up a
terminal and run your program:
$ python gps.py
The result should be that your co-ordinates and the
BlockType of what youre stood on are displayed as you move
about. Once youve memorized all the BlockTypes (joke),
Ctrl+C the Python program to quit.
We have covered some of the passive options of the API,
but these are only any fun when used in conjunction with the
more constructive (or destructive) options. Before we sign off,
well cover a couple of these. As before start Minecraft and a
Python session, import the Minecraft and block modules, and
set up the mc object:
posVec = mc.player.getTilePos()
x = posVec.x
y = posVec.y
z = posVec.z
for j in range(5):
for k in range(x - 5, x + 5)
mc.setBlock(k, j, z + 1, 246)
Behold! A 10x5 wall of glowing obsidian has been erected
adjacent to your current location. We can also destroy blocks
by turning them into air. So we can make a tiny tunnel in our
obsidian wall like so:
mc.setBlock(x, y, z + 1, 0)
Assuming of course that you didnt move since inputting the
previous code.
In the rest of this chapter, well see how to build and
destroy some serious structures, dabble with physics, rewrite
some of the laws thereof, and go a bit crazy within the
confines of our 256x256x256 world. Until then, try playing
with the mc.player.setPos() function. Teleporting is fun! Q
WorldMags.net
All manner
of improbable
structures can
be yours.
Quick
tip
Check out Martin
OHanlons
website www.
stuffaboutcode.
com, which
includes some
great examples of
just what the API is
capable of.
WorldMags.net
Minecraft: Image
wall importing
Have you ever wanted to reduce your pictures to 16 colour blocks?
You havent? Tough were going to tell you how regardless.
echnology has spoiled us with 32-bit colour, multimegapixel imagery. Remember all those blocky
sprites from days of yore, when one had to invoke
something called ones imagination in order to visualise what
those giant pixels represented? In this tutorial we hark back
to those halcyon days from the comfort of Minecraft-world, as
we show you how to import and display graphics using blocks
Standard setup
If youve used Minecraft: Pi Edition before
youll be familiar with the drill, but if not this is
how to install Minecraft and copy the API for
use in your code.
Were going to assume youre using Raspbian,
and that everything is up to date. You can
download Minecraft from http://pi.minecraft.
net, then open a terminal and unzip the file as
follows (assuming you downloaded it to your
home directory):
$ tar -xvzf ~/minecraft-pi-0.1.1.tar.gz
WorldMags.net
WorldMags.net
With just
16 colours,
Steve can
draw anything
he wants
(inaccurately).
WorldMags.net
WorldMags.net
then you may want to use more than 64 pixels in the
horizontal dimension and fix the height at 64. Replacing the
above block with just the two lines of the else clause would
achieve precisely this.
Now we convert our image to the RGB colourspace, so as
not to confuse the quantize() method with transparency
information, and then force upon it our woollen palette and
new dimensions. You might get better results by doing the
resize first and the quantization last, but we prefer to keep
our operations in lexicographical order.
mcImage = mcImage.convert("RGB")
mcImage = mcImage.quantize(palette = mcImagePal)
mcImage = mcImage.resize((rwidth,rheight))
For simplicity, we will position our image close to Steves
location, five blocks away and aligned in the x direction to be
precise. If Steve is close to the positive x edge of the world, or
if he is high on a hill, then parts of the image will sadly be lost.
Getting Steves coordinates is a simple task:
playerPos = mc.player.getPos()
x = playerPos.x
y = playerPos.y
z = playerPos.z
Then it is a simple question of looping over both
dimensions of the new image, using the slow but trusty
getpixel() method, to obtain an index into our palette, and
using the setBlocks() function to draw the appropriate
colour at the appropriate place.
If your image has an alpha channel then getpixel() will
return None for the transparent pixels and no block will be
drawn. To change this behaviour one could add an else
clause to draw a default background colour. Image
co-ordinates start with (0,0) in the top-left corner, so to
avoid drawing upside-down we subtract the iterating variable
k from rheight.
for j in range(rwidth):
for k in range(rheight):
pixel = mcImage.getpixel((j,k))
if pixel < 16:
mc.setBlock(j + x + 5, rheight - k + y, z, 35, pixel)
To do all the magic, start Minecraft and move Steve to
a position that befits your intended image. Then open a
terminal and run:
$ cd ~/mcimg
$ python mcimg.py
So that covers the code, but you can have a lot of fun by
expanding on this idea. A good start is probably to put the
contents of mcimg.py into a function. You might want to give
this function some arguments too. Something like the
following could be useful as it enables you to specify the
image file and the desired co-ordinates:
def drawImage(imgfile, x=None, y=None, z=None):
if x == None:
playerPos = mc.player.getPos()
x = playerPos.x
y = playerPos.y
z = playerPos.z
If no co-ordinates are specified, then the players position
is used. If you have a slight tendency towards destruction,
then you can use live TNT for the red pixels in your image.
Just replace the mc.setBlock line inside the drawing loop
with the following block:
if pixel == 14:
mc.setBlock(j + x + 5, rheight - k + y, z, 46, 1)
else:
mc.setBlock(j + x + 5, rheight - k + y, z,
mcPaletteBlocks[pixel])
If you dont like the resulting image, then its good news,
everyone it is highly unstable and a few careful clicks on the
TNT blocks will either make some holes in it or reduce it to
dust. It depends how red your original image was.
While Minecraft proper has a whole bunch of colourful
blocks, including five different types of wooden planks and
Unlike in Doom,
this strawberry/
cacodemon
doesnt spit
fireballs at you.
This is good.
WorldMags.net
WorldMags.net
Thats right,
Steve, go for
the ankles.
Lets see how
fast he runs
without those!
Block ID
Red
Green
Blue
Gold
41
241
234
81
Lapis Lazuli
22
36
61
126
Sandstone
24
209
201
152
Ice
79
118
165
244
Diamond
57
116
217
212
116,217,212
]
mcPaletteLength = len(mcPalette / 3)
then we can structure our lookup table as follows:
mcLookup = []
for j in range(16):
mcLookup.append((35,j))
mcLookup += [(41,0),(22,0),(24,0),(79,0),(57,0)]
Thus the list mcLookup comprises the blockType and
blockData for each colour in our palette. And we now have
a phenomenal 31.25% more colours [gamut out of here - Ed]
with which to play. To use this in the drawing loop, use the
following code inside the for loops:
pixel = mcImage.getpixel((j,k))
if pixel < mcPaletteLength:
bType = mcLookup[pixel][0]
bData = mcLookup[pixel][1]
mc.setBlock(j + x + 5, rheight - k + y, z, bType, bData)
In this manner you could add any blocks you like to your
palette, but be careful with the lava and water ones: their
pleasing orange and blue hues belie an inconvenient
tendency to turn into lava/waterfalls. Incidentally, lava and
water will combine to create obsidian. Cold, hard obsidian. Q
More dimensions
One of the earliest documentations of displaying
custom images in Minecraft:Pi Edition is Dav
Stotts excellent tutorial on displaying Ordnance
Survey maps, http://bit.ly/1lP20E5. Twodimensional images are all very well, but Steve
has a whole other axis to play with. To this end
the aforementioned Ordnance Survey team has
provided, for the full version of Minecraft, a
world comprising most of Great Britain, with
each block representing 50m. Its Danish
WorldMags.net
WorldMags.net
Build 2048
in Minecraft
Quick
tip
You could make
this project a bit
less of a Zork-esque
text adventure by
writing a control
system based on
player moves. Youd
want to reset the
players position
after each move,
but in principle
its quite a simple
modification.
WorldMags.net
WorldMags.net
Things turn out to be simpler if we split off the third
pseudocode block into its own function, newTile(). We first
need a list of co-ordinates of all the empty tiles; if theres
more than one then its certainly not Game Over, but even if
theres only a single space (which will get filled by a new tile)
there still may be possible moves. We use a couple of
functions from the random module to help decide the where
and what is of the new tile.
def newTile(self):
empties = []
self.game_over = True
for j in range(self.boardSize):
for k in range(self.boardSize):
if self.tiles[j][k] == 0:
empties.append([j,k])
Quick
tip
The board is
updated twice for
a successful move,
before and after
any tile merging.
If you were feeling
adventurous, you
could use this to
implement a two
step animation by
spacing out the
tiles somehow.
We were not feeling
adventurous.
if len(empties) > 1:
self.game_over = False
rnd_pos = random.choice(empties)
rnd_n = random.randint(1,2)
self.tiles[rnd_pos[0]][rnd_pos[1]] = rnd_n
self.drawBoard()
The drawBoard() function is what will actually place the
blocks in Minecraft world. Checking whether moves are
possible after the tile is added is a little awkward:
# check row neighbours
for j in range(self.boardSize):
for k in range(self.boardSize - 1):
if self.tiles[j][k] == self.tiles[j][k + 1]:
self.game_over = False
# check col neighbours
for j in range(self.boardSize):
for k in range(self.boardSize - 1):
if self.tiles[k][j] == self.tiles[k + 1][j]:
self.game_over = False
Get the
code!
You can find the
code at http://
www.linuxformat.
com/files/
mine2048.py.zip
or over at:
http://pastebin.
com/5zEZUfBS
WorldMags.net
It can be
frustrating,
and sometimes
Steves temper
gets the better
of him
Amazingly, thats the guts of the code all dealt with, but we
still need a means by which to input our moves, and of course
we need to render the results in Minecraft world.
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
Coding projects
Exciting and interesting coding
projects for you to write
Python 3.0 primer
Build a Gimp plug-in
Image filters
Sound filters
Twitter scanner
Build a GUI
120
124
128
134
138
142
..................................................
.........................................
.............................................................................
............................................................................
.............................................................
...................................................................................
WorldMags.net
WorldMags.net
Python: Dive
into version 3.0
Join us as we investigate what is probably one of the least loved and
disregarded sequels in the whole history of programming languages.
WorldMags.net
WorldMags.net
The Unicode revolution
Traditionally, text was encoded in ASCII, in which
each character is encoded as a 7-bit codepoint,
which gives you 128 characters to play with.
Some of these characters are invisible teletype
codes (ASCII originated in the 1960s), and once
we've counted the familiar alphanumeric
characters, there isn't really much room left.
Because we like things to be bytes, several
256-character extensions of the ASCII encoding
emerged. The most notorious of these is ISO8859-1, sometimes called Latin-1. This widelyused character set (and the related
Windows-1252) contains almost all the accents
required for the Latin-scripted languages, as well
Print in Python 3
A significant proportion of Python programs could be made
compatible with 3 just by changing the print syntax, but there
are many other, far less trivial, things that could go wrong. To
understand them, we must first be au fait with what really
changed in Python 3.
Most of the world doesn't speak English. In fact, most of
the world doesn't even use a Latin character set; even those
regions that do tend to use different sets of accents to
decorate the characters. As a result, besides the ASCII
standard, numerous diverse and incompatible character
encodings have emerged. Each grapheme (an abstraction of
a character) is assigned a codepoint, and each codepoint is
assigned a byte encoding, sometimes identically. In the past,
if you wanted to share a document with foreign characters in
it, then plain ASCII wouldn't help. You could use one of the
alternative encodings, if you knew the people you were
sharing it with could do the same, but in general you needed
to turn to a word processor with a particular font, which just
moves the problem elsewhere. Thankfully, we now have a
widely adopted standard: Unicode (see The Unicode
Revolution box, above) that covers all the bases, and is
backwards compatible with ASCII and (as far as codepoints
are concerned) its Latin-1 extension. We can even have
Unicode in our domain names, although internally these are
all still encoded as ASCII, via a system called Punycode.
Python 2 is far from devoid of Unicode support, but its
handling of it is done fairly superficially (Unicode strings are
sneakily re-encoded behind the scenes) and some third-party
modules still won't play nicely with it. Strings in Python 2 can
be of type str (which handles ASCII fine, but will behave
unpredictably for codepoints above 127) or they can be of
type unicode. Strings of type str are stored as bytes and,
The PyStone benchmark will likely be slower in Python 3, but the same wont
be true for all code. Dont be a Py3k refusenik without first trying your code.
>>> type(pi)
<type 'unicode'>
>>> len(pi)
1
However, if we were to try this on a terminal without
Unicode support, things will go wrong. You can simulate such
a scenario by starting Python with:
$ LC_ALL=C python
Now when you try to print the lowercase character pi, you will
WorldMags.net
Quick
tip
Arch Linux is one
of few distributions
to use Python 3
by default, but it
can live happily
in tandem with
its predecessor
(available in the
python2 package).
WorldMags.net
run into a UnicodeEncodeError. Essentially, Python is trying
and failing to coerce this to an ASCII character (the only type
supported by the primitive C locale). Python 2 also tries to
perform this coercion (regardless of current locale settings)
when printing to a file or a pipe, so don't use the unicode type
for these operations, instead use str.
The str type in Python 2 is really just a list of bytes
corresponding to how the string is encoded on the machine.
This is what you should use if you're writing your strings to
disk or sending them over a network or to a pipe. Python 2
will try and convert strings of type unicode to ASCII (its
default encoding) in these situations, which could result in
tears. So we can also get a funky pi character by using its
UTF-8 byte representation directly. There are rules for
converting Unicode codepoints to UTF-8 (or UTF-16) bytes,
but it will suffice to simply accept that the pi character
encodes to the two bytes CF 80 in UTF-8. We can escape
these with an \x notation in order to make Python
understand bytes:
>>> strpi = '\xCF\x80'
>>> type(strpi)
<type 'str'>
>>> len(strpi)
2
WorldMags.net
WorldMags.net
The popular
matplotlib
module has
been Python
3 compatible
since v1.2, for all
your graphing
and plotting
requirements.
WorldMags.net
WorldMags.net
Python: Code
a Gimp plugin
Use Python to add some extra features to the favourite open source imagemanipulation app, without even a word about Gimp masks.
ultitude of innuendoes aside, Gimp enables you to
extend its functionality by writing your own plugins.
If you wanted to be hardcore, then you would write
the plugins in C using the libgimp libraries, but that can be
pretty off-putting or rage-inducing. Mercifully, there exist
softcore APIs to libgimp so you can instead code the plugin
of your dreams in Gimps own Script-Fu language (based on
Scheme), Tcl, Perl or Python. This tutorial will deal with the
last in this list, which is probably most accessible of all these
languages, so even if you have no prior coding experience you
should still get something out of it.
Get started
On Linux, most packages will ensure that all the required
Python gubbins get installed alongside Gimp; your Windows
and Mac friends will have these included as standard since
version 2.8. You can check everything is ready by starting up
Gimp and checking for the Python-Fu entry in the Filters
menu. If its not there, youll need to check your installation. If
it is there, then go ahead and click on it. If all goes to plan this
should open up an expectant-looking console window, with a
prompt (>>>) hungry for your input. Everything that Gimp
You can customise lines and splodges to your hearts content, though frankly
doing this is unlikely to produce anything particularly useful.
WorldMags.net
WorldMags.net
pdb.gimp_context_set_foreground('#00a000')
pdb.gimp_context_set_brush_size(128)
pdb.gimp_paintbrush_default(layer,2,[160,100])
If you have the brush called Cell 01 available, then the
above code will draw a green splodge in the middle of your
canvas. If you dont, then youll get an error message. You can
get a list of all the brushes available to you by calling pdb.
gimp_brushes_get_list(). The paintbrush tool is more
suited to these fancy brushes than the hard-edged pencil,
and if you look in the procedure browser at the function
gimp_paintbrush, you will see that you can configure
gradients and fades too. For simplicity, we have just used the
defaults/current settings here. Download the code pack from
http://bit.ly/TMS12code and you will find a file linesplodge.
py which will register this a fully-fledged Gimp plugin, replete
with a few tweaks.
For the rest of the tutorial we will describe a slightly more
advanced plugin for creating bokeh effects in your own
pictures. Bokeh derives from a Japanese word meaning blur
or haze, and in photography refers to the out-of-focus effects
caused by light sources outside of the depth of field. It often
results in uniformly coloured, blurred, disc-shaped artefacts
Applying our
bokeh plugin
has created
a pleasing
bokeh effect in
the highlights.
WorldMags.net
WorldMags.net
Quick
tip
For many, many
more home-brewed
plugins, check out
the Gimp Plugin
Registry at http://
registry.gimp.org
Bring a bucket
To draw our appropriately-coloured disc on the bokeh layer,
we start somewhat counter-intuitively by drawing a black
disc. Rather than use the paintbrush tool, which would rely on
all possible users having consistent brush sets, we will make
our circle by bucket filling a circular selection. The selection is
achieved like so:
pdb.gimp_image_select_ellipse(timg, CHANNEL_OP_
REPLACE, x - radius, y - radius, diameter, diameter)
There are a few constants that refer to various Gimp-specific
modes and other arcana. They are easily identified by their
shouty case. Here the second argument stands for the
WorldMags.net
WorldMags.net
number 2, but also to the fact that the current selection
should be replaced by the specified elliptical one.
The dimensions are specified by giving the top left corner
of the box that encloses the ellipse and the said boxs width.
We feather this selection by two pixels, just to take the edge
off, and then set the foreground colour to black. Then we
bucket fill this new selection in Behind mode so as not to
interfere with any other discs on the layer:
pdb.gimp_selection_feather(timg, 2)
pdb.gimp_context_set_foreground('#000000')
pdb.gimp_edit_bucket_fill_full(bokeh_layer, 0,BEHIND_
MODE,100,0,False,True,0,0,0)
And now the reason for using black: we are going to draw
the discs in additive colour mode. This means that regions of
overlapping discs will get brighter, in a manner which vaguely
resembles what goes on in photography. The trouble is,
additive colour doesnt really do anything on transparency,
so we black it up first, and then all the black is undone by our
new additive disc.
pdb.gimp_context_set_foreground(color)
pdb.gimp_edit_bucket_fill_full(bokeh_layer, 0,ADDITION_
MODE,100,0,False,True,0,0,0)
Once weve drawn all our discs in this way, we do a
Gaussian blur if requested on our copied layer. We said
that part of the image should stay in focus; you may want to
work on this layer later so that it is less opaque at regions of
interest. We deselect everything before we do the fill, since
otherwise we would just blur our most-recently drawn disc.
if blur > 0:
pdb.plug_in_gauss_iir2(timg, blur_layer, blur, blur)
Softly, softly
Finally we apply our hue and lightness adjustments, and set
the bokeh layer to Soft-Light mode, so that lower layers are
illuminated beneath the discs. And just in case any black
survived the bucket fill, we use the Color-To-Alpha plugin to
squash it out.
pdb.gimp_hue_saturation(bokeh_layer, 0, 0, lightness,
saturation)
pdb.gimp_layer_set_mode(bokeh_layer, SOFTLIGHT_
MODE)
pdb.plug_in_colortoalpha(timg, bokeh_layer, '#000000')
And that just about summarises the guts of our script. You
will see from the code on the disc that there is a little bit of
housekeeping to take care of, namely grouping the whole
series of operations into a single undoable one, and restoring
After we apply
the filter, things
get a bit blurry.
Changing the
opacity of the
layer will bring
back some detail.
params, results,
function) # myplugin
main()
The proc_name parameter specifies what
your plugin will be called in the PDB; python_fu
is actually automatically prepended so that all
Python plugins have their own branch in the
taxonomy. The menupath parameter specifies
what kind of plugin youre registering, and where
your plugin will appear in the Gimp menu: in our
case <Image>/Filters/Artistic/LineSplodge...
would suffice. imagetypes specifies what kind
of images the plugin works on, such as RGB*,
GRAY*, or simply if it doesnt operate on any
image, such as in our example. The list params
WorldMags.net
WorldMags.net
Pillow: Edit
images easily
You can save time and effort by using Python to tweak your images in bulk.
WorldMags.net
WorldMags.net
argument, which is why weve wrapped the reduced
dimensions in an init() call.
You can similarly change the orientation of the image by
rotating it by a specified degree with the rotate method. The
rotate method takes a single argument (either integer or
float), which is the number of degrees to rotate the image by
and, just like resize , returns a new Image object.
>>> im.rotate(180).show()
>>> im.rotate(230.5).show()
>>> im.rotate(90).save(OnItsSide.png)
The first statement rotates the image by 180 degrees, the
second by 230.5 degrees, and the third by 90 degrees. Weve
saved ourselves some effort by calling show() directly in the
first two statements to view the image, and save() in the third
to save the rotated image. Note that if the image is rotated to
any angle besides 90 or 270, Pillow maintains its original
dimensions. When rotated at these two angles, the image
swaps its width and height. You can also create a mirror
version of the image, using the transpose method:
>>> im.transpose(Image.FLIP_LEFT_RIGHT).show()
>>> im.transpose(Image.FLIP_TOP_BOTTOM).show()
Also, just like the previous example, replace the call to
the show method with the save method, together with the
name of a new image file, to save the flipped image. Another
common trick is to create thumbnails:
>>> size=(128,128)
>>> im.thumbnail(size)
>>> im.show()
>>> im.save(thumbnail.png)
We start by defining a tuple with the dimensions of the
thumbnail, and then use it to create the thumbnail in place.
Remember that unlike the previous methods, thumbnail
changes the original image itself.
In addition to the more common image-editing
techniques, Pillow also includes a number of methods and
modules that can be used to enhance your images. To begin
with, lets take a look at the ImageFilter module, which
contains several enhancement filters. To use this module, you
will first have to import it with:
>>> from PIL import ImageFilter
You can now use the various filters of the ImageFilter, such
as blur to apply a Gaussian blur effect to the image:
>>> im.filter(ImageFilter.BLUR).show()
>>> im.filter(ImageFilter.BLUR).save(blurred.jpg)
Dressing up images
Begin by importing the ImageEnhance module with:
>>> from PIL import ImageEnhance
You can now use it to, for example, increase the brightness
by 20% with:
>>> ImageEnhance.Brightness(im).enhance(1.9).show()
Or, perhaps reduce contrast and save the results:
>>> newImage=ImageEnhance.Contrast(im).enhance(-1.5)
>>> newImage.save(reducedContrast.jpg)
Yet another common image-editing task is cropping
portions of an image. Pillows crop method can help you
describe the coordinates of the image youre interested in
and discard the rest, such as:
>>> width, height = im.size
>>> left = int(width/4)
>>> top = int(height/4)
>>> right = int(3 * width/4)
>>> bottom = int(3 * height/4)
>>> im.crop((left,top,right,bottom)).show()
WorldMags.net
WorldMags.net
>>> im.crop((left,top,right,bottom)).save(croppedImage.png)
The crop method takes a tuple and returns an Image
object of the cropped image. The above statements help cut
out the outside edges of the image, leaving you with the
centre of the original image. The cropped images dimensions
end up being half of the original images dimensions. For
example, if the image youre cropping is 100 x 100, the left
and top variables will both be set to 25, and the right and
bottom variables to 75. Thus, you end up with a 50 x 50
image, which is the exact centre of the original image. The
int() wrapper makes sure the returned values of the
dimensions are integers and not floats.
Then theres the paste method, which as you have
probably guessed, pastes an image on top of another one.
Remember, however, that the paste method doesnt return
an Image object, and instead modifies the image in place. So
its best to make a copy of your image and then call paste on
that copy:
>>> im=Image.open(screenshot.png)
>>> copyIM=im.copy()
Now that we have a copy of our screenshot, well first crop
a smaller bit from the image, just like we did in the previous
section:
>>> width, height = im.size
>>> left = int(width/4)
>>> top = int(height/4)
>>> right = int(3 * width/4)
>>> bottom = int(3 * height/4)
>>> croppedIM=copyIM.crop((left,top,right,bottom))
The cropped thumbnail image is in croppedIM, which
were going to paste over the copied image:
>>> copyIM.paste(croppedIM, (400,500)).show()
>>> copyIM.paste(croppedIM, (200,0)).show()
>>> copyIM.save(pastedIM.png)
The paste method takes the X and Y coordinates as the
two arguments thatll determine the location of the top-left
corner of the image being pasted (croppedIM) into the main
image (copyIM). After pasting the thumbnails twice at
different locations, we save the image as pastedIM.png.
Add a watermark
Tkinter is a
wonderful toolkit
to quickly cook up
a lightweight user
interface for your
Python scripts.
WorldMags.net
WorldMags.net
is covered in detail on page 142, so we wont go into details
about it here. To start constructing our basic image
manipulation app, import the required libraries:
import tkinter as tk
import tkinter.filedialog
import PIL
from PIL import Image, ImageTk, ImageOps, ImageEnhance,
ImageFilter
import numpy as np
Besides Tkinter and PIL, weve imported the NumPy
package for its superior arithmetic skills. Now lets create the
main class thatll initialise the graphical interface:
class ImageEditor(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.initUI()
Flip to the Tkinter tutorial on page 142 for a detailed
explanation of the methods and functions weve used here.
The initUI() function will draw the actual interface:
def initUI(self):
self.parent.title(Simple Photo Editor)
self.pack(fill = tk.BOTH, expand = 1)
self.label1 = tk.Label(self, border = 25)
self.label1.grid(row = 1, column = 1)
menubar = tk.Menu(self.parent)
filemenu = tk.Menu(menubar, tearoff=0)
The above code gives the app window a title and then uses
the Pack geometry manager that helps pack widgets in rows
and columns. The fill option makes sure the widgets fill the
entire space by expanding both horizontally and vertically.
The expand option is used to ask the manager to assign
additional space to the widget box. This will help us resize the
window to accommodate larger images. We then create a
pull-down menu:
filemenu.add_command(label=Open, command= self.
onOpen)
filemenu.add_command(label=Save)
filemenu.add_command(label=Close)
filemenu.add_separator()
filemenu.add_command(label=Exit, command=self.quit)
menubar.add_cascade(label=File, menu=filemenu)
editmenu = tk.Menu(menubar, tearoff=0)
editmenu.add_command(label=Rotate, command=self.
onRot)
editmenu.add_command(label=Trace Contours,
command=self.onCont)
menubar.add_cascade(label=Simple Mods,
menu=editmenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label=Help Index)
helpmenu.add_command(label=About...)
menubar.add_cascade(label=Help, menu=helpmenu)
self.parent.config(menu = menubar)
The add_command method adds a menu item to the
menu, add_seperator adds a separator line, and the add_
cascade method creates a hierarchical menu by associating
a given menu to a parent menu. Normally, Tkinter menus can
be torn off. Since we dont want that feature, well turn it off by
setting tearoff=0 . We can add menu options as we expand
our app. Each menu option has a command= option, which
WorldMags.net
WorldMags.net
iPAD
PRO
Worth the
upgrade?
http://goo.gl/fuXZt
JOIN US ON TWITTER: @iPadUserMag
WorldMags.net
WorldMags.net
ON
SALE
NOW!
WorldMags.net
PyAudio: Detect
and save audio
Discover how Python can be used to record and play back sound.
Capture audio
Once youve imported the libraries, you can quickly cobble
together the code to open the PyAudio recording stream.
WorldMags.net
WorldMags.net
data = stream.read(chunk)
frames.append(data)
We first print a line of text that notifies the start of the
recording, and then define a list variable named frames . The
for loop handles the task of recording the audio. It brings in
the data in correctly sized chunks and saves it into a list
named frames . The for loop helps determines how many
samples to record given the rate, the number of bytes a
recording should have, and the recording duration. In our
case, the loop makes 430 iterations and saves the captured
audio data in the frames list. We will now close the audio
recording stream:
print ( Done recording audio. )
stream.stop_stream()
stream.close()
p.terminate()
The above code first terminates the audio recording, then
closes the stream, and finally terminates the PyAudio session.
Now that we have the audio data as a string of bytes in the
frames list, well transform it and save it as a WAV file using
the wave module:
wf = wave.open(output_filename, wb)
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(format))
wf.setframerate(rate)
wf.writeframes(b .join(frames))
wf.close()
This is where the data is actually written to an audio file.
Python opens the file in the write-only mode, as defined by
the wave module. This is followed by a bunch of wave write
objects, such as setnchannels, which sets the number of
channels (mono in our case), and setframerate, which sets
the frame rate (44.1kHz in our case). The setsamwidth object
sets the sample width and is derived from the format using
PyAudios get_sample_size() function. Finally, the
b .join() method combines the bytes in a list, which in our
case is named frames, before closing the file.
If you run the program, youll first notice a string of
warnings. You can safely ignore these because they are the
result of PyAudios verbose interactions with the audio
hardware on your computer. What you should watch out for
are any Python exceptions that point to actual errors in the
code and must be ironed out. After executing the program,
youll have a 10-second WAV file named Monoaudio.wav in
the current directory.
While the above code will work flawlessly and helps
demonstrate the ease of use of the PyAudio library, this isnt
how youd program it in the real world. In a real project, youd
You can safely ignore all messages (except Exception errors) that appear
whenever you run a PyAudio script its simply the library discovering the
audio hardware on your machine.
break up the tasks into various smaller tasks and write a class
for each. In such a case, the code that begins the execution
would look something like:
if __name__ == __main__:
rec = Recorder(channels=1)
with rec.open(capture.wav, wb) as recfile:
recfile.record(duration=10.0)
We discuss Pythons if __name__ == __main__ trick for
writing reusable code in the Tweepy tutorial (see page 138).
The above code is executed when the program is run directly.
Now, when the with statement is executed, Python
evaluates the expression, and calls the magic __enter__
method on the resulting value (which is called a context
guard), and assigns whatever __enter__ returns to the
variable given by as . Python then executes the code body
and, irrespective of what happens in that code, it calls the
guard objects __exit__ method. The __enter__ and
__exit__ methods make it easy to build code that needs
some setup and shutdown code to be executed for
example, to close the file in our case:
def __exit__(self, exception, value, traceback):
self.close()
The Recorder class opens a recording stream and defines
its parameters:
class Recorder(object):
def __init__(self, channels=1, rate=44100, frames=1024):
self.channels = channels
self.rate = rate
WorldMags.net
WorldMags.net
self.frames_per_buffer = frames
def open(self, fname, mode=wb):
return RecordingFile (fname, mode, self.channels, self.rate,
self.frames)
The class defines the necessary parameters and then
passes them to another class, named RecordingFile, which
defines the record function that opens the stream, captures
the audio, and then writes the files using the for loop, as
described earlier.
Vocalise recordings
You can easily adapt the code to play back the file. In addition
to pyaudio and wave, well import the sys module for
accessing system-specific functions and parameters.
import pyaudio
import wave
import sys
For more
accurate results,
calculate the RMS
value for a group
of sound frames
to decide what
level is considered
to be silence.
if len(sys.argv) < 2:
print( The script plays the provided wav file.\n
Proper usage: %s filename.wav % sys.
argv[0])
sys.exit(-1)
The script will play any provided WAV file. If one isnt
provided, the above code comes into action and prints the
proper usage information before exiting.
wf = wave.open(sys.argv[1], rb)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.
getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
If a filename is provided, its opened in read-only mode
and its contents are transferred to a variable named wf. Then
the PyAudio library is initialised. Unlike the open() function
we used earlier to define the parameters of the stream, when
reading a WAV file, well use functions to extract details from
the file. For instance, the getsampwidth method fetches the
sample width of the file in bytes. Similarly, the getnchannels
method returns the number of audio channels, and
getframerate returns the sampling frequency of the wave
audio file.
# read data
data = wf.readframes(1024)
# play stream (3)
while len(data) > 0:
stream.write(data)
data = wf.readframes(1024)
Once we have all the details from the file, we extract the
frames in chunks of 1,024 bytes. As long as there are chunks
of data, the while loop writes the current frame of audio
data to the stream. When all the frames have been streamed,
well then terminate the stream and close PyAudio with:
stream.stop_stream()
stream.close()
p.terminate()
Auto recorder
In the scripts above, weve seen how to use PyAudio to record
from the microphone for a predefined number of seconds.
With a little bit of wizardry, you can also train PyAudio to start
recording as soon as it detects a voice, and keep capturing
the audio until we stop talking and the input stream goes
silent once again.
To do this, well initiate the script as usual with the
if __name__ == __main__': trick to print a message and then
call the function to record the audio, such as:
print(Start talking into the mic)
record_to_file(capture.wav)
print(Alrighty, were done. Recorded to capture.wav)
The record_to_file function receives the name of the file
to record the sound data to, and contains the code to record
data from the microphone into the file after setting up the
stream parameters. It also calls the record() function, which
WorldMags.net
WorldMags.net
Websites such
as Program
Creek help you
find sample
code for Python
modules like
PyAudio.
WorldMags.net
WorldMags.net
Tweepy: Create
Twitter trends
With some Python know-how, you can tap into the collective consciousness
and extract valuable information from it.
strings listed alongside API Key, API Secret, Access Token and
Access Token Secret.
Youre now all set to access Twitter with Python. To access
the tweets, you need to import the following libraries in your
Python script:
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
Next you need to paste in the API and Access tokens, and
use these to connect to Twitter, such as:
consumer_key = ENTER YOUR API KEY
consumer_secret = ENTER YOUR API SECRET
access_token = ENTER YOUR ACCESS TOKEN
access_secret = ENTER YOUR ACCESS TOKEN SECRET
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
Quick
tip
Scroll through
Tweepys
documentation
(http://docs.
tweepy.org/
en/latest/api.
html) for a list of
supported methods.
api = tweepy.API(auth)
From this point forward, we can use the api variable for
interacting with Twitter. For example, you can read your own
time with the following loop:
for status in tweepy.Cursor(api.user_timeline).items():
print(status.text)
The code will print all status updates in groups of 20 from
your own timeline. We use Tweepys Cursor interface, which
supports several different types of objects. Here we have
used the items method to iterate through our timeline. To
limit the number of tweets you wish to print, you can also
change the statement to something like tweepy.Cursor(api.
user_timeline).items(10) , which will only print the 10 most
recent tweets.
Capture tweets
As you can see, interacting with Twitter via Python is
reasonably straightforward. Twitter also offers a set of
streaming APIs, however, and by using these, you can access
Twitters global stream of tweets. The public streams work
really well for following specific topics and mining data. Well
import and modify Tweepys StreamListener class to fetch
the tweets.
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
WorldMags.net
WorldMags.net
auth = tweepy.OAuthHandler(consumer_key, consumer_
secret)
auth.set_access_token(access_token,access_secret)
api = tweepy.API(auth)
class StdOutListener(StreamListener):
def on_status(self, data):
print(data)
return True
def on_error(self, status):
print(status)
return False
The Twitter streaming API is used to download Twitter
messages in real time. It is useful for obtaining a high volume
of tweets, or for creating a live feed using a site stream or user
stream. Tweepy establishes a streaming session and routes
messages to the StreamListener instance.
Here we begin by creating a class that inherits from
StreamListener. StreamListener has several methods. Of
these, on_data() , on_status() and on_error() are the most
useful ones. The on_data() method handles replies to
statuses, warnings, direct messages and so on, and passes
data from status to the on_status() method.
When youre interacting with Twitter via its API, you have
to be mindful of rate limiting. If you exceed the limited
number of attempts to connect to the streaming API within a
window of time, the connect will error out. Repeated attempts
will only aggravate Twitter and it will exponentially increase
your wait time. To avoid such a situation, use Tweepys on_
error() method to print the error messages and terminate
execution.
listener = StdOutListener()
twitterStream = Stream(auth, listener)
Once weve authenticated with Twitter, we can now start
the stream listener. The modified StreamListener class is
used to create a listener instance. This contains the
information about what to do with the data once it comes
back from the Twitter API call.
if __name__ == __main__:
twitterStream.filter(track=[oggcamp])
Here we use our stream class to filter the Twitter stream to
fetch only tweets that contain the word oggcamp. The track
parameter is an array of search terms to stream. We place
Anatomy of a tweet
The 140 characters of the tweet that are visible
on your timeline are like the tip of the iceberg. A
complete tweet in its JSON form contains a lot
more information and attributes in addition to
the tweet itself. A knowledge of the important
ones will help you extract the relevant data.
The JSON string begins with the created_at
field, which lists the UTC time when the tweet
was created. It is followed by the id and the
id_str fields, which are the integer and string
WorldMags.net
WorldMags.net
various parameters. For example, twitterStream.filter
(location=[51.5073509,-0.12775829999998223], track=[linux])
will fetch any tweets that originate in London and contain the
word linux. Take a look at Twitters official API documentation
(https://dev.twitter.com/streaming/overview/requestparameters) for a list of all the supported streaming request
parameters.
The StreamListener will remain open and fetch tweets
based on the specified criteria until you ask it to stop by
terminating the script. The large blobs of text on your screen
are the tweets fetched by the streamlistener in the JSON
format (see box below).
WorldMags.net
class StdOutlistener(StreamListener):
def on_data(self, data):
all_data = json.loads(data)
if text in all_data:
tweet = all_data[text]
username = all_data[user][screen_name]
Once weve extracted the information from a
tweet in the stream, we can write these to the
table, and also print the values on the screen.
db.execute(INSERT INTO tweets (username,
tweet) VALUES (%s,%s),(username, tweet))
connect.commit()
print((username,tweet))
return True
WorldMags.net
tweets_hash = {}
for tweet_line in tweets_file:
if tweet_line.strip():
tweet = json.loads(tweet_line)
if entities in tweet.keys():
hashtags = tweet[entities][hashtags]
for ht in hashtags:
if ht != None:
if ht[text].encode(utf-8) in tweets_hash.keys():
tweets_hash[ht[text].encode(utf-8)] += 1
else:
tweets_hash[ht[text].encode(utf-8)] = 1
The above might look like a mouthful but its actually really
simple. To start with, the JSON file is received as the
tweetsFile variable, which is then read by the tweets_file
variable. We then define a dictionary variable named
tweets_hash that will house the hashtags and their
occurrence frequencies. We then initiate a loop for every
individual tweet in the file. Well first convert the JSON string
into a dictionary object, and save it in a variable named
tweet . If the tweet has a tag named entities, well extract the
hashtag value. Next, well check whether the hashtag is
already listed in our dictionary. In case it is, well just
print(\nHashtag - Occurrence\n)
for count,value in sorted(sortedHashTags.items(),
key=lambda kv: (kv[1],kv[0]),reverse=True):
print(#%s - %d times % (count.decode(utf-8), value))
Youll need to familiarise yourself with data structures and
dictionaries in Python to make sense of the above code. Each
key is separated from its value by a colon (:), the items are
separated by commas, and the whole thing is enclosed in
curly braces. Keys are unique within a dictionary. To access
dictionary elements, you can use the familiar square brackets
along with the key to obtain its value.
The above code comes into play after we have gone
through the entire JSON file and scavenged data from all the
tweets. The code block will filter the top 10 tweets based on
the descending order of their occurrence, and print the
results. The complete code for this tutorial is available in our
GitHub repository at: https://github.com/geekybodhi/
techmadesimple-tweepy. Q
WorldMags.net
WorldMags.net
Tkinter: Make a
basic calculator
Why not use Pythons default graphical toolkit to build a simple yet
easily extensible visual calculator?
Heres a more practical example that creates a window with
a title and two widgets, one of which quits the application
when clicked:
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.hi = tk.Label(self)
self.hi[text] = Hello World
self.hi.pack(side=top)
self.EXIT = tk.Button(self, text=EXIT, fg=red,
command=root.destroy)
self.EXIT.pack(side=bottom)
Quick
tip
The code in this
tutorial is based on
vegaseats Updated
Tiny Tkinter
Calculator, listed
on DaniWeb.com
(http://tinyurl.
com/h3eotvf).
root = tk.Tk()
app = App(master=root)
app.master.title(Sample Application)
app.master.geometry(250x70+550+150)
app.mainloop()
Well explain the individual elements of the code as we
build our calculator. For now, its important to note that weve
explicitly created an instance of Tk with root = tk.Tk() .
Tkinter starts a Tcl/Tk interpreter behind the scenes, which
then translates Tkinter commands into Tcl/Tk commands.
The main window of an application and this interpreter are
intrinsically linked, and both are required in order for a Tkinter
application to work. Creating an instance of Tk initialises
this interpreter and creates the root window. If you dont
explicitly initialise it, one will be implicitly created when you
create your first widget. While this is perfectly fine, it goes
against the spirit of Python, which states that explicit is
better than implicit.
Graphical calculator
As you can see, it doesnt take much effort to drape a Python
app with a graphical interface. Well use the same principles
to write our graphical calculator. Well add various widgets to
our calculator, including an editable display, which can be
used to correct mistakes and to enter symbols and
hexadecimals not on our calculators keypad. For example, if
WorldMags.net
WorldMags.net
Tkinter makes
it relatively
easy to build a
basic graphical
calculator
in Python.
you enter 0xAA, itll print the decimal equivalent, that is 170.
Similarly, the editable display can also be used to manually
type in functions that arent represented on the keypad but
are defined in Pythons math module, such as the logarithmic
and trigonometric functions. Furthermore, our calculator will
also have the ability to temporarily store and retrieve a result
from its memory bank.
The complete code for the calculator is available online.
Lets dissect individual elements to get a better grasp of the
Tkinter library and its interactions with Python.
import tkinter as tk
from math import *
from functools import partial
class Calculator(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
We begin by importing the various libraries and functions
for our calculator. Besides the Tkinter toolkit, well also need
the math library to handle the calculations. The partial
function from the functools module helps us write reusable
code. Well explain how weve used it later in the tutorial.
After importing the libraries, we begin defining the
Calculator class. To begin with, we initialise the instance
WorldMags.net
WorldMags.net
calculator. Arranging widgets on the screen includes
determining the size and position of the various components.
Widgets can provide size and alignment information to
geometry managers, but the geometry manager always has
the final say on positioning and size.
self.title(Simple Demo Calculator)
self.geometry(350x140+550+150)
self.memory = 0
self.create_widgets()
While Tkinter supports three geometry managers
namely, Grid, Pack and Place well use the Place manager,
which is the simplest of the three and allows precise
positioning of widgets within or relative to another window.
We use the geometry function to define the size and
position of the calculators window it accepts a single string
as the argument in the format: width x height + xoffset +
yoffset . The real string doesnt contain any spaces, which
have been added here for easier readability. You can also just
set the position of the window by only defining the x and y
offset coordinates.
def create_widgets(self):
btn_list = [
7, 8, 9, *, C,
4, 5, 6, /, Mem >,
1, 2, 3, -, > Mem,
0, ., =, +, Neg ]
r=1
c=0
for label in btn_list:
tk.Button(self, text=label, width=5, relief=ridge,
command=partial(self.calculate, label)).grid(row=r, column=c)
c += 1
if c > 4:
c=0
r += 1
Once weve defined the look of the calculator window, the
above code defines and creates the individual calculator
buttons. The create_widgets() method contains code to
create the layout for the calculator. The btn_list array lists
the buttons youll find in a typical calculator keypad. They are
listed in the order theyll appear on the keypad.
Next we create all buttons with a for loop. The r and c
variables are used for row and column grid values. Within the
loop, well first create a button using the Button function.
The Button widget is a standard Tkinter widget used to
implement various kinds of buttons. Buttons can contain text
or images, and you can associate a Python function or
method with each button. When the button is pressed,
Tkinter automatically calls that function or method.
In our code, we implement a plain button, which is pretty
straightforward to use. All you have to do is to specify the
button contents (text from the array, in our case) and what
WorldMags.net
WorldMags.net
WorldMags.net
Thanks to
Tkinters
extensibility, you
can add more
functions to your
calculator so
its capable of
more advanced
calculations.
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
Build essential programming
skills from the ground up
Everyone can code with these plain-English
guides youll learn core programming
concepts and create code to be proud of!
148
pages of tips,
tricks and
tutorials
9001
9000
WorldMags.net