Coding Made Simple 2016 PDF
Coding Made Simple 2016 PDF
NE
W
!
Learn how to program the easy and fun way
LEARN
HOW TO
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
WorldMags.net
EDITORIAL TEAM
CONTRIBUTORS
EDITOR Les Pounder, Mayank Sharma,
Neil Mohr Graham Morrison, Ben Everard,
ART EDITOR Mike Saunders, Jonni Bidwell
Efrain Hernandez-Mendoza EDITOR-IN-CHIEF
Graham Barlow IMAGES
ThinkStock, Future Photo Studio
'VUVSF1VCMJTIJOH-JNJUFE DPNQBOZOVNCFS
JTSFHJTUFSFEJO&OHMBOEBOE8BMFT3FHJTUFSFEPGmDF3FHJTUFSFEPGmDF2VBZ)PVTF
5IF"NCVSZ
#BUI
#"6"
All information contained in this publication is for information only and is, as far as we are aware, correct at the time of going to press. Future cannot accept any responsibility for
errors or inaccuracies in such information. You are advised to contact manufacturers and retailers directly with regard to the price and other details of products or services referred
to in this publication. Apps and websites mentioned in this publication are not under our control. We are not responsible for their contents or any changes or updates to them.
If you submit unsolicited material to us, you automatically grant Future a licence to publish your submission in whole or in part in all editions of the magazine,
including licensed editions worldwide and in any physical or digital format throughout the world. Any material you submit is sent at your risk and, although every
care is taken, neither Future nor its employees, agents or subcontractors shall be liable for loss or damage.
Future plc is a public Chief executive ;JMMBI#ZOH5IPSOF We are committed to using only magazine paper
company quoted Non-executive chairman Peter Allen XIJDI JT EFSJWFE GSPN XFMM NBOBHFE
DFSUJmFE
on the London &KLHIQDQFLDORIFHU1FOOZ-BELJO#SBOE forestry and chlorine-free manufacture. Future
4UPDL&YDIBOHF Managing director, Magazines Joe McEvoy
TZNCPM'653
Publishing and its paper suppliers have been
www.futureplc.com 5FM
JOEFQFOEFOUMZ DFSUJmFE JO BDDPSEBODF XJUI UIF
SVMFTPGUIF'4$ 'PSFTU4UFXBSETIJQ$PVODJM
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. pushing coding to the fore of education.
With the internet So no matter if youre looking to relive
driving a new world of those heady 80s coding days or are a
information exchange, newbie looking to take your first steps into
business start-ups and the coding world, you hold in your hands
online gaming, coders the ideal guide to start coding. Thanks to
have suddenly become a new generation of open free software,
the gatekeepers to we all can access operating systems,
these new realms. Combine the huge development tools, compilers and the
interest in learning how to create and programming languages needed to create
control these worlds with the surge of professional programs, apps and tools.
cool devices, such as the Raspberry Pi, Well show you how to get up and running
and youve got a head of technological with a Linux system, then access
steam the world hasnt seen since the everything you need freely online.
coding craze of the early 1980s. Coding is easy, exciting and fun. Well
Back then, it was more Code Britannia explain the basics, move on to more
than Cool Britannia jump forward to advanced topics, explain how you can use
today, and Britain is again firmly at the the Raspberry Pi, and provide you with
heart of a web and coding revolution. A exciting and easy-to-follow projects. So
Brit invented the web, a Brit designed what are you waiting for? Get coding!
the Raspberry Pi, and Britain is firmly Neil Mohr, Editor
Made Simple books are designed Break instructions down into Teach you new skills you can take
to get you up and running quickly easy-to-follow steps so you wont with you through your life and apply
with a new piece of hardware or be left scratching your head over at home or even in the workplace
software. We wont bombard you what to do next
with jargon or gloss over basic Make it easy for you to take our
principles, but we will Help you discover exciting new advice everywhere with you by
things to do and try exploring giving you a free digital edition of
Explain everything in plain English new technology should be fun and this book you can download and take
so you can tackle your new device or our guides are designed to make with you on your tablet, smartphone
software with confidence and really the learning journey as enjoyable as or laptop see page 146 for more
get to know how to use it possible for you details on this offer
How are we doing? Email [email protected] and let us know if weve lived up to our promises!
Contents
Get coding
Get started with Linux .................................................................................................... 10
Get coding in Linux Mint ......................................................................................... 16
Join the Coding Academy .................................................................................... 19
How to use an IDE ..................................................................................................................... 28
Coding basics
Lists ..................................................................................................................................................................................32
Functions and objects .................................................................................................... 34
Conditionals .............................................................................................................................................. 36
Variables ............................................................................................................................................................. 38
Program structure ................................................................................................................. 40
Recursion ........................................................................................................................................................ 44
Sorting algorithms .................................................................................................................. 46
Integers ................................................................................................................................................................. 48
Using loops .................................................................................................................................................. 50
Compilers ........................................................................................................................................................ 52
Common mistakes ............................................................................................................... 54
Modules 74
.................................................................................................................................................................
Persistence 76
...................................................................................................................................................
Databases 78
.......................................................................................................................................................
Raspberry Pi
Getting started 82
..................................................................................................................................
Starting Scratch 84
............................................................................................................................
Further Scratch 88
...............................................................................................................................
Python on the Pi 94
...........................................................................................................................
Coding projects
Python 3.0 primer .............................................................................................................. 120
Build a Gimp plug-in ..................................................................................................... 124
Image filters .......................................................................................................................................... 128
Sound filters ........................................................................................................................................ 134
Twitter scanner ......................................................................................................................... 138
Build a GUI ............................................................................................................................................... 142
WorldMags.net
WorldMags.net
Get coding!
Everything you need to
start coding today
Get started with Linux ........................................ 10
Get coding in Linux Mint ............................. 16
Join the Coding Academy ........................ 19
How to use an IDE........................................................ 28
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!
A
s you read through this Coding with many languages built in or ready to install you can get hold of Mint and then get it up and
Made Simple bookazine, youll from a central server. No scouting round running on your PC.
notice that most of the screens dodgy sites; just run a command or fire up a Its not scary, it wont damage your PC and
dont look like Microsoft software centre to get what you need. you dont even have to install anything if you
Windows or Apple Mac OS X. Theres a Thats the beauty of Linux its built by dont want to. If you currently have a Windows
good reason for that: theyre not. Just as geeks for geeks to do geeky things. Once PC, there are three options: run Linux in
there are different car VirtualBox, run it off a DVD or
manufacturers or makes of USB drive, or install it on your
TV, theres more than one Its not scary, it wont damage PC dual-booting with Windows.
operating system that can
run your PC or Mac. Its just
your PC and you dont even Were only going to look at
the first two as theyre the least
that Linux happens to be
free because its developed
have to install anything. likely to cause any damage. The
walkthrough opposite explains
by thousands of coders around the world. youre used to its slightly different interface how to run Mint within VirtualBox on top of
All the coding projects in this bookazine are and way of working, youll find how easy it Windows (or Mac OS X), while the next
based on someone running Linux on their really is to get along with. We recommend that walkthrough shows you how to create a live
desktop. You dont have to the code works you use a version (known as a distro) called image that you can boot and run from either a
on Windows or Mac OS X but Linux comes Linux Mint, and were going to look at the ways DVD or a USB flash drive.
I
f youve never tried Linux, we think youll developers. In many ways, the key weakness of
be surprised at how easy it is to use, and
how good a development platform it
Linux has been its lack of ease of use (Mint is
an exception to that rule) but then, when its
Top tip
makes. You should also know that Linux designed and created by developers, ease of Whats a distro?
runs the majority of the internet, from its use is going to come at the bottom of the to-do Unlike Windows and Mac OS X, because
core servers to websites, alongside powering list. The upshot of this history is that theres a Linux is free software, anyone can take it and
97% of the worlds supercomputers, and wealth of the most advanced tools freely effectively create their own OS to distribute.
being used widely in science and industry. available on all Linux platforms. As long as In the Linux world, these are called distros for
So its not such a bad thing to understand, youre willing to seek out help yourself and short, and there are literally hundreds out
after all. contribute back to the community, youll find there not all good, not all maintained, but
No matter which tutorial you follow, youll a very welcoming and rich development hundreds nonetheless.
need a copy of the Linux Mint Cinnamon distro. ecosystem waiting for you.
Head to www.linuxmint.com/download.php programs has always been from a central
and download the 32-bit build, unless you Getting apps repository of software, protected and
know your system is 64-bit, in which case get With Windows, in the past youve been used to maintained by the distros creators.
that. If you have an older or slower machine, getting programs by downloading them from This is one of the reasons why Linux has
opt for the MATE edition, which is less here, there and everywhere. More recently, the remained so secure (its not infallible) but
graphically demanding. introduction of the Windows Store has at least people downloading dodgy software is a key
centralised where software comes from and way that machines become infected. With
Up and running removed the worry of getting infected by Linux Mint, theres the Software Center, which
A big reason why Linux makes such a good viruses and malware. The fact is that with gives you access to hundreds of programs and
development platform is that it was created by Linux, the key way of getting new tools and all the programming development tools you
could wish for. Its not why were here but you called the Terminal. This can be called a a Terminal and use the ls command to list a
can also download Valves Steam gaming client number of different things, depending on the directory, and cd to change directory.
and take advantage of over 1,700 Linux games system, such as the command line, command
it has to offer. prompt or command-line interface. It is a direct Linux isnt Windows
interface to the operating system and all of its One thing to keep in mind when you first use
Drivers tools, which you access through text Linux is that its not Windows or Mac OS X.
Were not really here to talk about using Linux commands. Going back to the early days of This largely means things youre used to in
in every detail but there are a few standard computers and Linux, as computers were so Windows wont work the same in Linux, or be
questions that often crop up when people much slower, there werent any graphical in the same place, nor does it offer the same
move over from Windows. One key one is interfaces, so computers were controlled set of programs. So big commercial products
where are all the drivers? The cool thing with entirely through text commands. such as Microsoft Office, Adobe
Linux is that, on the whole, theres no need to Linux was developed originally in this type Photoshop and development
worry about drivers theyre built into the of environment, so all of its core tools are tools such as Microsoft Visual
Linux kernel. That isnt to say you cant add based on Terminal use. Many of them or all Studio arent directly made for
drivers, but theyre generally not required. the ones youll care about do have graphical Linux you can run them via a
There are a couple of exceptions: certain more interfaces these days. The fact is, the Terminal system called Wine but the
obscure laptop wireless cards can cause remains an efficient way of controlling Linux, Linux open-source community
issues, while if you want maximum 3D gaming and when it comes to troubleshooting, it offers has created its own tools
performance, you need to install the dedicated a consistent set of tools and interfaces to such as LibreOffice, Gimp,
graphics driver from your cards manufacturer. resolve problems. Krita and a whole range of
You dont need to know anything about the freely available and
The Terminal Terminal to use Linux on a day-to-day basis, open-source products
If youve heard of Linux, then one area you but its good to know that its there just in case. that offer the
might fear or just wonder about is a thing However, we would advise you to at least open same capabilities.
&
On iOroSid!
And
M
ints Cinnamon desktop (unlike Ubuntus Unity) is We shall begin this tutorial by opening the Terminal
cosmetically quite similar to Windows: theres a program from the menu. We find ourselves confronted with
menu in the bottom-right; open windows will appear an arcane prompt of the form
in the taskbar; theres a system tray area to which diverse user@host:~$
applets may be added; right-clicking on things gives the usual and a rather menacing flashing cursor. The ~ is shorthand
context menus; and so on. Perusing the aforementioned for our home directory, which lives at /home/user/ in the
menu, you will find a plethora of pre-installed software, Linux directory hierarchy. The $ shows that we do not have
including (but not limited to) the LibreOffice suite, the Firefox root access, which limits the amount of damage we can do.
web browser, VLC for playing movies, Gimp for image Root is the superuser in Linux and, as such, access to the
manipulation and the Transmission BitTorrent client. Besides account is limited. On a standard Linux Mint installation, root
navigating the program groups in the Places menu, you can access is granted through the sudo command (take a look
quickly access things by typing a few characters into the at https://xkcd.com/149/), which temporarily elevates our
livesearch box. privileges. If things were arranged differently, we would see
The idea of working at the command line sends shivers that a root shell has not a $ in its prompt, but a # . Let us
down the spines of many people, not least because it stirs up ignore our hungry cursor no more, and start doing some
long-repressed memories of darker times, viz. MS-DOS. But command line-fu.
dont worry, its a very powerful way to work, and thanks to If we want to see a listing of all the files in the current
tab-completion, you wont have to painstakingly transcribe directory, we use the ls command. So if we type that in and
lengthy hit Enter, well see
commands or that weve got
pathnames. Much Pretty much anything you might directories for
of your day-to-day
computing can be
be used to doing in a GUI can be Documents,
Downloads, Music
done at the done from the command line. and a few others.
terminal. File We can access the
management, text directory one level
editing and music playing can all be done with not one single up through the .. operator. So if we do
click. In fact, pretty much anything you might be used to $ ls ..
doing in a GUI can be done or at least instigated (for we will see a listing of the directory /home/ in which well see
example, playing a video) from the command line. our users directory, together with the directories of any other
users that are on the system (of which there probably none).
If required, we can get more information by using the -l
(long) option:
$ ls -l
This shows us file metadata such as permissions, ownership
information, last modified date and file size.
We can navigate the directory hierarchy using the cd
command. For example, to jump to the very top of the
filesystem and see what the view is like from up there, you
should issue:
$ cd /
$ ls
We see all kinds of tersely named directories, but we
recognise /home/ from being there a couple of sentences
ago. The /etc/ directory houses configuration files, which
advanced users enjoy manipulating, but apart from that,
LibreOffice Writer is a more than adequate word processor for almost theres generally no reason to do anything with the filesystem
everybodys needs. Its compatible with Microsoft Word files. outside of our home directory its all taken care of by Mints
package manager. That said, its fun to explore and familiarise get confused with actual programming directives or variable
yourself with how Linux does its filing. For example, the /dev/ names. Since strings can contain spaces, its easy to see how
directory contains device nodes representing all the hardware such confusion may arise. There isnt a reasonable notion for
attached to your system. Disk partitions get names such as subtracting or dividing by strings, so attempting to do that
sda1 or sdb2, and the primary video card lives in /dev/dri/ results in an error. Programmers encounter diverse error
card0. This is quite a radical idea to digest, but on Linux, messages as a matter of course, so you may as well try that
everything is a file. now. Your first ever type error congratulations! Exit
For a more thorough look at the command line, see the the interpreter by pressing Ctrl+D or typing exit() . The
five-minute introduction at http://community.linuxmint. interpreter is good for trying out code snippets but its no use
com/tutorial/view/100. Were going to focus our attention for proper coding. Were going to create our first Python
towards Python programming now. We can return to our program and run it, all from the command line.
home directory at any time simply by calling cd with no All programming begins with simple text files, and we can
arguments. So lets do that to start with, and then create a use the humble nano editor to create one. Lets navigate to
new directory in which to store our Pythonic labours. our newly created directory and create our Python script:
$ cd ~ $ cd ~/python
$ mkdir python $ nano helloworld.py
Mint currently uses the older Python 2.7 by default but its Python programs all use the .py extension, and our
worth adopting the newer 3.x series, unless youre morally particular program, as is tradition in such matters, is going to
opposed. So start the Python 3 interpreter by typing: issue a worldly greeting. The nano text editor is pretty easy to
$ python3 work with you can press Ctrl+X at any time to exit (youll be
We are presented with some version information, terse prompted to save any changes). There are some helpful
guidance and a different prompt: >>>. This enables us to shortcuts displayed on the bottom for searching (Ctrl+W),
enter Python commands and have the results immediately saving (Ctrl+O) and the like. If youve never used a terminal-
returned to us. Its known as a Read-Evaluate Print Loop based text editor, then you might dismayed to learn that the
(REPL). For example, we can use it as we would a standard mouse cant be used as you might expect here. It is possible
calculator to perform basic arithmetic: to select text and paste it using the middle mouse button, but
>>> 32 * 63 positioning of the cursor is done with and only with the
2016 cursor keys.
>>> 9 ** 2 Getting Python to print messages to the terminal is easy
81 we use the print() function. Type the following into nano:
>>> 1 / 3 print(Hello world)
0.3333333333333333 As before, weve had to enclose our string in quotes. Now
Weve put spaces around each operator, which is not commence departure from nano with Ctrl+X, press Y to save
necessary but does improve readability. And readable code is and then press Return to use our existing file. Back at the
A Good Thing. But its not just numbers that we can add up command line, the moment of truth lets see whether
we can do it with strings, too. We call them strings because Python can run our program:
they are strings of characters, but that doesnt stop them $ python3 helloworld.py
being added or multiplied: If all goes to plan, a friendly greeting should be displayed. If
>>> hello * 3 that didnt happen, welcome to the world of programming, in
hellohellohello which careless typing is punished. Rinse, lather and repeat
>>> hello there, + friend the editing and running stages until it works. Then settle down
hello there, friend with a cup of tea, satisfied in the knowledge that you wrote a
Notice that we enclose strings in quotes. This is so they dont program and lived to tell the tale. Q
OUT
NOW!
C
oding is the new cool. If you knowledge is more than useful its an time to pick up, theres also no cost and, as a
dont know how to Python, all essential skill, particularly now that coding is FLOSS user, access to every language under
your friends are going to be being taught in schools. the sun is just waiting an apt-get away.
laughing at you Knowing a bit of code helps with using So grab our hand and lets take just a few
Were not being flippant. Knowing how Linux itself, helps you get more out of the minutes to create a fun game in Python, learn
to code is almost an essential for the open- terminal, can open doors to a new career or which languages are right for you and your
source fiend. Be it basic Bash scripting or help you troubleshoot why that webpage projects, then see how you can tackle the new
knowing how to read a bit of PHP, coding wont work correctly. Other than taking a bit of school curriculum and web development.
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 There are all kinds of Pygame-powered games. This one, You Only Get One Match, features
after the first line, the prompt changes to lots of fireworks but limited means of ignition. Check it out at http://bit.ly/LXF202-onematch.
because the interpreter knows that a
discrete codeblock is coming and the line(s) >>> for count in range(5): isolation, returns a list consisting of a range of
following the for construct belong to it. print('iteration #', count) integers. Well cover lists in just a moment, but
Such codeblocks need to be indented, Theres a few things going on here. We have for now we just need to know that range(5)
usually using four spaces, though you can introduced a new variable, an integer by the looks like [0, 1, 2, 3, 4], which you can verify in
use as many as you like so long as youre name of count . Giving sensible names to your the interpreter. So our variable count is going
consistent. If you dont indent the second variables is a good idea; in this case, it implies to iterate over each of these values, with the
line, Python shouts at you. Entering a blank (even in the absence of any other coding print() line being issued five times once for
line after the print statement ends the knowledge) that some counting is about to each value in the range. Another type of loop is
codeblock and causes our loop to run. happen. The range() function, when used in the while loop. Rather than iterating over a
Code Clubs
There are also over 2,000 volunteer-run code an access-for-all gateway to coding free from as Bahrain and Iceland. The project materials
clubs across the UK. Code Club, armed with commercial interest and corporate control. have already been translated into 14 languages,
100,000 courtesy of Google, provides the The Code Club phenomenon is spreading with more on the way. You can find out more at
material, and schools (or other kind venues) worldwide, too. Youll now find them as far afield www.codeclub.org.uk.
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
techradar.com
WorldMags.net
WorldMags.net
Get started
with IDEs
Speed up your programming workflow by using Geany, a lightweight
integrated development environment.
T
he cycle of editing, running and debugging even The grown-up way to begin Python files is with a directive
simple programs can rapidly become painful. So known as a shebang. This is a line that tells the shell how it
imagine how ugly things can get when working on a ought to be run. So our Python 3 code should begin:
huge project involving complex build processes. This is why #!/usr/bin/env python3
integrated development environments (IDEs) exist so that The env program is a standard Linux tool that is always
the editing, compiling, linking and debugging can all take accessible from the /usr/bin/ directory. It keeps track of
place on a unified platform (and you have a single point at where various programs are installed, because this can differ
which to direct programming rage). Some IDEs are tailored wildly from distro to distro. On Mint, our shebang could
for a particular language, such as IDLE for Python (see page equally well call /usr/bin/python3 directly.
92). Were going to look at a more general purpose tool called Now well change up our program slightly. Were going to
Geany. Geany is pretty basic as far as IDEs go in fact, by show how variables can be used as placeholders, and in doing
some measures, its more of a text editor on steroids than an so make our greeting a little more customisable. Edit the code
IDE, but it is more than sufficient for this tutorial. (keeping the shebang above, unless you have some personal
Youll find Geany in Mints repositories, so you can install it grievance) so that it looks like this:
either from the Software application or by doing: name = Dave
$ sudo apt-get update print(Hello , name)
$ sudo apt-get Note the space
install geany after Hello ,
Now start
Geany from the
Variables allow us to abstract otherwise the
output would look a
Places menu.
Theres a quick
away specifics and deal with bit funny. We have
declared a variable
run-through of the quantities that, well, vary. called name
interfaces major containing the string
features opposite. We can start by opening up our helloworld. Dave . We can test our code by pushing F5 or clicking the
py program from before: Select File > Open and then navigate Run button (see annotation). You probably knew what was
to our python/ directory to select the file. going to happen, and you probably realise that the same
effect could be achieved just by changing world to Dave
in the original program but there is something much more
subtle going on here. Variables allow us to abstract away
specifics and deal with quantities that, well, vary. We can
elaborate on this concept a little more by introducing a
new function, input() , which takes a line of user input and
returns that string to our program. Change the first line to
name = input()
and run your code. In the terminal window, type your name, or
whatever characters you can muster, and press Return. The
program took your input and courteously threw it back at you.
The input() function can also be given a string argument,
which will be used to prompt the user for input. Our print()
function above does our prompting for us, so we have no
need for this.
Before we go further, its worth talking about tabs and
spaces. Python is terribly fussy about indentation, and even if
it wasnt, you still should be. Correctly indenting code makes
it easy to demarcate code blocks lines of code
that collectively form a function or loop so we can see,
Before you start, you need to install Geany from Mints repositories. for example, which level we are at in a nested loop. A tab
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.
represents a fixed amount of white space decided by the text greeting is issued. We can put as many lines as we want in
editor, but just because an editor renders a tab character as this clause so long as they respect the indentation, Python
eight spaces, that doesnt make it equivalent to pushing the knows that they all should be run when the condition is met.
space bar eight times. Python happily works with files that The else block is run when the condition is not met, in this
use tabs or spaces, so long as theyre consistent. case, the program behaves exactly as it had done before.
The reason Python is fussier than other languages about Note that strings are case-sensitive, so someone called
these matters is because indentation levels actually form part agamemnon would not be recognised as an old acquaintance
of the code. So just adding a random indent to your code is of our program. As well as testing for equality, we can also use
likely to break it. We need some more coding concepts at our the greater than ( > ) and less than ( < ) operators. The len()
disposal to illustrate this. Lets start with the if conditional. function returns the length of a string, so we could instead
Suppose we want our program to behave differently when make our if statement:
someone named, say, Agamemnon uses it. Replace the last if len(name) > 9:
line of our code with the following block, noting that Geany print(My what a long name you have)
automatically indents your code after the if and else Now anyone whose name is 10 characters or more gets
statements. recognition. Of course, the else block from above can be
if name == Agamemnon: included, too, if that behaviour is still desired.
print(Ah, my old friend...) There are many other IDEs besides Geany. If you want to
else: check out something more advanced, see PyCharm or
print(Hello , name) perhaps the PyDev plugin for Eclipse. Or maybe youre
It can be hard to get used to the autotabbing, but it saves happier with a couple of terminals open you wouldnt be the
you considerable effort. The code weve just introduced is first. Also, dont forget IDLE, which is packaged with Python
highly readable we use the == operator to test for equality we cover it in the Raspberry Pi chapter on page 92. However
and use a colon to indicate a new code block is starting. If the you choose to do it, though, we wish you good luck with your
condition is met (ie Agamemnon is visiting), then a special continued adventures in Linux and coding. Q
WorldMags.net
WorldMags.net
Coding basics
Now you have the tools, its time
to grasp the basics of coding
Lists
......................................................................................................................32
Functions and objects ........................................ 34
Conditionals .................................................................................. 36
Variables ................................................................................................. 38
Program structure ..................................................... 40
Recursion ............................................................................................ 44
Sorting algorithms ..................................................... 46
Integers ..................................................................................................... 48
Using loops ...................................................................................... 50
Compilers ............................................................................................ 52
Common mistakes ................................................... 54
POP PUSH
A QUEUE
With a queue,
the values you
put on the list
first are the
1 2 3 4 5
first out (FIFO).
>>> shoppinglist
[apples, baked beans, bread, milk, soup]
If you can follow the logic of that simple piece of code,
2 then its safe to assume youve grasped the logic of lists.
Theres really nothing more to it, and now we hope youre
wondering what all the fuss is about. The main problem is
that it can get complicated quickly, and because lists are
shorthand for so many different data types, it can be difficult
1 working out what might be happening in any one chunk of
code. This is especially true of Python because it has so
many convenience functions, unless you know what they do,
With a stack, the values you put youre unlikely to grasp the logic. But at their heart, theyre
on the list last are the first out (LIFO).
just a chain of values, and thats the best place to start. Q
O
nce upon a time, programs were just big lists of and therefore its a good thing to get right in the early days of
instructions, and these instructions could do your programming journey.
anything with variables and the contents of memory. Lets start with a bit of Python code. Try this:
This was fine for simple, low-level programs but as time went def myfunc():
on and people starting making bigger and more complicated x = 10
programs, the idea that one part of the program could do print x
anything with memory became less appealing.
For instance, you might have a program that controls
a robotic arm, and stores the state of the arm in a variable x=1
called X. Your program calls lots of different subroutines, print x
some of which may have been programmed by other people. myfunc()
How do you know that those routines arent messing about print x
with the RAM where X is stored? If youre totally new to Python: the def bit and the
The results could be disastrous, especially when the arm following two lines of code are a function which we call later.
starts punching people instead of stroking puppies as Program execution begins with the x = 1 line. So, we create a
originally envisaged. variable called x and assign it the number 1. We print it out to
This is where the concept of variable scope comes into confirm that. We then call a function which sets x to 10, and
play. In programming, scope defines how a variable is visible prints it. As control jumps back to the main chunk of our
to the rest of the program. In most high-level languages, we code, we print x again and its back to 1. How did that
Most well- can control the scope of a variable that is, we can choose happen? Didnt we just set x to 10 in the function?
documented whether it should be visible to the current chunk of code, to Well, yes, but the function had its own copy of the variable.
languages have the current source code file, or absolutely everywhere in all of It didnt do anything with the x variable that was declared
explanations
the programs files. outside of it. The function lives happily on its own, and doesnt
of variable
This level of control is absolutely fundamental to good want to interfere with data it doesnt know about. This is
scope, so once
youve got the program design, so that multiple programmers can work essential to keeping code maintainable imagine if all
basics from this together on a project, implementing their own routines variables were accessible everywhere, and you were writing a
article, you can without accidentally trampling over one anothers data. Its routine to be dropped inside a 50,000-line software project.
explore specific good for modularisation and keeping data safe and secure, Youd be totally terrified of choosing variable names that
implementations. 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
Remember to initialise!
By default, most C compilers dont dont blindly use variables that
set newly-created automatic havent been initialised with some
variables to zero. This is especially kind of value!
true in the case of automatic Now, why dont compilers just set
variables, because they might be the variable to zero? That would
created somewhere in RAM that was make everything a lot safer, right?
used previously by other data. Well, yes, but its extra work for the
So if you have a program like this: CPU and adds a performance hit.
#include <stdio.h> The compiler allocates space for
variables in RAM that may have been
int main() used for other data before, so theres
{ no guarantee that the RAM is zeroed-
int x; out.
printf(x is %d\n, x); Setting that chunk of RAM to zero
} requires an extra CPU instruction,
then compile and run it, and then and GCC tries to be pretty efficient.
compile and run it again, the number You can tell GCC to warn about the
will probably be different each time use of uninitialised variables with the If you try to use automatic variables that havent been
you do. The moral of the story is: -Wall flag. given any value, prepare for some horrors.
O A N M
AT
O
R T T
WorldMags.net
E
GET FIT FAST IN 2016
WITH THE VERY BEST TECH
FOR RUNNING,
CYCLING AND MORE
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 weve been doing with previous tutorials a strategy known
function factorial(c)
4 3 2 1 1 2 6 24 if c > 1
else
return c * factorial(c-1)
return 1
the function finishes and returns the final calculation, the trtl.fd(length)
Python interpreter prints this out without us needing to do trtl.left(144)
anything else, which is why you see the results under this line. trtl.fd(length)
Congratulations youve solved the problem using recursion! trtl.left(144)
You should now be able to see how recursion can be used in trtl.fd(length)
all sorts of places that require an indeterminate amount of trtl.left(144)
repetition, such as many sorting algorithms, other trtl.fd(length)
mathematical solutions and filesystem searches. Even if you trtl.left(144)
cant think of a solution yourself, if you have an idea that trtl.fd(length)
something can be approached using recursion, then a simple trtl.left(144)
Google search will reveal whether it can and what the code, or return
pseudo code, should look like.
If you run the above piece of code, either from the
Generating fractals interpreter, by typing fractal(200), or by adding that
But if you want to visualise recursion in action, which is one of command to the last line in a script, youll see the Turtle
the quickest ways to understand the concept, theres one cursor first move forward before turning left 144 degrees and
particularly effective set of functions that can be used to drawing another line. It does this four more times to complete
illustrate both its advantages and its complexities, and these the star. To turn this into a fractal, we want to execute the
are fractals. Fractal algorithms use recursion to add infinite same function from within the function itself at the end of
levels of detail to whats a much simpler algorithm. Some of each limb, and this can be easily achieved by adding
the most famous were documented by the French fractal(length*0.3) after every trtl.fd(length) line. This will
mathematician Benot B Mandelbrot in his book, The Fractal call the fractal function again, only this time with a length
Geometry of Nature, but there are many easier algorithms value around a third smaller than the original. This star, too,
that can be created with just a few lines of code, which is what will launch other iterations, but the reason why this doesnt go
were going to do using Python. on for ever is because we first check whether the length of
Before we start, though, you will need to make sure you each line is going to be greater than 10. If it isnt, then we
have two dependencies installed. The first is Pythons Turtle return from the function, and stars will stop being indefinitely
graphics module, but it should be part of any default Python drawn. As with the factorial function, this means that all the
installation. We use this to draw and display lines with as little other part-completed stars can draw their respective limbs
code as possible. The Turtle module itself needs you to install until the main star itself has finished and the fractal is
tk-8.5 for its drawing routines, which means if your complete. With just a few lines of code, you end up with what
distribution doesnt add this automatically, youll have to could be an infinitely complex image. And thats the never
install it yourself. Using the Turtle graphics module means we ending beauty of recursion. Q
8 5 2 9
Bubble sort
5 8 2 9
works by
comparing
adjacent
elements in the
list. As it does
so, the largest
element bubbles
its way to the
end of the list.
Bubble sort
OK, so sorting a list two elements long isnt particularly Bubble sort
impressive. But we can extend the same technique to sort a is an algorithm
list that has an infinite number of elements this is known as When we add a third card, assuming that theyre in with O(n2)
bubble sort. reverse order, bubble sort has to do the most work possible; performance
The idea is that we loop over the entire list, comparing it has do two comparisons on each loop, and three loops. that means
(and swapping, if necessary) each set of adjacent elements In total thats six operations. it takes
in turn. The element with the greatest value will always end up What about if we add a fourth? It will take three increasingly
longer for every
on the right-hand side of the comparison, so will always be comparisons on each loop, and four loops. Thats 12
element added.
compared in the next pair of elements. Eventually, it will end operations this time. Five cards? Thats four comparisons and
up at the very end of the list, where it belongs. five loops: 20 operations.
Notice that, if we were to loop over the list only once, wed Theres a definite pattern here. The work done by bubble
get only the largest element in the correct position. The way sort seems to conform to the formula:
to get around this is to keep looping over the list; on each num. operations = num. loops x num. comparisons
loop, well encounter another largest element that always And we can see that the number of loops is always equal
ends up on the right-hand side of each comparison that is, to the number of elements in the list, and the number of
until it reaches the largest element from the previous comparisons is always equal to one less than the number of
iteration. At this point, it wont move any further, because its loops. This means that, if we have n elements, the amount of
now in the correct position. work done by bubble sort is:
Well know the list is sorted when we run a loop in which num. operations = n x (n - 1) = n(n - 1) = n2 - n
nothing gets swapped. This code shows how a bubble sort
might be implemented in Python: Big O
cards = [8, 3, 7, 4, 2, 1] What this means is that the amount of work bubble sort does
swapped = True increases in polynomial time for every item added to the list.
while swapped: #sort until swapped is False That is to say the increase in the amount of work becomes
swapped = False #assume nothing is swapped more and more rapid for every element added to the list. This
for i in range(len(cards) - 1): #loop entire list is why its so slow for large lists a 10-element list may take
cur = cards[i] only 90 operations, but a 1,000-element list will take 999,000
j=i+1 operations, and it will simply get worse for every card from
if cards[i] > cards[j]: there on.
cards[i] = cards[j] In the general case given above, the n2 term is always
cards[j] = cur going to be far larger than the n term, and hence will always
swapped = True #reset swapped to True if anything have a much larger influence on how well the algorithm
is swapped performs. Because of this, when discussing the performance
print cards of an algorithm, in computer science its only ever the largest
term thats considered.
Speed matters The terminology used for this is big O notation, and we
Its pretty clever, right? You can now use this program to sort say that bubble sort is a O(n2) algorithm. Bubble sort is
any size list and it will get the job done. However, if you try to actually considered to be one of the worst-performing sorting
use it on a large list, youll find that its painfully slow even algorithms, and its definitely not how Python does it. That
on a fast computer. said, dont discard it entirely. Its easy to implement, and
To understand why this is, lets think a little about how works all right for small lists on fast computers. There may be
much work our bubble sort algorithm has to do. When we had times when you just want to get the job done, and bubble sort
just two cards, bubble sort had to do two comparisons once makes the most sense here.
to get the cards in the correct place, and then again to check If you fancy looking at sorting algorithms that are much
that theres nothing else to sort. In total, it had to perform just faster, albeit harder to implement, jump to page 102, these
two operations. are much closer to how Python does it. Q
Y
ou wouldnt think that numbers would be a relevant represents a number thats double the bit to the right of it,
topic in the 21st century. After all, if theres one thing giving a sequence of bits the ability to represent every value
computers can do well, its count them. Billions of between zero and double the value of the most significant bit
times a second. But for better or worse, having some minus 1 (or 28-1). Thats a difficult couple of sentences to
knowledge of how computers deal with numbers, and the digest, so here are some examples:
systems used to process them, will help you to write better The positions within an 8-bit binary number have the
programs. And its not all for low-level stuff like assembler following values: 1, 2, 4, 8, 16, 32, 64 and 128. But rather than
either. Number systems are just as important for high-level assign them in that order, the most significant bit the one
languages. If you write a PHP script for the web, for example, holding the largest value is usually on the left. So, the binary
small changes in the way you process numbers within the number 10, or 00000010, is equivalent to 2. If we move that
script can have a huge impact on the performance of your bit to the left so that the 1 occupies the third slot along, the
servers. Your code total value
might be run doubles to 4, and
thousands of
times a second,
The position of each bit within a so on. If we
enable more than
making any small binary value represents a number one bit at a time,
discrepancy in then all the values
how those thats double the bit to the right are added
numbers are together, so that
calculated or stored have an impact on performance. 00000011 represents 3 (1+2) and 10000011 represents 131
Numbers are important because they engage the CPU at (128+2+1). If we enable every bit in an 8-bit binary number,
its lowest level, right down to hardware and design. Typically, a we get a maximum value of 255.
single operation of the CPU can deal only with a value held
within one of its internal registers. And that value can be as 2s compliment
If you want large only as the number of bits the CPU supports. These Consider the following question: what does 10101010
to see binary days, that means either 32 or 64 bits. But whats a bit? Its represent in 2s compliment? You might think, after the last
registers update literally a switch thats turned on or off a single bit of couple of paragraphs, that the binary value represented here
in real time, information, and this is normally represented by a 1 for on is 170 (2+8+32+128). But youd be wrong. The clue is the
KCalc has a and a 0 for off. Computers use a numeral system called reference to 2s compliment. This is, in fact, a scheme for
handy display
binary to store these on/off values within their registers and representing negative values in binary, and it changes the
mode for up to
memory. The position of each bit within a binary value meaning of the bits and what they represent. It does this by
64 bits of data.
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,
A
fter losing ourselves in the surprisingly complex problem by adding a loop in your code. In many ways, theyre
domain of using numbers in our code, were returning related to the idea of recursion. But where a loop or function
to a simpler concept. Its the idea of a loop, and how in recursion calls another instance of itself, usually to build a
programmers use them to solve problems. A loop, in the more complex solution, a loop on its own is used mostly to
programming sense, is a chunk of the same code thats solve a simple calculation, or for waiting for a specific
designed by the programmer to be run over and over again. condition. Which is why, perhaps, the most common loop
Its different from a function or a method, because while combines a conditional statement within a loops structure
theyre both designed to be re-run over and over again too, and thats the for keyword. The for loop is one of those
theyre self-contained islands of logic. peculiar parts of any language, because it requires a specific
With a method or a function, the programmer needs to syntax that doesnt feel that logical. In the case of Python, a
know only what input is needed and what output to expect. typical for loop looks like the following:
Everything else should be handled by the function. A loop is a >>> for i in range (5):
much more primitive construction, and without loops almost ... print (i)
any programming task would become impossibly tedious. ...
This is because they encapsulate what computers do well: 0
repetition and iteration. Without loops, programmers are 1
forced to write everything in longhand, and write code that 2
cant accommodate unknown values or sizes. Its the 3
difference between sowing a field by hand or by using Jethro 4
Tulls horse-drawn You can see in this snippet
seed drill. that the initial for statement
If you want to
Loops are so
integral to code,
Without loops, almost any requires a variable, which for
some reason is nearly always
build your own
C++ application, weve already programming task would called i in examples. Ours is
used several of no exception. This is followed
add the source
code to a text file them in our become impossibly tedious in Python by the phrase in
and build it with examples in range (5). Range is a special
the command previous tutorials in this bookazine. Theyre used as counters, keyword in Python that well revisit in a couple of paragraphs.
g++ helloworld. as ways to step through data, for when you need to wait for a On the following line, we print the value of i. This line needs to
cpp -o helloworld. condition, and for filling arrays and files. But weve not spent be tabbed or spaced in because its going to be executed as
Just run ./
any time discussing the various kinds of loops you can part of the for loop (as denoted by the :).
helloworld to see
implement, or how you might attempt to solve a linear But without prior experience, you cant really guess at the
the results.
output from the syntax of the for loop, and its the same with
other languages. This is because for is slightly different from
most loops, because it includes the implicit definition of a
variable. You just have to accept that whatever syntax they do
use does the equivalent of defining a variable and creating an
acceptable set of circumstances for the execution of your
program to leave the loop. In our previous example, the
variable i iterates between 0 and 4, leaving the loop when it
gets to 5 (a total of five steps when you include zero). We print
the value of i within the loop so you can see whats
happening. If you write the same thing in C or C++, the syntax
looks slightly different, but it gives you a better insight into
whats happening:
for ( int i=0 ; i < 5 ; i++ ){
cout << i << endl;
}
When compiled, linked and run (this is the big difference
C
omputers arent actually very smart. Sure, they can do a compiler, and its one of the most important components of
a million things in the space of a second, and help us an operating system.
to transmit videos of cats around the internet, but Compilers exist for many different programming
they dont understand human languages. Ultimately, languages, but here were going to focus on C, because its
everything in a computer boils down to binary ones and the language used for many important projects, such as the
zeros because thats all that a CPU understands. For us Linux kernel. By far the most common compiler on Linux is
programmers, this makes things a bit complicated. We cant GCC, from the GNU project, and that is what we are going to
directly tell the CPU in English that we want to print a use here.
message on the screen, wait for a key to be pressed and so
forth; we have to somehow tell the machine this information Dissecting a program
in binary. But then, imagine if your programs looked like this: First up, consider this simple C program:
10010010 11100011 #include <stdio.h>
01011011 10001101 int main()
... {
That would be a nightmare, wouldnt it? Finding mistakes puts(Hello, world!);
would be nigh-on impossible, and if you gave your code to }
someone else for modification Well, the entire concept of This simply prints the message Hello world! to the
free/open source software falls apart. So, to fix this problem, screen. If youve never seen C before, you might be a bit
we use compiled puzzled by some of
and interpreted the text, so heres a
languages (more In a compiled language, source brief explanation:
on the latter
later). In a
code is processed, pulled apart the #include line
says that we want to
Try to read
a binary
compiled and converted into binary code use standard input
executable file in language, human- and output routines
a text editor, and readable source (stdio). Then main
youll just see code is processed, pulled apart, jumbled up and eventually says that this is the main part of our program (that is, where
a load of messed- converted into binary code that the CPU can understand. The execution should begin), and the puts command means put
up characters program that does this is called naturally enough a string.
like this. 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:
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 On the left, the output from gcc -S, and on the right a disassembled binary.
such as C and Python, where you can simply say print a Weve highlighted the areas showing identical instructions.
string for me. In assembly, you have to move memory around
into the video buffer for instance, telling the CPU exactly a compiler are deep and complex, and many would argue
which instructions it should execute. So, now enter this: that writing a fully-featured compiler for a complicated
gcc -S foo.c language such as C++ is an even harder job than writing an
This produces a file called foo.s that contains the operating system. Think of all the work a compiler has to do
assembly language version of our program. In other words, in terms of managing variables, parsing complicated
its a list of CPU instructions written in a slightly more instructions, passing control around to different functions,
readable form. Most of it will look like complete gibberish if and so on.
you have never touched assembly before, but these are the Dont think for a second that weve completely covered
two most important lines: the compiler here. Shelves full of whopping great books have
movl $.LC0, (%esp) been written about the science of compilers its not a
call puts subject for the faint-hearted! But now you know the
Essentially, this places the location of our Hello world! fundamentals, and you will certainly look at your code in a
text string into a register (a little like a variable), and then calls different way from this point on. Q
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
What are interpreted languages?
second, it can be quite humbling to see single instructions laid There are two main types of quicker. Similarly, the compiler can spot
bare like this. programming language: compiled and errors that might come up and warn the
In the normal process of compilation (that is, without the interpreted. As weve seen in the former, programmer about them.
-E or -S switches), GCC now runs an assembler to convert a program called a compiler takes an An interpreter, on the other hand, is a
entire source code file and converts it much simpler program that simply
those human-readable instructions into their binary
into CPU binary instructions in one fell steps through the program line-by-line.
equivalents. It then adds extra information to the resulting file
swoop. In contrast, an interpreter reads It doesnt know whats coming up, and
(a.out) to make it a valid Linux executable, and we have a
individual lines from a source code file just dutifully executes things as and
program thats ready to run. and acts on them one at a time, when it sees them. This offers some
essentially interpreting each instruction advantages, in that you dont have to
Jump back! at run time. For instance, take this wait for a compilation process to finish
We can take the a.out file and go one step backwards in the BASIC program: before testing your work; and many
process, to the assembly language stage, using the objdump 10 LET A = 1 interpreters allow you to pause them
command, like so: 20 PRINT Hello world and make changes in the middle
objdump -d a.out > list 30 LET A = A + 1 of execution.
40 IF A < 6 THEN GOTO 20 Ultimately, most programmers prefer
Have a look at the list file now its a disassembly of the
50 END compiled languages over interpreted
binary file, and therefore full of assembly language
Its pretty clear what this does it ones because of the performance
instructions. Most of them are unrelated to our program, and benefits. Having your whole work
prints a message five times on the
are used to tell the Linux program loader some useful things screen. Now, how would a compiler look condensed down into CPU-readable
and set up the environment. But tucked away in there, youll at it? Because a compiler processes the binary makes for better performance
find these lines: source code file as a whole, it sees the than a background program parsing
movl $0x8048490,(%esp) bigger picture, knowing in advance that each line step by step during execution.
call 80482f0 <puts@plt> the loop is going to be executed five Interpreted languages can, however,
Those are the exact same instructions as the ones we saw times. Consequently, the compiler can be useful where speed isnt crucially
a moment ago, expressed in a slightly different way. So, thats make various optimisations, such as important, for example the Bash
replacing the whole bunch with five scripting language. Precision and
the process of how human-readable source code is converted
PRINT lines if that makes execution individual execution rule here.
into CPU-understandable instructions. The inner workings of
Comment syntax
Different languages mark comments differently, and there seems to be of code on the same line, and theyre initiated by using a couple of
little consensus on what a comment should look like. However, there are characters before the comment. Block comments are used to wrap pieces
a couple of rules. Most languages offer both inline and block comments, of text (or code you dont want interpreted/compiled), and usually have
for example. Inline are usually for a single line, or a comment after a piece different start and end characters.
# A hash is used for comments in many scripting languages. When # is followed by a ! it becomes a shebang # and is used to
Bash tell the system which interpreter to use, for example: #!/usr/bin/bash
BASIC REM For many of us, this is the first comment syntax we learn
C /* This kind of comment in C can be used to make a block of text span many lines */
C++ // 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
HTML therefore comments, in action -->
Java /** Similar to C, because it can span lines, but with an extra * at the beginning */
= 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
Perl force you to explain your code more thoroughly
=cut
As well as the hash, Python users can denote blocks of comments using a source code literal called a docstring, which is
Python a convoluted way of saying enclose your text in blocks of triple quotes, like this
WorldMags.net
WorldMags.net
Further coding
Now youve got the basics down,
its time to advance your skills
Data types ......................................................................................... 58
More data types ............................................................... 60
Abstraction ..................................................................................... 62
Using files ........................................................................................... 64
UNIX programs part 1 ....................................... 66
UNIX programs part 2 ...................................... 68
UNIX programs part 3 ....................................... 70
Modules 74
.....................................................................................................
Persistence ...................................................................................... 76
Databases ........................................................................................... 78
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 identified by round brackets, as opposed to square brackets: The Python
interpreter is a
between round brackets, just as you would with a normal (bananas, tiffin, cereal). Dictionaries are similar to a list or
great place to
function. It works the same with strings and any other data a tuple in that they contain a collection of related items. They
experiment with
object, too: differ in that the elements arent indexed by numbers, but by Python code and
>>> word = HELLO keys and are created with curly brackets: {}. Its quite like an see how different
>>> word.lower() English language dictionary. The key is the word that youre data types work
hello looking up, and the value is the definition of the word. together.
There are lots of different methods that can be applied to With Python dictionaries, however, you can use any
lists and strings, and to tuples and dictionaries (which were immutable data type as the key (strings are immutable, too),
about to look at). To see the order of the arguments and the so long as its unique within that dictionary. If you try to use
full range of methods available, youll need to consult the an already existing key, its previous association is forgotten
Python documentation. completely and that data lost for ever.
>>> english = {free: as in beer, linux: operating system}
Variables >>> english[free]
In the previous examples, we used the idea of variables to as in beer
make it easier to work with our data. Variables are a way to >>> english[free] = as in liberty
name different values different pieces of data. They make it >>> english[free]
easy to manage all the bits of data youre working with, and as in liberty
greatly reduce the complexity of development (when you use
sensible names). Looping sequences
As we saw above, in Python you create a new variable with One common operation that you may want to perform on any
an assignment statement. First comes the name of the of the sequence types is looping over their contents to apply
variable, then a single equals sign, followed by the piece of an operation to every element contained within. Consider this
data that you want to assign to that variable. small Python program:
From that point on, whenever you use the name assigned list = [banana, tiffin, burrito]
to the variable, you are referring to the data that you assigned for item in list:
to it. In the examples, we saw this in action when we print item
referenced the second character in a string or the third First, we created the list as we would normally, then we
element in a list by appending index notation to the variable used the for in construct to perform the print function
name. You can also see this in action if you apply the type() on each item in the list. The second word in that construct
function to a variable name: doesnt have to be item, thats just a variable name that
>>> type(word) gets assigned temporarily to each element contained within
<type str> the sequence specified at the end. We could just as well
>>> type(list) have written for letter in word and it would have worked
<type list> just as well.
Thats all we have time to cover in this article, but with the
Other data types basic data types covered, well be ready to look at how you
There are two other common types of data that are used by can put this knowledge to use when modelling real-world
Python: tuples and dictionaries. problems in later articles.
Tuples are very similar to lists theyre a sequence data In the meantime, read the Python documentation to
type, and they can contain elements of mixed types. The big become familiar with some of the other methods that it
difference is that tuples are immutable that is to say, once provides for the data types weve looked at before. Youll find
you create a tuple you cannot change it and that tuples are lots of useful tools, such as sort and reverse! Q
I
n the previous few tutorials, we have been looking at data. press this button once much easier. This second approach
First, we introduced some of Pythons core data types, is whats known as an abstraction. When working on
and then we demonstrated how they can be put to use problems, such as those involving Pythagoras theorem, we
when solving a real problem. The next data-related topic we dont care about how to calculate the square root, only that
want to consider is abstraction, but before we get on to that, we can do it and get the correct result. We can treat the
were first going to look at abstraction in general and as it square root button on our calculator as a black box we
applies to procedures. So, this time well take a brief hiatus never look inside it, we dont know how it does what it does,
from data, before returning to it in a later article. all that matters is that we know how to use it and that it gives
the correct result.
Square roots This a very powerful technique, which can make
To get our heads around the concept of abstraction, lets start programming a lot easier, because it helps us to manage
by thinking about square roots and different techniques for complexity. To demonstrate how abstraction can help us,
finding them. One of these was discovered by Newton, and is consider the Python code below for finding the longest side of
thus known as Newtons method. a right-angled triangle:
It says that when trying to find the square root of a import math
number (x), we should start with a guess (y) of its square def pythag(a, b):
root; we can then improve upon that result by averaging our a2b2 = (a * a) + (b * b)
guess (y) with the result of dividing the number (x) by our guess = 1.0
guess (y). As we repeat this procedure, we get closer and while (math.fabs((guess * guess) - a2b2) > 0.01):
closer to the square root. In most attempts, well never reach guess = (((a2b2 / guess) + guess) / 2)
a definite result, well only make our guess more and more return guess
accurate. Eventually, well reach a level of accuracy that is The first thing to note is that its not in the least bit
good enough for our needs and then give up. Just to be clear readable. Sure, with a piece of code this short, you can read
about what is involved, take a look at the table below for how through it reasonably quickly and figure out whats going on,
you would apply this method to find the square root of 2 (for but at a glance its not obvious, and if it were longer and
example, x). written like this, youd have a terrible time figuring out what
Its a lot of work just to on earth it was doing. Whats
find the square root of
a number. Imagine if when This is a very powerful more, it would be very
difficult to test the different
you were in school, every
time you had to find a square
technique that makes parts of this code as you go
along (aka incremental
root you had to do all these programming easier development, vital for
steps manually. Solving building robust software).
problems involving Pythagoras theorem, for instance, would For instance, how would you break out the code for testing
be much more unwieldy. whether or not a guess is close enough to the actual result
Luckily, assuming you were allowed to use calculators (and can you even identify it?), or the code for improving a
when you were at school, theres another, much easier guess, to check that it works? What if this function didnt
method to find square roots. Calculators come with a button return the expected results how would you start testing all
marked with the square root symbol, and all you have to do is the parts to find where the error was?
Finally, theres useful code in here that could be reused in
other functions, such as that for squaring a number, for
Find a square root taking an average of two numbers, and even for finding the
square root of a number, but none of it is reusable because of
the way its written. You could type it all out again, or copy and
Guess (y) Division (x/y) Average (((x/y) + y)/2) paste it, but the more typing you have to do, the more
1 2/1 = 2 (2 + 1)/2 = 1.5 obscure code you have to copy and paste, and the more likely
mistakes are to make it in to your programming.
1.5 2/1.5 = 1.33 (1.33 + 1.5)/2 = 1.4167 Lets try writing that code again, this time coming up with
1.4167 2/1.4167 = 1.4118 (1.4118 + 1.4167)/2 = 1.4142 some abstractions to fix the problems listed above. We
havent listed the contents of each new function weve
F
or the majority of programming projects, you dont get complexity of how data input and output can be
far before facing the age-old problem of how to get accomplished is entirely down to your programming
data into and out of your application. Whether its using environment. Every language will include functions to load
punched cards to get patterns into a 19th century Jacquard and save data, for instance, but this can either be difficult or
textile loom, or Googles bots skimming websites for data to easy depending on how many assumptions the language is
feed its search engine, dealing with external input is as willing to make on your behalf. However, theres always a
fundamental as programming itself. logical sequence of events that need to occur.
And its a problem and a concept that you may be more You will first need to open a file, creating one if it doesnt
familiar with on the command line. When you type ls to list exist, and then either read data from this file, or write data to
the contents of the current it, before explicitly closing the
directory, for example, the file again so that other
command is reading in the If the filesystem knows processes can use it.
contents of a file, the current
directory, and then the file is being changed, Most languages require
you to specify a read mode
outputting the contents to
another, the terminal.
it wont allow access when you open a file,
because this tells the
When you Of course, the inputs and outputs arent files in the sense filesystem whether to expect file modifications or not. This is
read a file, most most people would recognise, but thats the way the Linux important because many different processes may also want
languages will filesystem has been designed nearly everything is a file. This to access the file, and if the filesystem knows the file is being
step through its helps when you want to save the output of a command, or changed, it wont usually allow access. However, many
data from the use that output as the input to another. processes can access a read-only file without worrying
beginning to the You may already know that typing ls >list.txt will redirect about the integrity of the data it holds, because nothing is
end in chunks the output from the command to a file called list.txt, but you able to change it. If you know about databases, its the same
you specify. In can take this much further because the output can be treated kind of problem you face with multiple users accessing the
this example,
exactly like a file. ls | sort -r will pipe (thats the vertical bar same table.
were reading a
character) the output of ls into the input of sort to create a In Python, as with most other languages, opening a file to
line at a time.
reversed alphabetical list of a folders contents. The write or as read-only can be done with a single line:
>>> f = open(list.txt, r)
If the file doesnt exist, Python will generate a No such file
or directory error. To avoid this, weve used the output from
our command line example to create a text file called list.txt.
This is within the folder from where we launched the
Python interpreter.
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
I
n the next few pages, thats what were aiming to do: get Our goal for the project overall is to:
you writing real programs. Over the next few tutorials, Create a Python program, cat.py, that when called with no
were going to create a Python implementation of the arguments accepts user input on the standard input pipe
popular Unix tool cat. Like all Unix tools, cat is a great target until an end of line character is reached, at which point it
because its small and focused on a single task, while using sends the output to standard out.
different operating system features, including accessing files, When called with file names as arguments, cat.py should
pipes and so on. send each line of the files to
This means it wont take too standard output, displaying the
long to complete, but will also You now know more whole of the first file, then the
expose you to a selection of
Pythons core features in the
than enough to start whole of the second file.
It should accept two
Standard Library, and once
youve mastered the basics, its
writing real programs arguments: -E, which will make
it put $ signs at the end of
learning the ins-and-outs of your chosen languages libraries each line; and -n, which will make it put the current line
that will let you get on with real work. number at the beginning of each line.
This time, were going to create a cat clone that can work
with any number of files passed to it as arguments on the
command line. Were going to be using Python 3, so if you
want to follow along, make sure youre using the same
version, because some features are not backwards-
compatible with Python 2.x.
Python files
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 file-
object 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 newly-
created 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
The final program well be implementing. Its not long, but it makes use of a lot
pass to it to be displayed on standard output.
of core language features youll be able to re-use time and again.
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
The output of the real Unix command, cat, and our Python re-implementation,
to the top of your cat.py file.
are exactly the same in this simple example.
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 because this is the name of the program itself. If you think
access and manipulate it using various techniques weve back to our previous article on data types and common list
seen in previous tutorials and will show in future ones. operations, youll realise this is easily done with a slice. This is
There are only two things just one line:
you really need to know for file in sys.argv[1:]:
about this. They are: The part of the sys Because operating on all
The first element of the list
is the name of the program
module were interested the files passed as arguments
to a program is such a
itself all arguments follow in is the argv object common operation, Python
this. provides a shortcut for doing
To access the list, you need to use dot notation that is to this in the Standard Library, called fileinput.
say, argv is stored within sys, so to access it, you need to type In order to use this shortcut, you must first import it by
sys.argv, or sys.argv[1] to get the first argument to putting import fileinput at the top of your code. You will then
your program. be able to use it to recreate the rest of our cat program so far,
Knowing this, you should now be able to adjust the code as follows:
we created previously by replacing hello.txt with sys.argv[1]. for line in fileinput.input():
When you call cat.py from the command line, you can then print(line, end=)
pass the name of any text file, and it will work just the same. This simple shortcut function takes care of opening each
file in turn and making all their lines accessible through a
Many files single iterator.
Of course, our program is meant to accept more than one file Thats about all that we have space for in this tutorial.
and output all their contents to standard output, one after Although there has not been much code in this particular
another, but as things stand, our program can only accept example, we hope you have started to get a sense for how
one file as an argument. much is available in Pythons Standard Library (and therefore
To fix this particular problem, you need to loop over all the how much work is available for you to recycle), and how a
files in the argv list. The only thing that you need to be careful good knowledge of its contents can save you a lot of work
of when you do this is that you exclude the very first element, when implementing new programs. Q
T
he previous tutorial showed you how to build a simple new line character is passed to standard input (by pressing
cat clone in Python. In this guide, were going to add [Return]), it will then print everything that came before it to
some more features to our program, including the standard output.
ability to read from the standard input pipe, just like the real Right, now we have two modes that our program can
cat, and the ability to pass options to your cat clone. So, operate in, but we need to put them together into a single
without further delay, lets dive in. program. If we call our program with arguments, we want it to
Fortunately, you already know everything you need to work like last time that is, by concatenating the files
interact with the standard input pipe. In Linux, all pipes are contents together; if its called without any arguments, we
treated like files you can want our program to work by
pass a file as an argument to repeating each line entered
a command, or you can pass Python provides us with into standard input. We could
a pipe as an argument. It
doesnt matter which you do,
a much more powerful easily do this with what we
have learned so far simply
because theyre basically the
same thing.
alternative to sys.argv check to see what the length
of the sys.argv array is. If its
In Python, the same is true. All you need to get to work greater than 1, then do last lessons version, otherwise do this
with the standard input pipe is access to the sys library, which version:
if you followed along last time, you already have. Lets write a if len(sys.argv) > 1:
little sample program first to demonstrate: [last month...]
import sys else:
for line in sys.stdin: [this month...]
print(line, end=) Pretty straightforward. The only point of interest here is
The first line imports the sys module. The lines that follow the use of the len() function, seeing as were on a journey to
are almost identical to those that we had last time. Rather discover different Python functions. This function is built in to
than specifying the name of a file, however, we specified the Python, and can be applied to any type of sequence object (a
name of the file-object, stdin, which is found inside the sys string, tuple or list) or a map (like a dictionary), and it always
module. Just like a real file, in Python the standard input pipe tells you how many elements are in that object.
is an iterable object, so we use a for loop to walk through There are more useful functions like this, which you can
each line. find at http://docs.python.org/3/library/functions.html.
You might be wondering how this works, though, because
standard input starts off empty. If you run the program, you Parsing arguments and options
will see what happens. Rather than printing out everything This is quite a simplistic approach, however, and Python
thats present straightaway, it will simply wait. Every time a actually provides us with a much more powerful alternative to
sys.argv. To demonstrate this, we are going to add two
options to our program that will modify the output generated
by our program.
You may not have realised it, but cat does in fact have a
range of options. Were going to implement the -E, which
shows dollar symbols at the end of lines, and -n, which
displays line numbers at the beginning of lines.
To do this, well start by setting up an OptionParser. This
is a special object, provided as part of the optparse module,
which will do most of the hard work for you. As well as
automatically detecting options and arguments, saving you a
lot of hard work, OptionParser will automatically generate
help text for your users in the event that they use your
The Python language comes with all the bells and whistles you need to write program incorrectly or pass --help to it, like this:
useful programs. In this example, you can see the replace method applied to
[jon@LT04394 ~]$ ./cat.py --help
a string in order to remove all white space in the tutorial, we used the rstrip
Usage: cat.py [OPTION]... [FILE]...
method for a similar purpose.
W
eve come quite a long way over the last two nested for loops, although theyre not nearly as readable as
tutorials, having implemented the ability to echo object-oriented code.
the contents of multiple files to the screen, the When building complicated programs, figuring out how to
ability to echo standard input to the screen and the ability to organise them so they remain easy to read, easy to track
detect and act upon options passed by the user of our which variables are being used by which functions, and easy
program. All that remains is to update, extend, or add new
for us to implement the line features, can be challenging.
number option and to gather Its a very natural way To make this easier, there are
together everything else
weve written into a single,
of thinking because it various paradigms that
provide techniques for
working program. mirrors the real world managing complexity.
One of these paradigms is
Objects the concept of object-oriented programming. In object-
Last time, we ended by saying that there are many ways we oriented programming, the elements of the program are
could implement the line counting option in our program. broken down into objects which contain state variables, in
Were going to show you how to do it in an object-oriented other words that describe the current condition of the
style, because it gives us an excuse to introduce you to this object, and methods, that allow us to perform actions on
aspect of Python programming. You could, however, with a those variables or with that object.
little careful thought, implement the same function with some Its a very natural way of thinking, because it mirrors the
real world so closely. We can describe a set of properties
about your hand, such as having five fingers that are in
certain locations, and we can describe certain methods or
things you can do with your hand, such as moving one finger
to press a key, or holding a cup. Your hand is an object,
complete with state and methods that let you work with it.
Were going to turn our cat program into an object, where
its state records how many lines have been displayed, and its
methods perform the action of the cat program
redisplaying file contents to the screen.
Python objects
Python implements objects through a class system. A class is
a template, and an object is a particular instance of that class,
modelled on the template. We define a new class with
a keyword, much like we define a new function:
class catCommand:
Inside the class, we specify the methods (functions) and
state that we want to associate with every instance of the
object. There are some special methods, however, that are
frequently used. One of these is the init method. This is run
when the class is first instantiated into a particular object, and
allows you to set specific variables that you want to belong to
that object.
def __init__(self):
self.count = 1
In this case, weve assigned 1 to the count variable, and
well be using this to record how many lines have been
displayed. You probably noticed the self variable, passed as
Just to prove that it works, heres our cat implementation, with all of the
the first argument to the method, and wondered what on
options being put to use.
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.
I
n previous tutorials, we have mentioned how automatically parsed command line options. We could access
programming is all about managing complexity, and weve them by typing the modules name, followed by the name of
introduced you to quite a few of the tools and techniques the function we wanted to execute:
that help programmers do this. From variables to function optparse.OptionParser()
definitions or object-orientation, they all help. One tool weve This was great from a readability perspective. In our cat
yet to cover, in part because you dont start to come across it clone, we didnt have to wade through lots of code about
until youre writing larger programs, is the idea of modules parsing command line arguments; instead we could focus on
and name spaces. the code that dealt with the logic of echoing file contents to
Yet, if youve written a program of any length, even just a the screen and to the standard output pipe. Whats more, we
few hundred lines, this is a tool youre no doubt desperate for. didnt have to worry about using names in our own program
In a long file of code, youll have noticed how quickly it that might collide with those in the optparse module,
becomes more difficult to read. Functions seem to blur into because they were all hidden inside the optparse
one another, and when youre trying to hunt down the cause namespace, and reusing this code was as easy as typing
of the latest error, you find it difficult to remember exactly import optparse no messy copy and pasting here.
where you defined that all-important variable.
These problems are caused by a lack of structure. With all How modules work
your code in a single file, its more difficult to determine the Modules sound fancy, and you might think theyre
dependencies between elements of your program that is, complicated, but in Python at least theyre really just plain
which parts rely on each other to get work done and its old files. You can try it out for yourself. Create a new directory
more difficult to and inside it create a fact.py file. Inside it, define a function to
visualise the flow return the factorial of a given number:
As your programs grow of data through def factorial(n):
Python style
While many people think of Python as common problems, and the best ways to which you can start learning:
a modern language, its actually been format your code to make sure its http://python.net/~goodger/
around since the early 1990s. As with any readable for co-workers and anyone else projects/pycon/2007/idiomatic/
programming language thats been working on the code with you (including handout.html
around for any length of time, people who your future self). www.python.org/dev/peps/
use it often have learned a lot about the If youre interested in finding out more pep-0008
best ways to do things in the language about these best practices in Python, Read these and youre sure to gain
in terms of the easiest way to solve there are two very useful resources from some deeper insight into the language.
S
torage is cheap: you can buy a 500GB external hard Easy! Notice how we used the format string function to
drive for less than 40 these days, while even add a new line to the end of each string, otherwise we would
smartphones come with at least 8GB of storage, and end up with everything on one line which would have made
many are easily expandable up to 64GB for only the price of a it harder to use later.
pair of jeans. Re-using the contents of this file would be just as simple.
Its no surprise, then, that almost every modern Using the file as an iterator, load each line in turn into a list,
application stores data in one way or another, whether thats stripping off the trailing new line character. Well leave you to
configuration data, cached data to speed up future use, saved figure this one out.
games, to-do lists or photos. The list goes on and on. When using files in your Python code, there are two things
With this in mind, this programming tutorial is going to that you need to keep in mind. The first is that you need to
demonstrate how to deal with persistent data in our language convert whatever you want to write to the file to a string first.
of choice Python. This is easy, though, because you can just use the built-in
The most obvious form of persistent storage that you can str() function for example, str(42) => 42.
take advantage of in Python is file storage. Support for it is The second is that you have to close the file after you have
included in the standard library, and you dont even have to finished using it if you dont do this, you risk losing data that
import any modules to take advantage of it. you thought had been committed to disk, but that had not yet
To open a file in the current working directory (that is, been flushed. You can do this manually with the close method
wherever you ran the Python script from, or wherever you of file objects. In our example, this would translate to adding
were when you launched the interactive shell), use the open() file.close() to our program. Its better, however, to use the
function: with keyword:
file = open(lxf- with open(lxf-test.txt, a) as file:
Almost every modern test.txt, w) feeds = [line.rstrip(\n) for line in f]
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).
This is much easier, and it has other applications outside to keep track of how many unread items each feed had, and
of persisting data in files, too. For example, if you wanted to which item was the last to be read. You might do this with a
transfer your feed list across the network, you would first dictionary, for example:
have to make it into a character string, too, which you could tracker = { bbc.co.uk: { last-read: foo,
do with pickle. num-unread: 10, },
The problem with this is that it will only work in Python tuxradar.co.uk: { last-read: bar,
that is to say, other programming languages dont support num-unread: 5, }}
the pickle data format. If you like the concept of pickling
(more generically, serialising), theres another option that You could then store the list of feeds and the tracking
does have support in other languages, too: JSON. details for each in a single file by using the shelve module,
You may have heard of JSON it stands for JavaScript like so:
Object Notation, and is a way of converting objects into import shelve
human-readable string representations, which look almost shelf = shelve.open(lxf-test)
identical to objects found in the JavaScript programming shelf[feeds] = feeds
language. Its great, because its human readable, and also shelf[tracker] = tracker
because its widely supported in many different languages, shelf.close()
largely because it has become so popular with fancy web There are a few important things that you should be aware
2.0 applications. of about the shelve module:
In Python, you use it in exactly the same way as pickle in The shelve module has its own operations for opening and
the above example, replace pickle with json throughout, and closing files, so you cant use the standard open function.
youll be writing interoperable, serialised code! To save some data to the shelf, you must first use a
standard Python assignment operation to set the value of a
Shelves particular key to the object you want to save.
Of course, some code bases have many different objects that As with files, you must close the shelf object once finished
you want to store persistently between runs, and keeping with, otherwise your changes may not be stored.
track of many different pickled files can get tricky. Theres Accessing data inside the shelf is just as easy. Rather than
another Python standard module, however, that uses pickle assigning a key in the shelf dictionary to a value, you assign a
underneath, but makes access to the stored objects more value to that stored in the dictionary at a particular key: feeds
intuitive and convenient: the shelve module. = shelf[feeds]. If you want to modify the data that was
Essentially, a shelf is a persistent dictionary that is to stored in the shelf, modify it in the temporary value you
say, a persistent way to store key-value pairs. The great thing assigned it to, then re-assign that temporary value back to
about shelves, however, is that the value can be any Python the shelf before closing it again.
object that pickle can serialise. Lets take a look at how you Thats about all we have space for this tutorial, but keep
can use it. Thinking back to our RSS reader application, reading, because well discuss one final option for persistent
imagine that as well as the list of feeds to check, you wanted data: relational databases (for example, MySQL). Q
I
n the last tutorial, we looked at how to make data
persistent in your Python programs. The techniques we
looked at were flat-file based, and as useful as they are,
Relational database
theyre not exactly industrial scale. As your applications grow
Album name Free At Last
more ambitious, as performance becomes more important,
or as you try to express more complicated ideas and Running time 65:58
relationships, youll need to look towards other technologies, Year 1972
such as an object database or, even, a relational database. Artist_id 1
As relational databases are by far the most common tool
for asking complex questions about data today, in this coding and good, but very wasteful. For every track on the same
tutorial were going to introduce you to the basics of relational album, we have to duplicate all the information, such as the
databases and the language used to work with them (which is album name, its running time, the year it was published, and
called SQL, or Structured Query Language). With the basics all the information about the artist, too, such as their name
mastered, youll be able to start integrating relational and the year they split. As well as being wasteful with storage
databases into space, this also makes the data slower to search, harder to
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 MariaDB is a drop-in replacement for the MySQL database, and is quickly
finding favour among distros including Mageia, OpenSUSE and even Slackware.
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. primary key. You can find out more about the create table
Much as you work within a current working directory on the statement in the MySQL documentation at http://dev.
Linux console, in MySQL, many commands you issue are mysql.com/doc/refman/5.5/en/create-table.html.
relative to the currently selected database. You can switch
databases with the use command: Inserts and queries
use lxfmusic; Inserting data into the newly created tables isnt any trickier:
Now to create some tables: insert into Album (name) values (Free at Last);
create table Album ( Once again, we specify the action and the object on which
Album_id int auto_increment primary key, were acting, we then specify the columns which were
name varchar(100) inserting into, and finally the values of the data to be put in.
); Before we can insert an entry into the Track table,
however, we must discover what the ID of the album Free At
create table Track ( Last is, otherwise we wont be able to link the tables together
Track_id int auto_increment primary key, very easily. To do this, we use the select statement:
title varchar(100), select * from Album where name = Free At Last;
running_time int, This command says we want to select all columns from
Album_id int the Album table whose name field is equal to Free At Last.
); Pretty self-explanatory really! If wed only wanted to get the ID
The most obvious things to note here are that we have field, we could have replaced the asterisk with Album_id and
issued two commands, separated by semi-colons, and that it would have taken just that column.
we have split each command over multiple lines. SQL doesnt Since that returned a 1 for us (it being the first entry in the
care about white space, so you can split your code up database), we can insert into the Track table as follows:
however you like, as long as you put the right punctuation in insert into Track (title, running_time, Album_id) values
the correct places. (Little Bit of Love, 154, 1);
As for the command itself, notice how similar it is to the The big thing to note is that we specified the running time
create database statement. We specify the action we want in seconds and stored it as an integer. With most databases,
to take, the type of object that were operating on and then you must always specify a data type for your columns, and
the properties of that object. With the create database sometimes this means that you need to represent your data
statement, the only property was the name of the database; in a different manner than in your application, and you will
with the create table statement, weve also got a whole load need to write some code to convert it for display. That said,
of extra properties that come inside the parentheses and are MySQL does have a wide range of data types, so many
separated by commas. eventualities are covered.
These are known as column definitions, and each comma- Thats all we have space for here, but dont let your MySQL
separated entry describes one column in the database. First, education stop there. Now youve seen the basics, youll want
we give the column a name, then we describe the type of data to investigate foreign keys and joins, two more advanced
stored in it (this is necessary in most databases), then we techniques that will enable you to be far more expressive with
specify any additional properties of that column, such as your SQL. Youll also want to investigate the different types of
whether it is part of the primary key. relationship, such as one-to-one, one-to-many, many-to-one
The auto_increment keyword means you dont have to and many-to-many.
worry about specifying the value of Track_id when inserting Finally, if you want to integrate MySQL with your
data, as MySQL will ensure that this is an integer that gets programming language of choice, look out for an appropriate
incremented for every row in the database, thus forming a module, such as the python-mysql module for Python. Q
WorldMags.net
WorldMags.net
Raspberry Pi
Discover how you can develop on
the low-cost, micro-PC system
Getting started 82
.....................................................................
Starting Scratch 84
................................................................
Further Scratch 88
...................................................................
Python on the Pi 94
...............................................................
T
he Pi-tailored Raspbian desktop environment free to peruse that to learn about commands such as ls , cd
received a thorough overhaul in late 2015, making it and mkdir . Its a good idea to keep the software on your Pi
an even friendlier place to learn, play or work. The up to date, so lets see how that works from the terminal.
Raspbian team has squeezed a great deal into the 3.5GB Were going to use the apt-get package manager, which
install. Most importantly, theres a special edition of Minecraft, handles all of the consultations with Raspbian mirrors and
but theres also Wolfram Mathematica (for doing hard sums), does a remarkable job of (un)installing packages and their
LibreOffice and the Epiphany web browser, as well as PDF dependencies with a minimum of fuss. Open LXTerminal from
and image viewers. Were amply catered for programming- either the menu or the launch bar, and enter the following
wise, too theres Scratch (the visual coding language), Sonic command:
Pi (the live coding synth), not to mention the Python $ sudo apt-get update
programming language.
Compared to, say, Windows 10, Raspbian may appear Terminal velocity
somewhat spartan. Bear in mind that the Pi (particularly non- That updates Raspbians local list of all available packages. In
Pi 2 models) is considerably less powerful than your average the next step, the list of current packages installed on the
desktop computer, so lightweight is the order of the day. It system is checked against this, and where newer packages
remains no less functional, and thanks to graphical hardware are available, these are offered for upgrade:
acceleration, it $ sudo apt-get
can play high-
definition video The Pi is far less powerful than upgrade
If theres lots
without batting an
eyelid. Software is
your average desktop computer, of packages to
upgrade, the
managed from but remains no less functional. process may take
the Add/Remove some time. The
Software bottleneck is mostly
application. This downloads packages from Raspbians due to the speed at which data can be written to the SD card
repositories, which you can trust. Its possible to do this from you may want to make a cup of tea. You should update your
the command line too. In fact, its possible to do pretty much packages pretty regularly; youll get important security
anything from the command line. updates and the latest features or fixes for all your software.
Theres a very brief introduction to the terminal in the Mint Python is a particularly good first programming language.
tutorial (see page 16), which applies equally well here, so feel 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.
Launch bar
Frequently used applications
can be added here just right-
click 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 Terminal Network
browsing Windows and other The command line gives direct access to the From here you can configure your wired and wireless network
network shares. operating system. It can be daunting but, with connections. Some wireless adapters work better with the Pi
practice, youll appreciate how efficient it is. than others, so its worth doing some research before you buy.
Scratch comes with a range of images that you can use for sprites. Each sprite can have a number of costumes, or images. Click on the
Click on New Sprite From File, or Costumes > Import to see them. Costumes tab to create new ones or manage existing ones.
Step by step
H
ow did you learn to program? Typically, we think of a
person sitting in front of a glowing screen, fingers Fig 1.0 The logic for
slowly typing in magic words that, initially, mean our logo sprite.
nothing to the typist. And in the early days of coding, that was
the typical scene, with enthusiasts learning by rote, typing in
reams of code printed in magazines.
In this modern era, when children are now encouraged to
learn coding concepts as part of their primary school
education, we see new tools being used to introduce coding
to a younger generation, and the most popular tool is the
subject of this very tutorial.
Scratch, created by MIT, is a visual programming Fig 1.2 This is the code
environment that promotes the use of coloured blocks over that clears effects.
chunks of code. Each set of blocks provides different
functionality and introduces the concepts of coding in a to version 2.
gentle and fun way. Children as young as six are able to use As mentioned, Scratch uses a block-based system to
Scratch, and its now being heavily used in the UK as part of teach users how different functions work in a program. This
the curriculum for Key Stages 1 to 3 (6 to 14 years old), and can be broken down into the following groups and colours:
as part of the Code Club scheme of work. Motion (dark blue) This enables you to move and control
Scratch uses a For the purpose of this tutorial, we are using the current sprites in your game.
three-column UI. stable version of Scratch, which at the time of writing is 1.4. Control (orange) These blocks contain the logic to control
From left to right: Version 2 of Scratch is available as a beta and reports suggest your program (loops and statements) and the events needed
Block Palette,
that its very stable to use, but theres a number of differences to trigger actions, such as pressing a key. In Scratch 2.0, the
Script Area and
between the locations of blocks when comparing version 1.4 events stored in Control have their own group, which is called,
The Stage.
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 with the same interface and blocks of code, but version of Scratch from the website. This very
comes pre-loaded on every Raspberry Pi running with a few subtle differences. This is the latest latest version is still in beta but reports show that
Raspbian and is available for download from version of Scratch and you can save all of your it is very stable and ready for use. Scratch 2.0
http://scratch.mit.edu. But did you know that work in the cloud ready to be used on any PC you uses Adobe Air and is available across all
there is an online version that provides all of the come across. If you wish to use the code built platforms including Linux. Adobe dropped its
functionality present in the desktop version, but online on a PC that does not have an internet support for Air on Linux a few years ago but you
requires no installation or download? Head over connection, you can download your project and can still download the packages necessary for
to the MIT website (above) and youll be greeted run it offline. You can also download the latest Scratch 2.0 to work.
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 Parallelism This is the principle of running rules that we all learn in school. We can apply
you are also learning to code? No matter what more than one sequence of code at the same operators to text and numbers, which enables us
language you use, the underlying concepts of time. Weve used that a lot in our Scratch game to perform calculations in our code and iterate
coding provide a firm foundation. And once because each sprite has its own code that runs data if required.
learnt they can be applied to any coding project. in parallel to each other. Conditionals These form the basis of our logic
The main concepts are: Events This is a trigger that starts a sequence and provide a method for us to compare data
Sequences A series of tasks required to be of code, and the most visible event in our Scratch against the input that is given by the player. We
completed in a certain order. For example, the game here is clicking on the green flag to start have used conditionals in our game to compare
steps needed to solve a maze. the game. the answer given to the expected answer. If they
Loops A way to repeat a sequence. They can Data We use a variable to store the value of our both matched, which in Boolean logic would be
be run for ever (while true) or controlled using a score and we can later retrieve and manipulate classed as True, the player would be awarded a
for statement (for x in range(0,3)). We have the score to show the players progress through point. If they did not match, which would be
used many loops to control the players progress the game. defined as False, the player would have to try
in our game. Operators These are the basic mathematical once again.
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 Fig 1.9 Hit the
forever if a < 10 a=0 Make a Variable
say a for 2 secs while a < 10: button to create
a variable.
change a by 1 print a
This gives the variable a the value of 0. We then create a a=a+1
conditional loop that only loops while a is less than 10. Inside So why did we include that piece of Python code in a
the loop, we ask Scratch to print the value of a for 2 seconds, Scratch tutorial? It simply illustrates that there isnt a lot of
then increment the value of a by 1. This loop will continue until difference in the logic between the two languages, and that
we reach a = 9 and then it will stop as the next value, 10, is Scratch can be used to help understand the logic that powers
not less than 10. In Python our code looks like this 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.
T
he rationale for favouring an integrated development interface) for hooking it up to Python. So in no time you can
environment (IDE) is discussed in the Mint section be coding all kinds of colourful, improbable things involving
(see page 28), wherein we met the Geany IDE. obsidian and TNT blocks.
Ultimately, it comes down to being a more centralised,
efficient way to work. Raspbian comes with IDLE an IDE IDLE hands
specifically designed for the Python language and were Well put Minecraft aside for the moment as we introduce
going to use it to further our programming adventures using a IDLE. Press Tab to release the mouse cursor from the game,
game called Minecraft, which you might have heard of. and click on the Raspbian menu. Youll notice that it rather
Youll find Minecraft in the Games menu, so start it up now unhelpfully appears behind the Minecraft window. This is
as a pre-coding warm-up. Click on Start Game and then because of the selfish way the game accesses the GPU, in
Create New to generate a new world. After a few seconds, effect ignoring anything else on the screen and summarily
youll find yourself in the Minecraft world. You can navigate drawing on top of it. So either minimise the Minecraft window
with the W, A, S and D keys, and look around with the mouse. or put it somewhere out of the way. We need it open so that
Space makes you (actually the Minecraft protagonist, whose IDLE can talk to it.
name is Steve) jump, and double-tapping Space makes you Youll find Python 2 and 3 versions of IDLE in the
fly (or fall, if you were already flying). You can bash away at the Programming menu. The Minecraft Python API has been
landscape with the left mouse button the right button is updated to support version 3, so use that one. Theres a quick
used for placing blocks. To choose a type of block to place, guide to the interface opposite note that only the
press E to bring Interpreter window
up a menu. opens initially.
In the event In no time you can be coding all Select File >
that youre
already a
kinds of colourful, improbable Open, then navigate
to the python/
Minecraft
aficionado, youve
things involving TNT blocks. folder and open the
helloworld.py file
probably noticed we made in the
that the Pi edition is slightly restricted. Theres no crafting, no previous tutorial. The Editor window opens to display our
enemies and no Nether, but it does support networked play, code, complete with helpful syntax highlighting. From the Run
and it is free. It also has another trick up its sleeve it comes menu, select Run Module or press F5. The Interpreter window
with a comprehensive API (application programming 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...
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 Run menu Editor
Window menu. Youll find the Run Module option (F5) here. This This is where we edit our code. Similar to Geany (see page
checks the syntax of your code, then executes it, 28), Python keywords are highlighted. IDLE auto-indents
with the output appearing in the shell. using four spaces, rather than a tab.
without saving it, the Run Module helpfully asks if you want to current position, and mc.player.setPos() to change it. Add the
save it first you cant run unsaved code. If your code following lines to your code:
contains mistakes, you get an error message and the x, y, z = mc.player.getPos()
offending bit of code is highlighted in red. mc.player.setPos(x, y + 50, z)
Close the helloworld.py file and start a new one by Save this update, and again make sure the Minecraft
choosing File > New from the Interpreter window. Well start window is visible before running the module. As before, the
our new program with the boilerplate code required to get message is displayed, but then we suddenly find ourselves
Python talking to our Minecraft game: high in the sky and falling fast. Fortunately, Steve doesnt get
from mcpi.minecraft import Minecraft hurt in the game, and hell land somewhere around where
mc = Minecraft.create() he was before the program was run, presumably a little
The first line imports the Minecraft module into our code discombobulated, though
dont worry too much about the awkward syntax here, but
do worry about getting the capitalisation correct. The second Catching the bug
line sets up a new object, mc (short for minecraft), using the We can use IDLEs built-in debugger to see this happening
create() function that we imported in the first line. The mc step by step. Right-click the final setPos() line and choose
object acts as a conduit between our code and the Minecraft Set Breakpoint. Next, in the Interpreter window, choose
game. Now lets add a third line, so that our program actually Debug > Debugger. Now return to your program and run it
does something: once again. The debugger enables you to run step by step
mc.postToChat(Hello Minecraft World) through your code, which is incredibly useful for (funnily
Save this file in the python/ folder as hellomc.py. Now enough) ridding your code of bugs.
arrange windows so that both your code and Minecraft are By defining a breakpoint, we can also tell IDLE to execute
visible, and run your program. If all goes according to plan, a everything up to this line, and on our go ahead continue. From
message should appear in the game. A few seconds later, it the debugger window, choose Go. Notice that in the game
will vanish. Weve manipulated the games chat feature to window we get as far as displaying the message, but not as
display messages not from other users, but from our own far as being sent skyward. Theres a lot of stuff you neednt
program. And we can do an awful lot more. worry about going on in the debugger, but notice that the
As you [you mean Steve Ed.] wander around the variables x , y and z from the getPos() line have been
Minecraft world, youll notice that some numbers in the top- calculated in the Globals panel. Press Go again to initiate
left change. These give your position in terms of a 3D Steves catapulting skyward.
co-ordinate system. Dont worry if the idea of doing geometry The Minecraft API also contains getBlock() and
is terrifying or offensive for now, all you need to know is that setBlock() functions for probing and manipulating the
the x and z directions are parallel to the worlds surface, and environment. These functions are beyond the scope of this
the y direction determines your height. primer, unfortunately, but youll be able to find some great
The API contains functions for working with the players Minecraft tutorials at Martin OHanlons handy website:
position: we can use mc.Player.getPos() to get the players http://stuffaboutcode.com. Q
A
fter the basics lets delve into Python. Learning a new following code uses a dot:
programming language can be as daunting as >>> print(Greetings, name, enjoy your stay.,sep=.)
it is rewarding, all the more so for those embarking On the other hand, using sep= instead gives no
on their maiden coding voyage. Exactly which language is separation at all. Besides separators, print() also issues a
most suitable for beginners can be debated until the bovine newline character, represented by the string \n after the
herd returns, but Python is certainly a worthy candidate. final argument. This can be easily changed, though, by
Python code is easy to follow, the syntax favours simplicity specifying the end parameter, which is sometimes desirable,
throughout and its enforcement of indentation encourages depending on the circumstances.
good coding styles and practices. Your distribution probably Besides welcoming people, we can use the interpreter as a
already has some version of Python installed, but for this calculator. It understands + (addition), - (subtraction), *
tutorial were targeting the newer version 3. You can check (multiplication), / (division) and ** (exponentiation), as well
your Python version by opening up a terminal and typing: as many more advanced maths functions if you use the
$ python -V maths module. For example, to find the square root of 999
Many distributions ship with both versions installed, but times 2015 and store the result in a variable x:
some (including Debian 8) still default to the 2.7 series. If the >>> import math
previous command indicated that this was the case, see what >>> x = math.sqrt(999 * 2015)
happens if you do: One of many things that Python helpfully takes care of for
$ python3 us is data types. Our first variable name was a string, while
If that doesnt work, then youll need to find the python3 the recently defined x is a floating point number (or float for
packages from your package manager. They should be short). Unlike typed languages, where we would have to
straightforward to locate and install. If, on the other hand, the explicitly specify the type of a variable where it is defined,
command succeeded, then you will, besides being availed of Python is smart enough to figure out that sort of information
version information, find yourself at the Python 3 interpreter. for itself, saving us the trouble. Also, Python variables do not
Here you can execute code snippets on the fly, and here is have fixed types, so we could actually recast x above to an
where we will begin this tutorial. You can exit the interpreter at integer with:
any time by pressing Ctrl+D or by typing: >>> x = int(x)
Now x has been rounded down to the nearest whole number. arguments specifying the start value and step size of the
We could convert it to a string as well, using the str type. range. This means that we can get all the odd numbers
Another important construct in Python is the list. A list is between 5 and 19 (including the former but excluding the
defined by using square brackets and may contain any and all latter) by using:
manner of data even including other lists. Heres an >>> twostep = list(range(5,19,2))
arbitrary example: Negative step sizes are also supported, so we could count
>>> firstList = [aleph, beth, 3.14159265] down from 10 to 1 with range(10,0,-1).
Items in our list are zero-indexed, so we access the first Items can be inserted into lists or removed from them
item, for example, with firstList[0] and the third one with by using various methods. Methods in Python are
firstList[2]. Some languages are one-indexed, which some properties of an object, and are called by suffixing said
people find more intuitive, but Python is not one of them. object with a dot, followed by the method name. So we could
Strings are similar to lists (in that they are a sequence of add 19 to the end of our previous list by using the append()
characters) and many list methods can be applied to them. method, such as:
Thus we can get the first character of name with name[0]. >>> twostep.append(19)
We can also get the last character using name[ -1] rather We could also insert the value 3 at the beginning of the list
than having to go through the rigmarole of finding the strings with twostep.insert(0,3). There are many other list methods,
length first, for which you would have to use the len() which you can peruse using help(list). Help is available for
function. Strings are immutable which means that once any Python keyword and many other topics, and this can be a
theyve been defined, they cannot be changed but this is very useful resource when you get stuck.
fine because they can be redefined, copied or reconstructed When you start working with multiple lists, you might
depending on our purposes. witness some rather strange behaviour, such as the diagram
A particularly useful construct is list (or string) slicing.
We can get the first two elements of our list by using
firstList[:2], or all but the first item with firstList[1:]. These
are short forms of the more general slicing syntax [x:y],
which returns the substring starting at position x and ending
at position y. Omitting x defaults to the beginning of the list,
and omitting y defaults to the end. One can even specify a
third parameter, which defines the so-called step of the slice.
For example, the slice [x:y:2] gives you every second item
starting at position x and ending at position y, or wherever the
previous step lands. Again, this can be abbreviated if you
just want every second item from the whole list to [::2].
Negative step sizes are also permitted, in which case our
starting point x is greater than y, and omitting x defaults to
the end of the list. Thus slicing our list above like so
returns the reverse of that list:
>>> firstList[::-1]
Besides defining lists by explicitly specifying their
members, there are a number of constructs available for
defining lists that have some sort of pattern to them. For
example, we can make the list [0,1,2,3,4,5] by using
list(range(6)). In Python 3, the range() function returns an
iterator (it doesnt contain any list items but knows how to
generate them when given an index), so there is an additional
function call to turn it into a list proper. Again, there is
potential for 0-based grievances, because 6 is not in that list,
and again its something we simply have to get used The IDLE environment is ideal [ha, ha Ed] for developing larger Python
to. Similar to slicing, the range() function can take optional projects. Its included in the Raspbian distribution, too.
def daysOld(birthday):
today = datetime.date.today()
ageDays = (today - birthday).days
return(ageDays)
if __name__ == __main__':
weekdays = [Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday]
List comprehensions
Lists and the various constructs weve just comprehensions. Our iterator variable j runs we could select only those beginning with Z or
introduced can join forces to form one of over the beginnings of the names of each day of those which have e as their second letter by
Pythons most powerful features: list the week, and then our list comprehension using:
comprehensions. These are a great suffixes the string day the addition operator >>> names = [Dave, Herman, Xavier,
demonstration of Pythons laconic concision. (+) concatenates (in other words, joins) strings Zanthor]
Consider the following example: together. If you want a more arithmetical >>> [j for j in names if j[0] == Z or j[1] == e]
>>> daysShort = [Mon, Tues, Wednes, example, you could try: [Herman, Zanthor]
Thurs, Fri, Satur, Sun] >>> [j ** 3 for j in range(11)] Its worth noting at this point that to test for
>>> days = [j + day for j in daysShort] This returns a list comprising the cubes of equality, we use the == operator. Be careful,
Very often, coders use short variable names, the numbers from 0 to 10. Comprehensions can because a single equals sign is only used for
commonly i, j and k, for ephemeral variables, use all kinds of other constructs as well as for assignment, so doing something such as if a =
such as those which are used in loops or loops. For example, if we have a list of names, 5: would result in an error.
desktop and open a terminal window. From here, start Python The text_colour parameter here specifies the RGB
3 ($ python3) and enter the following: components of the colour. As well as text, we can also work Quick
>>> from mcpi import minecraft with the individual LEDs, using the set_pixels() function. This tip
>>> mc = minecraft.Minecraft.create() probably isnt the type of thing you want to do too much of by Were assuming
>>> mc.postToChat("Hello world.) hand, though, because each LED requires its own colour you use Python 3
The API is capable of much more than this we can triple. You can use this trick as a slight shortcut if youre only throughout this
change the player and camera position, get the ground level going to be working with a few colours, though: series. This project
still works with
at a given set of co-ordinates, and get and set block X = [255, 0, 0] # Red Python 2, but you
information there, too. The following draws a rough and ready O = [255, 255, 255] # White may need to install
representation of a Raspberry Pi (it could be a Pi 2 or a Model the Pillow imaging
B+), component by component: question_mark = [ library with sudo
pip install Pillow.
>>> mc.setBlocks(-6, -3, -9, 7, -3, 11, 35, 13) O, O, O, X, X, O, O, O,
>>> mc.setBlocks(7, -2, -8, 7, -1, 5, 35, 15) O, O, X, O, O, X, O, O,
>>> mc.setBlocks(4, -2, 8, 6, 0, 11, 42) O, O, O, O, O, X, O, O,
>>> mc.setBlocks(0, -2, 8, 2, 0, 11, 42 ) O, O, O, O, X, O, O, O,
>>> mc.setBlocks(-5, -2, 8, -2, 0, 11, 42) O, O, O, X, O, O, O, O,
>>> mc.setBlocks(-5, -2, 1, -2, -2, 1, 35, 15) O, O, O, X, O, O, O, O,
>>> mc.setBlocks(2, -2, -9, -1, -2, -9, 35, 15) O, O, O, O, O, O, O, O,
>>> mc.setBlocks(-6, -2, -7, -6, -2, -6, 42) O, O, O, X, O, O, O, O
>>> mc.setBlocks(-6, -2, -2, -6, -2, 0, 42) ]
>>> mc.setBlock(-6, -2, 3, 35, 15)
>>> mcsetBlocks(0, -2, -2, 2, -2, -4, 35, 15) ap.set_pixels(question_mark)
The setBlocks() function fills the cuboid given by the first It is probably more desirable to type this code into a text
six numbers (the first three are co-ordinates of one corner editor and then import it into Python, rather than work in the
and the next three are co-ordinates of the other corner). The interpreter. So you need to put the first two lines from our E.T.
next number decides the block type (42 stands for iron, while phone home snippet at the top of the program, so that our ap
35 stands for wool). Wool is a special block that comes in 16 object is correctly set up. You can then import it into the
colours; we dont need to specify a colour, but if we wish to do interpreter or run it from the command line if you prefer. As
so, we can through an optional eighth parameter, which in well as dealing with individual pixels, we can also load images
general is referred to as block data. Weve used the
setBlock() function above this just sets up a single block,
so only requires one set of co-ordinates, plus the block type
and optional block data arguments. You can find a thorough
guide to the API at www.stuffaboutcode.com/p/minecraft-
api-reference.html, but well describe what everything does
as we use it.
For now, well turn our attention to coding on the Astro Pi.
This connects to the Pi via the General Purpose Input/Output
(GPIO) pins, but thanks to the astro_pi Python module, we
dont need to worry about coding at the level of individual
pins. The module provides some simple and powerful
functions for querying sensors and manipulating the LED
array. For example, we can display a suitably space-themed
message in an extraterrestrial shade of green using the
show_message function:
from astro_pi import AstroPi
ap = AstroPi()
ap.show_message(E.T. phone home..., text_colour = [0 Space case. This carefully machined and very strong case (its made of 6063
,255,0]) grade aluminium, dont ya know) will house the Pi on its space odyssey.
Sword-wielding
Steve stands
ominously close
to a USB port.
Something
about knives
and toasters...
W
ithin the human species, sorting things (putting concepts. Furthermore, it enables us to show some of
them into some kind of order) is perceived as Pythons advantages, including its no-nonsense syntax and
anywhere between a necessity (things disordered the simplicity by which we can use graphics. To make things
cause the most dire distress) and a dark ritual into which one easier, were going to work exclusively with lists of integers. All
should not delve. Computers, by comparison, universally of the concepts apply equally well to any other kind of data, of
prefer things to be sorted, because searching and indexing course, but this way we have a well-defined order (ascending
data is much easier if it is in order. Imagine how much less numerical) to aim for.
Quick useful would be an unsorted telephone directory. As such, When challenged with the drudgery-filled task of, say,
various sorting algorithms were developed early in the history putting 52 cards into order, first by suit and then within each
tip of computer science. These enable a modern computer to suit by face value, most people will flounder around with some
If youre using happily sort a several thousand-strong iTunes (err, haphazard piling and re-arranging, and eventually complete
a new version
Rhythmbox) library in a matter of seconds, listing tracks with the task (or give up and find something better to do).
of Matplotlib
with Gnome, you whatever ordering the user desires, be it lexicographical or Machines obviously need to be more systematic (and
might find that chronological. In theory, one could even re-enact Rob dedicated) in their approach. Its straightforward enough to
the animation Flemings autobiographical ordering (the order in which he come up with a couple of sorting algorithms that work, albeit
described in the
purchased them) from the Nick Hornby book High Fidelity, not very quickly. Well sum up some of these with pseudocode
box displays only
a blank window. albeit several orders of magnitude faster. below. Pseudocode is a freeform way of expressing code that
A workaround Sorting may not be the most glamorous of programming lies somewhere in between human language and the
is to add pylab. topics, and indeed Pythons built-in sorting methods will programming language. Its a good way to plan your
pause(0.001) after prove much faster than anything we program here, but it programs, but dont be disheartened when the translation
each draw() call.
serves as a great introduction to various programming becomes difficult. Weve actually overlooked a couple of
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. worry about the first few lines of pseudocode. Besides
Moving on to Insertion Sort, we translate the pseudocode def partition(l, low, high): animations,
into Python code, which does not look drastically different. storePos = low sorting
Add this to the sorting.py file. l[high], l[storePos] = l[storePos], l[high] algorithms can
def insertionsort(l): for j in range(low, high): be visualised as
wave diagrams.
for j in range(1, len(l)): if l[j] <= l[high]:
Find out more
k=j l[j], l[storePos] = l[storePos], l[j]
at http://
while k > 0 and l[k - 1] > l[k]: storePos += 1 sortvis.org.
l[k - 1], l[k] = l[k], l[k - 1] l[storePos], l[high] = l[high], l[storePos]
k -= 1 return storePos
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 > def quicksort(l, low, high):
0 to the while condition because the loop should stop before if low < high:
k gets to zero, rather than try (and fail) to retreat past the p = partition(l, low, high)
beginning of the list. Weve used our swapping shortcut quicksort(l, low, p - 1)
from before and in the final line use the -= notation, which quicksort(l, p + 1, high)
is short for k = k - 1. Try visualising this using the guide in the box on page
Again, add print statements to either the for or while 103 the pylab.draw() function should be called after the
loops to see the algorithms progress. Smaller elements final swap in the partition() function, so that the pivot
are swapped to the beginning of the list one place at a time element is plotted in the correct place.
until the list is sorted. There are many more sorting algorithms, and research is
The Quicksort implementation is a bit more involved. For ongoing to find more, but hopefully this introduction has
the partition operation, were going to take the bold step of shown you something of sorting as well as increased your
just using the first element for the pivot, so we dont need to Python skills. We have more fun lessons ahead. Q
www.techradarpro.com
twitter.com/techradarpro facebook.com/techradar
WorldMags.net
WorldMags.net
THE EASY WAY TO
LEARN WINDOWS
100%
JARGON
FREE
A
rguably more fun than the generously provided Assuming youve got your Pi up and running, the first step
Wolfram Mathematica: Pi Edition is Mojangs is downloading the latest version from http://pi.minecraft.
generously provided Minecraft: Pi Edition. The latter net to your home directory. The authors stipulate the use of
is a cut-down version of the popular Pocket Edition, and as Raspbian, so thats what wed recommend your mileage
such lacks any kind of life-threatening gameplay, but includes may vary with other distributions. Minecraft requires the X
more blocks than you can shake a stick at, and three types of server to be running so if youre a boot-to-console type youll
saplings from which said sticks can be harvested. have to startx. Start LXTerminal and extract and run the
This means that theres plenty of stuff with which to contents of the archive like so:
unleash your creativity, then, but all that clicking is hard work, $ tar -xvzf minecraft-pi-0.1.1.tar.gz
and by dint of the edition including of an elegant Python API, $ cd mcpi
you can bring to fruition blocky versions of your wildest $ ./minecraft-pi
dreams with just a few lines of code. See how smoothly it runs? Towards the top-left corner you
can see your x, y and z co-ordinates, which will change as you
navigate the block-tastic environment. The x and z axes run
parallel to the floor, whereas the y dimension denotes altitude.
Each block (or voxel, to use the correct parlance) which
makes up the landscape is described by integer co-ordinates
and a BlockType. The floor doesnt really have any depth,
so is, instead, said to be made of tiles. Empty space has the
BlockType AIR, and there are about 90 other more tangible
substances, including such delights as GLOWING_OBSIDIAN
and TNT. Your players co-ordinates, in contrast to those of
the blocks, have a decimal part since youre able to move
continuously within AIR blocks.
The API enables you to connect to a running Minecraft
instance and manipulate the player and terrain as befits your
megalomaniacal tendencies. In order to service these our first
task is to copy the provided library so that we dont mess with
the vanilla installation of Minecraft. Well make a special folder
for all our mess called ~/picraft, and put all the API stuff in
~/picraft/minecraft. Open LXTerminal and issue the
following directives:
$ mkdir ~/picraft
Dont try this at home, kids actually do try this at home. $ cp -r ~/mcpi/api/python/mcpi ~/picraft/minecraft
T
echnology has spoiled us with 32-bit colour, multi- colour quantization we first need to define our new restrictive
megapixel imagery. Remember all those blocky palette, which involves specifying the Red, Green and Blue
sprites from days of yore, when one had to invoke components for each of the 16 wool colours. This would be a
something called ones imagination in order to visualise what tedious process, involving importing an image of each wool
those giant pixels represented? In this tutorial we hark back colour into Gimp and using the colour picker tool to obtain
to those halcyon days from the comfort of Minecraft-world, as the component averages, but fortunately someone has done
we show you how to import and display graphics using blocks all the hard work already.
Standard setup
If youve used Minecraft: Pi Edition before All the files will be in a subdirectory called $ tar -xvzf mcimg.tar.gz
youll be familiar with the drill, but if not this is mcpi. To run Minecraft you need to have first $ cp -r ~/mcpi/api/python/mcpi ~/mcimg/
how to install Minecraft and copy the API for started X, then from a terminal do: minecraft
use in your code. $ cd ~/mcpi For this tutorial were going to use the PIL
Were going to assume youre using Raspbian, $ ./minecraft-pi (Python Imaging Library), which is old and
and that everything is up to date. You can It is a good idea to set up a working directory deprecated but is more than adequate for this
download Minecraft from http://pi.minecraft. for your Minecraft project, and to copy the API projects simple requirements. It can import
net, then open a terminal and unzip the file as there. The archive on the disk will extract into a your .jpg and .png files, among others, so theres
follows (assuming you downloaded it to your directory called mcimg, so you can extract it to no need to fiddle around converting images.
home directory): your home directory and then copy the api files Install it as follows:
$ tar -xvzf ~/minecraft-pi-0.1.1.tar.gz in the following way: $ sudo apt-get install python-imaging
We also need to resize our image Minecraft-world is only http://bit.ly/ca2015ref though it is a mistake without any
256 blocks in each dimension, so since we will convert one real consequence (phew). Padding out the palette in this
pixel to one block our image must be at most 256 pixels in its manner does however have the possibly unwanted side-effect
largest dimension. However, you might not want your image of removing any really black pixels from your image. This
taking up all that space, and blocks cannot be stacked more happens because their value is closer to absolute black (with
than 64 high, so the provided code resizes your image to 64 which we artificially extended the palette) than the very
pixels in the largest dimension, maintaining the original slightly lighter colour of the black wool. To work around this
aspect ratio. You can modify the maxsize variable to change you can change the (0,0,0) above to (25,22,22), so that there
this behaviour, but the resultant image will be missing its top are no longer any absolute blacks to match against. A
if it is too tall. reasonable hack if youre working with a transparent image is
The PIL module handles the quantization and resizing to replace this value with your images background colour,
with one-line simplicity, but we must first define the palette then the transparent parts will not get drawn. We make a new
and compute the new image size. The palette is given as a list single-pixel dummy image to hold this palette:
of RGB values, which we then pad out with zeroes so that it is mcImagePal = Image.new("P", (1,1))
of the required 8-bit order. For convenience, we will list our mcImagePal.putpalette(mcPalette)
colours in order of the blockData parameter. The provided archive includes the file test.png, which is in
mcPalette = [ fact the Scratch mascot, but you are encouraged to replace
221,221,221, # White this line with your own images to see how they survive the
219,125,62, # Orange {res,quant}ize. You can always TNT the bejesus out of them if
179,80,188, # Magenta you are not happy. To ensure the aspect ratio is accurate we
107,138,201, # Light Blue use a float in the division to avoid rounding to an integer.
177,166,39, # Yellow mcImage = Image.open("test.png")
65,174,56, # Lime Green width = mcImage.size[0]
208,132,153, # Pink height = mcImage.size[1]
64,64,64, # Dark Grey ratio = height / float(width)
154,161,161, # Light Grey maxsize = 64
46,110,137, # Cyan As previously mentioned, blocks in Minecraft-world do not
126,61,181, # Purple stack more than 64 high (perhaps for safety reasons). The
46,56,141, # Blue next codeblock proportionally resizes the image to 64 pixels
79,50,31, # Brown in its largest dimension.
53,70,27, # Green if width > height:
150,52,48, # Red rwidth = maxsize
25,22,22, # Black rheight = int(rwidth * ratio)
] else:
rheight = maxsize
mcPalette.extend((0,0,0) * 256 - len(mcPalette) / 3) rwidth = int(rheight / ratio)
Unfortunately the / 3 is missing from the code at If you have an image that is much longer than it is high,
Unlike in Doom,
this strawberry/
cacodemon
doesnt spit
fireballs at you.
This is good.
More dimensions
One of the earliest documentations of displaying counterpart has also done similar, though parts to jump around on. If you were to proceed
custom images in Minecraft:Pi Edition is Dav of Minecraft-Denmark were sabotaged by with this, then youd probably have to make
Stotts excellent tutorial on displaying Ordnance miscreants. Another fine example is Martin everything pretty small the drawing process
Survey maps, http://bit.ly/1lP20E5. Two- OHanlons excellent 3d modelling project. This is slow and painful. Naturally, someone (Henry
dimensional images are all very well, but Steve can import .obj files (text files with vertex, face Garden) has already taken things way too far
has a whole other axis to play with. To this end and texture data) and display them in Minecraft: and has written Redstone a Clojure interface
the aforementioned Ordnance Survey team has Pi Edition. Read all about it at http://bit. to Minecraft which enables movies to be
provided, for the full version of Minecraft, a ly/1sutoOS . Of course, we also have a temporal rendered. You can see the whole presentation
world comprising most of Great Britain, with dimension, so you could expand this tutorial in including a blockified Simpsons title sequence
each block representing 50m. Its Danish that direction, giving Steve some animated gifs at http://bit.ly/1sO0A2q.
for column 0 to 2:
if tile[row,column + 1] = tile[row,column]:
double tile[row, column]
for x in column + 1 to 2:
tile[row][x] = tile[row, x + 1]
empty tile[row,3]
Y
ou may have not encountered the opium-like approach here, which means that there will be a lot of self
moreishness of the Android/iOS game 2048, but flying around but dont worry, it makes it easier for the
once you do, youll get a taste of the thrilling addition Minecraft functions to talk to those to do with the board and
of successive powers of two and as an addict, like so many
hopeless lovers, youll be powerless to abandon the pursuit of
the cherished 2048 tile, always just beyond reach. Granted,
much of the pleasure comes from the touch interface: there
is an innate pleasure in orchestrating an elegant and board-
clearing sequence of cascades with just a few well-executed
swipes. Be that as it may, the game has simple rules and is
Quick based on blocks. As such, its perfect material for the next
tip instalment in our Minecraft:Pi Edition series.
Well worry about how to draw and move the blocks later.
You could make
The first challenge is to understand the algorithm underlying
this project a bit
less of a Zork-esque the game. So dutifully read the rules in the box they might
text adventure by seem clumsily worded or overly complex, but rest assured
writing a control they have been carefully crafted for easy translation to
system based on Python. Sometimes its easier to visualise the situation using
player moves. Youd
want to reset the
pseudocode this way we can see the shape of the program
players position without getting bogged down in details.
after each move, for each row:
but in principle move tiles left, clearing empty space
its quite a simple
You even get a handy help command, but it kind of looks
modification.
for each row: like a cheat for Sonic the Hedgehog
if len(empties) > 1:
The standard wool colour numbers dont really do high self.game_over = False
value blocks justice: eg 128 is represented by black.
rnd_pos = random.choice(empties)
rnd_n = random.randint(1,2)
the command interpreter. Honest. Lets look at line 2 of our self.tiles[rnd_pos[0]][rnd_pos[1]] = rnd_n
pseudocode, in which we move tiles in the row left, occupying self.drawBoard()
any available empty tiles. Python does have a remove() The drawBoard() function is what will actually place the
method for lists, but it only works on one element at a time, blocks in Minecraft world. Checking whether moves are Get the
so well clear the zeroes from our row the old-fashioned way. possible after the tile is added is a little awkward: code!
We dont waste time fiddling with empty rows, and we also # check row neighbours
need to keep track of whether we actually moved anything for j in range(self.boardSize): You can find the
code at http://
(through the boolean isMove). Also dont worry about the for k in range(self.boardSize - 1): www.linuxformat.
three other possible moves for now; we can cater for them if self.tiles[j][k] == self.tiles[j][k + 1]: com/files/
later with a very cunning trick. self.game_over = False mine2048.py.zip
def leftMove(self): or over at:
http://pastebin.
isMove = False # check col neighbours
com/5zEZUfBS
for row in range(self.boardSize): for j in range(self.boardSize):
j=0 for k in range(self.boardSize - 1):
while j < self.boardSize - 1: if self.tiles[k][j] == self.tiles[k + 1][j]:
# check rest of row non-empty self.game_over = False
row_empty = True
for k in range(j,self.boardSize):
if self.tiles[row][k] != 0:
row_empty = False
2048: An Open Source Odyssey
if self.tiles[row][j] == 0 and not row_empty: The 2048 game was written by young
for k in range(j,self.boardSize - 1): Italian programmer Gabriele Cirulli,
self.tiles[row][k] = self.tiles[row][k + 1] who wanted to see if he could write a
self.tiles[row][self.boardSize - 1] = 0 game in a weekend. The fruits of his
isMove = True labour proved hugely popular, accruing
else: over 4 million downloads within a week
j += 1 of its release in 2014. Numbers soared
after mobile versions were released the
Now we can deal with the situation in the second block in
following May, with up to 50,000
the pseudocode, finding two horizontally-adjacent tiles are
simultaneous players at its peak.
the same. The game is described as a clone of
for row in range(self.boardSize): Veewo Studios 1024 and conceptually
for column in range(self.boardSize - 1): similar to the indie title Threes!. Rather
if self.tiles[row][column] == self.tiles[row][column + 1] than profit from his success, Cirullis
\ blog says he didnt feel good about
and self.tiles[row][column] != 0: keeping [the code] private, since it was
self.tiles[row][column] += 1 heavily based off someone elses work.
for k in range(column + 1, self.boardSize - 1): Thus it is available to all at his GitHub
http://bit.ly/2048GitHub. In the
self.tiles[row][k]= self.tiles[row][k + 1]
spirit of open source, this spurred all
self.tiles[row][self.boardSize - 1] = 0
kinds of modifications and further
isMove = True depleted global productivity.
return isMove
Amazingly, thats the guts of the code all dealt with, but we decide whether or not the game is up. If it is all over, then we
still need a means by which to input our moves, and of course cheekily use raw_input() as a means to wait for the user to
we need to render the results in Minecraft world. press Return. The cmd module works by subclassing its Cmd
class. Any class you write that instantiates this will inherit all
Master and commander of its functionality. We use the standard boilerplate to start
Well use the cmd module to provide a simple command line things up when the program is executed:
interface to our game. You may have seen this already in if if __name__ == __main__:
youve ever made a Minecraft cannon and used cmd to command2048().cmdloop()
control it (You could always try making an image wall, p110). This will instantiate our command interpreter when the
We shall have a command for initialising the board, four program is run with:
directional commands and a command to quit, Ctrl-D, aka the $ python mine2048.py
end of file (EOF) character or the quick way to end a Python Minecraft must be running when you do this, or youll run
session. We even get a help command for free (see picture, into errors. Also youll need to make sure youve copied the
page 114). When you enter a command, for example left, the Python API from your Minecraft install (e.g. ~/mcpi/api/
cmd module will run the function with that name prefixed by python/mcpi) to a subdirectory called minecraft in the
do_, hence we have a function do_left() which calls the same directory as the mine2048.py file from the download.
leftMove() function above. This function is really part of our But if youve been following our Minecraft series youll be au
board class. fait with all this.
The do_left() function will check if the left move is valid, The command2048 classs __init__() method sets up the
and if so update the board, via the imaginatively-titled basics, including setting up the mc object and using it to get
updateBoard() function, which in turn will add a new tile and the players position. This provides a reference point for
WorldMags.net
WorldMags.net
Coding projects
Exciting and interesting coding
projects for you to write
Python 3.0 primer ..................................................120
Build a Gimp plug-in .........................................124
Image filters .............................................................................128
Sound filters ............................................................................134
Twitter scanner .............................................................138
Build a GUI 142
...................................................................................
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 The PyStone benchmark will likely be slower in Python 3, but the same wont
regions that do tend to use different sets of accents to be true for all code. Dont be a Py3k refusenik without first trying your code.
decorate the characters. As a result, besides the ASCII
standard, numerous diverse and incompatible character when printed to a terminal, are converted to whichever
encodings have emerged. Each grapheme (an abstraction of encoding your system's locale specified (through the LANG Quick
a character) is assigned a codepoint, and each codepoint is and LC_* environment variables in Linux). For any modern tip
assigned a byte encoding, sometimes identically. In the past, distro, this is probably UTF-8, but it's definitely not something Arch Linux is one
if you wanted to share a document with foreign characters in you should take for granted. of few distributions
it, then plain ASCII wouldn't help. You could use one of the The unicode type should be used for textual intercourse to use Python 3
alternative encodings, if you knew the people you were finding the length of, slicing or reversing a string. For example, by default, but it
can live happily
sharing it with could do the same, but in general you needed the Unicode codepoint for the lowercase Greek letter pi is
in tandem with
to turn to a word processor with a particular font, which just 03c0 in hex notation. So we can define a unicode string from its predecessor
moves the problem elsewhere. Thankfully, we now have a the Python console like so, provided our terminal can handle (available in the
widely adopted standard: Unicode (see The Unicode Unicode output and is using a suitable font: python2 package).
Revolution box, above) that covers all the bases, and is >>> pi = u'\u03c0'
backwards compatible with ASCII and (as far as codepoints >>> print(pi)
are concerned) its Latin-1 extension. We can even have
Unicode in our domain names, although internally these are >>> type(pi)
all still encoded as ASCII, via a system called Punycode. <type 'unicode'>
Python 2 is far from devoid of Unicode support, but its >>> len(pi)
handling of it is done fairly superficially (Unicode strings are 1
sneakily re-encoded behind the scenes) and some third-party However, if we were to try this on a terminal without
modules still won't play nicely with it. Strings in Python 2 can Unicode support, things will go wrong. You can simulate such
be of type str (which handles ASCII fine, but will behave a scenario by starting Python with:
unpredictably for codepoints above 127) or they can be of $ LC_ALL=C python
type unicode. Strings of type str are stored as bytes and, Now when you try to print the lowercase character pi, you will
on your Python 2 code, with the goal of emitting bona fide behave like a standard module instead it acts as a compiler
Python 3 code. Using it can be as simple as directive, which in this case provides the modern print syntax.
$ 2to3 mypy2code.py We can likewise import division to get the new style division,
which will output a diff of any changes against the original file. or unicode_literals to make strings Unicode by default. There
You can also use the -w option to overwrite your file don't is another module, confusingly called future, which isn't part
worry, it will be backed up first. Some fixers will only run if you of the standard library, but can help ease transition issues.
specify them explicitly, using the -f option. An example is When Python 3.0 was released, it was generally regarded
buffer which will replace all buffer types with memoryviews. as being about 10% slower than its predecessor. This was not
These two aren't entirely compatible, so some tweaking might surprising, because speed was not really a priority for the new
be necessary in order to successfully migrate. Using 2to3 can version and many special-case optimisations were removed
save you a whole heap of effort, because searching and in order to clean up the code base. Now that we're up to
replacing print statements manually, for instance, is a version 3.5 (which was released in September 2015), it
drudgerous task. The pictured example (over on the left, on would be nice if performance had since been improved.
p122) shows the changes to a simple three-line program Unfortunately, this hasnt been the case, which you can verify
the unichr() function is now chr(), because Unicode is now for yourself using the PyStone benchmark.
implicit, and the print line is reworked, even if it uses % to We tested it (see the screenshot on p121) on an aging
format placeholders. machine in the office, which has now come back from the
dead twice and hence possesses supernatural powers far
Parlez-vous Python Trois? beyond its dusty 2.3GHz processor would suggest. But
Another option is to create 'bilingual' code that is compatible don't be misled; PyStone tests various Python internals, of
with both Python 2 and Python 3. While you should really only which your code may or may not make extensive use. Its
target one specific Python version, this middle ground is very important to test your code in both versions to get a more
useful when you're porting and testing your code. You might accurate picture. You can always use Cython (maintained at
need to rework a few things in Python 2 while still enjoying the http://cython.org) to speed up code that is amenable to
new features and learning the new syntax. Many Python 3 C translation (loops, math, array access), or the bottleneck
features and syntaxes have already been backported to 2.7, module.
and many more can be enabled using the __future__ module. Guido van Rossum, the author of Python, says that Python
For example, you can get the new-fangled print syntax by 2.7 will be supported until 2020, but that's no excuse to
using the following: postpone learning the new version now. Python 3 adoption is
>>> from __future__ import print_function increasing, so you won't be alone. Half the work is retraining
This is used with the from in this way, and __future__ doesn't your fingers to add parentheses. Q
Applying our
pdb.gimp_context_set_foreground('#00a000') in the highlights of the image, reminiscent of lens flare. The bokeh plugin
pdb.gimp_context_set_brush_size(128) effect you get in each case is a characteristic of the lens and has created
pdb.gimp_paintbrush_default(layer,2,[160,100]) the aperture depending on design, one may also see a pleasing
If you have the brush called Cell 01 available, then the polygonal and doughnut-shaped bokeh effects. For this bokeh effect in
above code will draw a green splodge in the middle of your exercise, well stick with just circular ones. the highlights.
canvas. If you dont, then youll get an error message. You can Our plugin will have the user pinpoint light sources using
get a list of all the brushes available to you by calling pdb. a path on their image, which we will assume to be single-
gimp_brushes_get_list(). The paintbrush tool is more layered. They will specify disc diameter, blur radius, and hue
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 Our plugin creates a layer with
gradients and fades too. For simplicity, we have just used the
defaults/current settings here. Download the code pack from
bokeh discs and another with
http://bit.ly/TMS12code and you will find a file linesplodge. a blurred copy of the image
py which will register this a fully-fledged Gimp plugin, replete
with a few tweaks. and saturation adjustments. The result will be two new layers:
For the rest of the tutorial we will describe a slightly more a transparent top layer containing the bokeh discs, and a
advanced plugin for creating bokeh effects in your own layer with a blurred copy of the original image. The original
pictures. Bokeh derives from a Japanese word meaning blur layer remains untouched beneath these two. By adjusting the
or haze, and in photography refers to the out-of-focus effects opacities of these two new layers, a more pleasing result may
caused by light sources outside of the depth of field. It often be achieved. For more realistic bokeh effects, a part of the
results in uniformly coloured, blurred, disc-shaped artefacts image should remain in focus and be free of discs, so it may
Softly, softly
Finally we apply our hue and lightness adjustments, and set To finish, group the operations
the bokeh layer to Soft-Light mode, so that lower layers are
illuminated beneath the discs. And just in case any black
into a single undoable one, and
survived the bucket fill, we use the Color-To-Alpha plugin to reset any changed tool settings
squash it out.
pdb.gimp_hue_saturation(bokeh_layer, 0, 0, lightness, Critics may proffer otiose jibes about the usefulness of
saturation) this script, and indeed it would be entirely possible to do
pdb.gimp_layer_set_mode(bokeh_layer, SOFTLIGHT_ everything it does by hand, possibly even in a better way.
MODE) That is, on some level at least, true for any Gimp script. But
pdb.plug_in_colortoalpha(timg, bokeh_layer, '#000000') this manual operation would be extremely laborious and
And that just about summarises the guts of our script. You error-prone youd have to keep a note of the coordinates
will see from the code on the disc that there is a little bit of and colour of the centre of each disc, and youd have to be
housekeeping to take care of, namely grouping the whole incredibly deft with your circle superpositioning if you wanted
series of operations into a single undoable one, and restoring to preserve the colour addition. Q
iPAD
PRO
Worth the
upgrade?
A
udio is an important means of communication and, we use to define the length of the audio buffer. The data
truth be told, working with multimedia is fun. captured in this buffer can then either be discarded or saved.
However, working with audio is a challenging task PyAudio uses chunks of data, instead of a continuous amount
because it involves handling a variety of devices, standards of audio, for a couple of reasons. For one, reading data in
and formats. There are several libraries that you can use chunks helps use resources efficiently. Recording a
with Python to capture and manipulate sound, and make continuous flow of data from the microphone would just eat
interactive apps. One of the better platform-independent up the processor and may lead to memory leaks on devices
libraries to make sound work with your Python application is such as the Raspberry Pi, which has a limited amount of
PyAudio. The library can help you record audio as well as play RAM. Another advantage of reading data in chunks, as we will
back sounds without writing oodles of code. see later on, is that it enables us to analyse the data and only
The PyAudio library provides Python bindings for the save those relevant bits that meet our criteria, and discard
popular cross-platform PortAudio I/O library. It can be the rest.
easily installed with a simple pip install pyaudio command. In a real program, youll want to define these parameters
As with all programs, you need to begin any PyAudio program in variables, because theyll be used in other places in the
by importing the library. PyAudio is generally complemented program as well. Along with the above, we will also define
with the WAV library to capture (or play) audio in the lossless two variables:
WAV format: record_seconds = 10
import pyaudio output_filename = Monoaudio.wav
import wave Now comes the code that does the actual audio capture:
print ( Now recording audio. Speak into the microphone. )
Capture audio frames = [ ]
Once youve imported the libraries, you can quickly cobble
together the code to open the PyAudio recording stream. for i in range(0, int(rate / chunk * record_seconds)):
among other things, checks the captured data for silence. elif not silent and not snd_started:
def record(): snd_started = True
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=1, rate=RATE, if snd_started and num_silent > 100:
input=True,frames_per_buffer=CHUNK_ break
SIZE) If the captured data is above the threshold of silence and
we havent been recording yet, well set the snd_started
num_silent = 0 variable to True to begin recording sound. But if weve been
snd_started = False recording sound and the captured data is below the silence
threshold, well increment the num_silent variable. Moving
r = array(h) on, well stop recording if weve been recording but its been
100 frames since the last word was spoken.
while 1: Next, well call another function to manipulate the
snd_data = array(h, stream.read(CHUNK_SIZE)) recording and pad the audio with 1.5 seconds of blank sound.
Adding silence is simple. Well just read the recorded data into
if byteorder == big: an array and then extend it by adding blank frames.
snd_data.byteswap() def add_silence(snd_data, seconds):
r.extend(snd_data) r = array('h, [0 for i in range(int(seconds*RATE))])
The function records words from the microphone and r.extend(snd_data)
returns the data as an array of signed shorts. We next check r.extend([0 for i in range(int(seconds*RATE))])
whether this recorded data contains silence by calling the return r
is_silent function with silent = is_silent(snd_data) . Weve The complete script is on our GitHub at https://github.
defined the is_silent (snd_data) function elsewhere in the com/geekybodhi/techmadesimple-pyaudio/. When you
script. It uses the max(list) method, which returns the execute it, it will detect and start capturing audio frames but
elements from a list (snd_data in this case) with the discard them until it detects a voice. From here on, itll save all
maximum value. This function will return True if the snd_data consequent frames until it detects a period of silence.
is below the silence threshold. The threshold intensity defines The script also contains comments to help you extend it
the silence-to-noise signal. A value lower than the threshold by writing a trim function thatll trim the silent portion from
intensity is considered silence. Once the is_silent function the end and even from the start, if it exists. Although there are
returns a value (either True or False), well then subject it to several other Python libraries for playing sound, PyAudio is
further tests: one of the easiest to use with third-party audio toolkits that
if silent and snd_started: you can use to capture and manipulate sound, so have fun
num_silent += 1 playing around with your sound! Q
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
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:
T
witter is a wonderful tool if you want to get a feel for for status in tweepy.Cursor(api.user_timeline).items():
what the world is thinking. The visible 140-character print(status.text)
social network is powered by a wonderful API that The code will print all status updates in groups of 20 from
enables users to drink from the firehose and capture all the your own timeline. We use Tweepys Cursor interface, which
raw data passing over the network. Once captured, you can supports several different types of objects. Here we have
use this data to mine all kinds of meaningful information. You used the items method to iterate through our timeline. To
can, for example, find trends related to specific keywords or limit the number of tweets you wish to print, you can also
gauge sentiments during events. change the statement to something like tweepy.Cursor(api.
Tweepy is one of the easiest libraries that you can use to user_timeline).items(10) , which will only print the 10 most
access the Twitter API with Python. In this tutorial, well be recent tweets.
using Tweepy to capture any tweets that mention OggCamp,
and then well mine the data to print a list of the most Capture tweets
frequently used hashtags. As you can see, interacting with Twitter via Python is
You can easily install Tweepy with pip install tweepy . reasonably straightforward. Twitter also offers a set of
Quick Tweepy accesses Twitter via the OAuth standard for streaming APIs, however, and by using these, you can access
tip authorisation. For that, youll first have to head over to Twitters global stream of tweets. The public streams work
Scroll through http://apps.twitter.com and sign in with your regular really well for following specific topics and mining data. Well
Tweepys
Twitter credentials. Now press the Create New App button import and modify Tweepys StreamListener class to fetch
documentation
(http://docs. and fill in all the fields except for the Callback URL, which you the tweets.
tweepy.org/ can leave blank, and then click on the Create button. Once the from tweepy import Stream
en/latest/api. app has been created, you can hop over to the Keys and from tweepy.streaming import StreamListener
html) for a list of Access Tokens tab and create an OAuth token. This might from tweepy import OAuthHandler
supported methods.
take a minute or two. Once its done, make a note of the
class StdOutListener(StreamListener):
Anatomy of a tweet
The 140 characters of the tweet that are visible representation of the user who has contributed used (you) has marked the tweet as favourite or
on your timeline are like the tip of the iceberg. A to the tweet. Then comes the text field, which retweeted it. Then there are the favorite_count
complete tweet in its JSON form contains a lot weve used extensively in the tutorial this and retweet_count fields, which contain the
more information and attributes in addition to contains the actual text of the status update. number of times the tweet has been favourited
the tweet itself. A knowledge of the important Weve also use the entities field, which contains and retweeted respectively.
ones will help you extract the relevant data. information that has been parsed out of the text Also important is the lang field, which
The JSON string begins with the created_at of the tweet, such as URLs and hashtags. contains the two-character language identifier of
field, which lists the UTC time when the tweet Besides these, theres the favorited and the tweet, such as en or de. The place and geo
was created. It is followed by the id and the retweeted fields, Boolean fields that indicate fields contain information that points to the
id_str fields, which are the integer and string whether the user whose credentials are being geographic location of the tweet.
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)
root = tk.Tk()
app = App(master=root)
app.master.title(Sample Application)
P
ython provides various options for developing app.master.geometry(250x70+550+150)
graphical user interfaces, such as wxPython, JPython app.mainloop()
and Tkinter. Of these, Tkinter is the standard GUI Well explain the individual elements of the code as we
library that ships by default with Python. Tkinter is the Python build our calculator. For now, its important to note that weve
interface to Tk, the GUI toolkit for Tcl/Tk. Tk was developed explicitly created an instance of Tk with root = tk.Tk() .
as a GUI extension for the Tcl scripting language in the early Tkinter starts a Tcl/Tk interpreter behind the scenes, which
1990s. One of the reasons for its popularity is that its easier then translates Tkinter commands into Tcl/Tk commands.
to learn than other toolkits. The main window of an application and this interpreter are
Tkinter provides a powerful object-oriented interface to intrinsically linked, and both are required in order for a Tkinter
the Tk GUI toolkit. To use Tkinter, you dont need to write Tcl application to work. Creating an instance of Tk initialises
code, because Tkinter is a set of wrappers that implement the this interpreter and creates the root window. If you dont
Tk widgets as Python classes. explicitly initialise it, one will be implicitly created when you
Creating a GUI application using Tkinter is an easy task. create your first widget. While this is perfectly fine, it goes
Quick We begin by first importing the Tkinter module, which is then against the spirit of Python, which states that explicit is
tip used to create the graphical apps main window. Next we add better than implicit.
one or more of the supported widgets (see box on page 143)
The code in this
tutorial is based on
to the main window, before entering the main event loop to Graphical calculator
vegaseats Updated take action against each event triggered by the user. For As you can see, it doesnt take much effort to drape a Python
Tiny Tkinter example, the following code will display a window: app with a graphical interface. Well use the same principles
Calculator, listed import tkinter as tk to write our graphical calculator. Well add various widgets to
on DaniWeb.com window = tk.Tk() our calculator, including an editable display, which can be
(http://tinyurl.
# Insert code to add widgets used to correct mistakes and to enter symbols and
com/h3eotvf).
window.mainloop() hexadecimals not on our calculators keypad. For example, if
you enter 0xAA, itll print the decimal equivalent, that is 170. variables with the __init__ method in the class body. This
Similarly, the editable display can also be used to manually method is executed automatically when a new instance of the
type in functions that arent represented on the keypad but class is created. Python passes the instance as the first
are defined in Pythons math module, such as the logarithmic argument, and it is a convention to name it self. In other
and trigonometric functions. Furthermore, our calculator will words, the __init__ method is the constructor for a class in
also have the ability to temporarily store and retrieve a result Python. The basic idea is that it is a special method, which
from its memory bank. is automatically called when an object of that class is created.
The complete code for the calculator is available online. So when you call Calculator() , Python creates an object for
Lets dissect individual elements to get a better grasp of the you, and then passes it as the first parameter to the __init__
Tkinter library and its interactions with Python. method.
import tkinter as tk The self variable and the __init__ method are both OOP
from math import * constructs. The self variable represents the instance of the
from functools import partial object itself. Most object-oriented languages pass this as a
hidden parameter to the methods defined on an object, but
class Calculator(tk.Tk): Python does not. You have to declare it explicitly. When you
def __init__(self): create an instance of the Calculator class and call its
tk.Tk.__init__(self) methods, it will be passed automatically. It is important to use
We begin by importing the various libraries and functions the self parameter inside an objects method if you want to
for our calculator. Besides the Tkinter toolkit, well also need persist the value with the object.
the math library to handle the calculations. The partial
function from the functools module helps us write reusable Define the layout
code. Well explain how weve used it later in the tutorial. The first order of business is to create the graphical interface
After importing the libraries, we begin defining the for the calculator. In this section we define different
Calculator class. To begin with, we initialise the instance parameters for the positioning and appearance of our
WorldMags.net
WorldMags.net
Learn as you code with stacks of Get to grips with hacking and coding Take coding concepts further with our
example-packed guides with the amazing Raspberry Pi exciting and easy-to-follow projects
9001