Table Of Contentcodin
G
ro ec s
P J T
PY THON
®
R
IN
!
H e l l o , W o r l d
US_001_half_title.indd 1 16/01/17 9:43 am
US_002-003_full_title.indd 2 16/01/17 9:43 am
codin
G
ro ec s
P J T
PY THON
®
R
IN
US_002-003_full_title.indd 3 06/02/17 4:00 pm
DK UK
Senior editors Ben Morgan, Steve Setford
Senior art editor Peter Radcliffe
US editors Jill Hamilton, Margaret Parrish
Consultant editor Craig Steele
Jacket design development manager Sophia MTT
Jacket editor Claire Gell
Producer, pre-production Robert Dunn, Nadine King
Producer Anna Vallarino
Managing editor Lisa Gillespie
Managing art editor Owen Peyton Jones
Publisher Andrew Macintyre
Associate publishing director Liz Wheeler
Art director Karen Self
Design director Phil Ormerod
Publishing director Jonathan Metcalf
DK INDIA
Project editor Suefa Lee
Art editor Sanjay Chauhan
Assistant editor Isha Sharma
Assistant art editors Yashashvi Choudhary,
Simar Dhamija, Sonakshi Singh
Jacket designer Juhi Sheth
Jackets editorial coordinator Priyanka Sharma
Managing jackets editor Sreshtha Bhattacharya
DTP designer Sachin Gupta
Senior DTP designer Harish Aggarwal
Senior managing editor Rohan Sinha
Deputy managing art editor Anjana Nair
Pre-production manager Balwant Singh
First American Edition, 2017
Published in the United States by DK Publishing
345 Hudson Street, New York, New York 10014
Copyright © 2017 Dorling Kindersley Limited
DK, a Division of Penguin Random House LLC
17 18 19 20 21 10 9 8 7 6 5 4 3 2 1
001–299420–June/2017
All rights reserved.
Without limiting the rights under the copyright reserved above, no part of this publication may be reproduced,
stored in or introduced into a retrieval system, or transmitted, in any form, or by any means (electronic, mechanical,
photocopying, recording, or otherwise), without the prior written permission of the copyright owner.
Published in Great Britain by Dorling Kindersley Limited.
A catalog record for this book is available from the Library of Congress.
ISBN 978-1-4654-6188-9
DK books are available at special discounts when purchased in bulk for sales promotions, premiums,
fund-raising, or educational use. For details, contact: DK Publishing Special Markets, 345 Hudson Street,
New York, New York 10014 or SpecialSales@dk.com
Printed in China
A WORLD OF IDEAS:
SEE ALL THERE IS TO KNOW
www.dk.com
US_004-005_imprint.indd 4 16/01/17 5:39 pm
CAROL VORDERMAN MBE is one of Britain’s best-loved TV presenters and
is renowned for her mathematical skills. She has hosted numerous TV shows
on science and technology, from Tomorrow’s World to How 2, and was co-host
of Channel 4’s Countdown for 26 years. A Cambridge University engineering
graduate, she has a passion for communicating science and technology
and has a keen interest in coding.
CRAIG STEELE is a specialist in Computing Science education. He is Project
Manager for CoderDojo Scotland, which runs free coding clubs for young people.
Craig has previously worked for the Raspberry Pi Foundation, Glasgow Science
Centre, and the BBC micro:bit project. Craig’s first computer was a ZX Spectrum.
DR. CLAIRE QUIGLEY studied Computing Science at Glasgow University,
where she earned a BS and PhD. She has worked in the Computer Laboratory
at Cambridge University and Glasgow Science Centre, and is currently
working on a project to develop a music and technology resource for primary
schools in Edinburgh. She is a mentor at CoderDojo Scotland.
DR. MARTIN GOODFELLOW has a PhD in computer science and experience
of teaching coding up to university level. He has developed educational
content and workshops for CoderDojo Scotland, Skills Development
Scotland, Glasgow Life, and Highlands and Islands Enterprises, and has
consulted on digital content for the BBC. He is currently the Scottish
Ambassador for National Coding Week.
DANIEL McCAFFERTY holds a degree in Computer Science from the University
of Strathclyde. He has worked as a software engineer for companies big and
small in industries from banking to broadcasting. Daniel lives in Glasgow with
his wife and daughter and when not teaching young people to code, he enjoys
bicycling and spending time with family.
DR. JON WOODCOCK studied physics at Oxford University and computational
astrophysics at the University of London. An avid coder since the age of eight,
he has programmed all kinds of computers from single-chip microcontrollers
to world-class supercomputers. He is author of DK’s bestselling Coding Games
in Scratch and has written or contributed to six other DK coding books.
US_004-005_imprint.indd 5 06/02/17 4:00 pm
Contents
8 FOREWORD
STARTING WITH PYTHON
12 What is coding?
14 Meet Python
16 Installing Python
18 Using IDLE TURTLE GRAPHICS
72 Robot Builder
FIRST STEPS
82 Kaleido-spiral
90 Starry Night
98 Mutant Rainbow
22 Your first program
24 Variables
28 Making decisions
32 Loopy loops PLAYFUL APPS
36 Animal Quiz
44 Functions
48 Fixing bugs 110 Countdown Calendar
52 Password Picker 120 Ask the Expert
58 Modules 130 Secret Messages
60 Nine Lives 142 Screen Pet
US_006-007_Contents.indd 6 02/03/17 4:53 pm
Let’sssss get
sssstarted!
GAMES IN PYTHON
158 Caterpillar
168 Snap
180 Matchmaker
190 Egg Catcher
REFERENCE
202 Project reference
220 Glossary
222 Index
224 Acknowledgments
Find out more at:
www.dk.com/computercoding
US_006-007_Contents.indd 7 02/03/17 4:53 pm
Foreword
We live in a digital world, and computers are part of almost everything we do. Not so
long ago, computers were bulky, noisy machines that lived mainly on desks, but now
they are tiny, silent devices hidden inside our phones, cars, TVs, and even watches.
We use them to work, play games, watch movies, go shopping, and keep in touch with
our friends and family.
Today’s computers are so simple to use that anyone can operate them. But not as
many people know how to write the code that makes them work. Becoming a coder
allows you to look under the hood and see how a computer really works. With a bit
of practice, you can build your own apps, write your own games, or just tinker with
other people’s programs and customize your own ingenious creations.
As well as being an addictive hobby, coding is a skill that’s in huge demand all over
the world. Learn how to code and it will set you in good stead wherever your life
leads, whether you’re interested in science, art, music, sport, or business.
Today, there are hundreds of coding languages you can learn, from simple, drag-and-
drop languages like ScratchTM to web-programming languages like JavaScript®. This
book is based on Python®, one of the world’s most widely used coding languages.
Equally popular with students and professionals, Python is easy to pick up yet powerful
and versatile. It’s a great language to learn whether you’re a beginner or moving up
from a simple language like Scratch.
US_008-009_Foreword.indd 8 16/01/17 9:43 am