Statistics
4
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-05-03

AuthorDK

Using fun graphics and easy-to-follow instructions, this straightforward, this visual guide shows young learners how to build their own computer projects using Python, an easy yet powerful free programming language available for download. Perfect for kids ages 10 and over who are ready to take a second step after Scratch, Coding Projects in Python teaches kids how to build amazing graphics, fun games, and useful apps. All they need is a desktop or laptop, and an Internet connection to download Python 3. Step-by-step instructions teach essential coding basics like loops and conditionals, and outline 14 fun and exciting projects, including a script that cracks secret codes, a quiz to challenge family and friends, a matching game, and much more. When they are feeling more confident, kids can think creatively and use the tips and tricks provided to personalize and adapt each project. The simple, logical steps in Coding Projects in Python are fully illustrated with fun pixel art and build on the basics of coding, so kids can have the skills to build whatever kind of project they can dream up. Supporting STEM education initiatives, computer coding teaches kids how to think creatively, work collaboratively, and reason systematically, and is quickly becoming a necessary and sought-after skill. DK's computer coding books are full of fun exercises with step-by-step guidance, making them the perfect introductory tools for building vital skills in computer programming.

Tags
No tags
ISBN: 1465461884
Publisher: DK Children
Publish Year: 2017
Language: 英文
Pages: 224
File Format: PDF
File Size: 21.9 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
(This page has no text content)
H e l l o W, o r l !d ProJecTs IN PYTHON codinG ®R 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
ProJecTs IN PYTHON®R codinG US_002-003_full_title.indd 3 06/02/17 4:00 pm
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 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 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. 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. 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. 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 F O R E W O R D STARTING WITH PYTHON 12 What is coding? 14 Meet Python 16 Installing Python 18 Using IDLE FIRST STEPS 22 Your first program 24 Variables 28 Making decisions 32 Loopy loops 36 Animal Quiz 44 Functions 48 Fixing bugs 52 Password Picker 58 Modules 60 Nine Lives TURTLE GRAPHICS 72 Robot Builder 82 Kaleido-spiral 90 Starry Night 98 Mutant Rainbow PLAYFUL APPS 110 Countdown Calendar 120 Ask the Expert 130 Secret Messages 142 Screen Pet US_006-007_Contents.indd 6 02/03/17 4:53 pm
Let’sssss get sssstarted! Find out more at: www.dk.com/computercoding GAMES IN PYTHON 158 Caterpillar 168 Snap 180 Matchmaker 190 Egg Catcher REFERENCE 202 Project reference 220 Glossary 222 Index 224 Acknowledgments 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
The best way to learn to code is to get immersed, and that’s how this book is designed to work. Just follow the numbered steps and you’ll be building apps, games, graphics, and puzzles in no time. Learning to code is easier if you’re having fun, so we’ve tried to make the projects as much fun as possible. If you’re new to programming, start at the beginning and work your way through. Don’t worry if you don’t understand every detail—it doesn’t matter. The more projects you build, the better you’ll get. And don’t worry if your programs don’t work the first time you run them. Even the pros have to debug their work. Once you’ve finished building each project, there are tips on how to tweak and adapt it. Feel free to try your own hacks. With a little bit of imagination and skill, there’s no limit to what a coder can achieve. Have fun coding! US_008-009_Foreword.indd 9 16/01/17 9:43 am
US_010-011_Chapter_1_opener.indd 10 16/01/17 9:43 am
Starting with Python US_010-011_Chapter_1_opener.indd 11 16/01/17 9:43 am
12 S T A R T I N G W I T H P Y T H O N What is coding? Computer programmers, or “coders,” are people who write step-by-step instructions that can make a computer perform a task. Coders can get computers to do addition, make music, move a robot across a room, or fly a rocket to Mars. Dumb boxes A computer can’t do anything of its own accord—it just sits there like a dumb box until it’s told exactly what to do. Because computers can’t think for themselves and can only do as they’re told, coders have to do the thinking for them and write their instructions carefully. Programming languages In order to tell a computer what to do, you need to learn a programming language. Visual languages are easy for beginners to learn, while professional coders use text-based languages. This book is based on the popular text-based language Python. ▽ Scratch Scratch is a visual programming language. It’s great for creating games, animations, and interactive stories. You write code in Scratch by snapping together blocks of instructions. ▽ Python Python is a text-based programming language. In Python, programmers write code using words, abbreviations, numbers, and symbols. Instructions are typed in using the computer’s keyboard. △ Performing pet By learning how to code, you’ll be able to write your own programs and make the computer do what you want. It’s a bit like having an electronic pet that you can teach to perform tricks! Both these bits of code do the same thing. The answer to the sum is shown on the screen in a “thinks” bubble. You hit the enter/return key to see the result. >>> 3 + 3 6 Why don’t you say something? 6 when clicked think 3 + 3 US_012-013_What_is_coding.indd 12 02/03/17 4:53 pm
13W H A T I S C O D I N G ? Anyone can code To be a coder you just need to learn a few basic rules and commands, and then you can start writing programs to suit your skills and interests. If you’re into science, for example, you could make an app that draws graphs from the results of your experiments. Or you could use your art skills to design an alien world for your own video game. Get coding Coding may sound daunting, but learning how to do it is easy. The secret is to just jump in. This book is designed to teach you how to code by guiding you through simple projects. Just follow the numbered steps and you’ll be creating games, apps, and digital art in no time. ▽ Think logically Coders need to think logically and carefully to write good code. If the instructions aren’t quite right or the steps are in the wrong order, a program won’t work properly. Think through each step and make sure things happen in a logical order—after all, you wouldn’t put your coat on before your sweater, would you! ▽ Pay attention to detail If you’re good at spot-the-difference puzzles, you’ll probably be a great coder. An important skill in coding is spotting mistakes in your code. These mistakes are called bugs, and even tiny bugs can cause big problems. Eagle-eyed coders can pick out spelling mistakes and faults with the logic or order of the instructions. Debugging a program can be tricky, but learning from your mistakes is a great way to improve your coding powers. I knew you’d get that wrong! Keep those eyes peeled! I’m on a bug hunt! L I N G O Bugs Bugs are errors in code that make programs behave in unexpected ways. They are so-called because early computers sometimes went wrong when insects got stuck in their circuits! US_012-013_What_is_coding.indd 13 12/01/17 2:33 pm
14 S T A R T I N G W I T H P Y T H O N Meet Python Python is one of the most popular computer programming languages in the world. It was first released in the 1990s and is now used to build millions of apps, games, and websites. Why Python? Python is a great language for getting started with computer programming. Many schools and universities use it to teach coding. Here are some of the reasons that Python’s so useful. It’s easy to read and write! △ Easy to read and write Python is a text-based computer programming language. You write the instructions using a mixture of English words, punctuation characters, symbols, and numbers. This makes Python code simple to read, write, and understand. ▽ Batteries included Programmers say Python has “batteries included.” This is because it comes with everything you need to start coding right away. △ Works everywhere Python is portable. This means you can write and run Python code on lots of different computers. The same Python code will work on PCs, Macs, Linux machines, and Raspberry Pi computers. The programs behave the same way on each machine. ▷ Great support Python has well-written documentation. It has a guide to getting started, a reference section for looking up what things mean, and a bunch of example code. △ Handy tools Python is packed with lots of useful tools and preprogrammed code that you can use in your programs. This is called the Standard Library. Using these tools makes it easier and quicker for you to build your own programs. L I N G O Python Python isn’t named after the type of snake. It’s actually named after a British comedy group called “Monty Python’s Flying Circus.” The creator of Python, Guido van Rossum, was a big fan of the group and their quirky humor. Python programmers often use the group’s jokes and famous quotes in their code as a tribute. US_014-015_Meet_Python.indd 14 02/03/17 4:53 pm
15M E E T P Y T H O N Python in action Python isn’t just an educational tool. It’s such a powerful program it’s used for many interesting and exciting tasks in business, medicine, science, and the media. It can even be used to control the lights and heating in your home. E X P E R T T I P S The interpreter Some programming languages use an interpreter. The interpreter is a program that can translate from one programming language into another. Every time you run a Python program, the interpreter translates each line of Python code into a special code that the computer can understand, known as machine code. Action! I’m a mighty powerful program! ▽ Crawling the web Python is widely used on the Internet. Parts of Google’s search engine are written in Python. Much of YouTube is also built using Python code. △ Serious business Python helps banks keep track of the money in their accounts, and big store chains to set the prices of the goods they sell. △ Out of this world Software engineers used Python to create tools for NASA’s Mission Control Center. These tools help the crew prepare for and monitor the progress of each mission. △ In the movies Disney uses Python to automate repetitive parts of the animation process. Rather than animators carrying out the same steps over and over, they use a Python program to repeat the steps automatically. This saves work, shortening the time it takes to make a film. Don’t worry, this won’t hurt—much! △ Medical marvels Python can be used to program robots to perform tricky operations. A Python-programmed robot surgeon can work more quickly than a human one, and be more accurate and less likely to make errors. We’ve been expecting you! Python? It’s a serious business! US_014-015_Meet_Python.indd 15 12/01/17 2:33 pm
16 S T A R T I N G W I T H P Y T H O N Installing Python All the projects in this book use Python 3, so make sure you download the correct version from the website. Follow the instructions that match your computer. Open IDLE When the installation is finished, check that it was successful by opening the IDLE program. Go to the “Start” menu, choose “All Apps”, then select “IDLE”. A window like the one below should open up. Download Python Click on the latest version of Python for Windows, beginning with the number 3. The installer file will download automatically. Of the different installer options, select “executable installer”. Go to the Python website Type the address below into your web browser to go to the Python website. Then click on “Downloads” to open the download page. Python on Windows Before you install Python 3 on a Windows PC, find out if it uses the 32-bit or 64-bit version of windows. Click “Start”, right-click “Computer”, and left-click “Properties”. Then choose “System” if the option appears. If you have a 32-bit version of Windows, use this installer. If you have a 64-bit version of Windows, use this installer. Click the installer. Run the installer Double-click the installer file to install Python. Choose “install for all users” and click “next” at each prompt, without changing the default settings. • Python 3.6.0a4 - 2016-08-15 • Windows x86 executable installer • Windows x86-64 executable installer • https://www.python.org/ L I N G O IDLE IDLE (short for Integrated Development Environment) is a free app that you get when you install Python. Designed for beginners, IDLE includes a basic text editor that allows you to write and edit Python code. 1 3 2 4 Python 3.6.0a4 (v3.6.0a4:017cf260936b, Aug 15 2016, 00:45:10) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> Python 3.6.0a4 Shell IDLE File Edit Shell Debug Window Help US_016-017_Installing_Python.indd 16 06/02/17 4:01 pm
17I N S T A L L I N G P Y T H O N Go to the Python website Type the address below into your web browser to go to the Python website. Then click on “Downloads” to open the download page. Open IDLE When the intallation is finished, check that it was successful by opening the IDLE program. Open the “Applications” folder, and then the “Python” folder. Double-click “IDLE” and a window like this should appear. Download Python From the downloads options, click on the latest version of Python 3 that matches your operating system. The Python.pkg file will download to your Mac automatically. Install Python You’ll find the .pkg file in the “Downloads” folder. Its icon looks like an opened parcel. Double-click it to start the installation. At the prompts, click “Continue” and then “Install” to accept the default settings. I M P O R T A N T ! Ask permission Never install Python or any other program unless you have permission to do so from the computer’s owner. You may also need to ask the owner to provide an administration password during installation. Python on a Mac Before you install Python 3 on a Mac, check which operating system the computer uses. Click the Apple icon in the top left of the screen and choose “About this Mac” from the drop-down menu. The version number might not be exactly the same as this one—just make sure you download the one that has a 3 at the beginning. Click the package to run the installer. • Python 3.6.0a4 - 2016-08-15 • Download macOS X 64-bit/32-bit installer https://www.python.org/ Python 3.6.0a4 (v3.6.0a4:017cf260936b, Aug 15 2016, 13:38:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. >>> Python 3.6.0a4 Shell IDLE File Edit Shell Debug Window Help 21 3 4 US_016-017_Installing_Python.indd 17 06/02/17 4:01 pm
18 S T A R T I N G W I T H P Y T H O N Using IDLE IDLE has two different windows in which you can work. The editor window can be used to write and save programs, while the shell window runs Python instructions immediately. The shell window When you open IDLE, the shell window pops up. This is the best place to get started in Python because you don’t have to create a new file first. Just type the code directly into the shell window. >>> print('I am 10 years old') >>> ''.join(reversed('Time to code')) >>> 123 + 456 * 7 / 8 You should come out of your shell more! E X P E R T T I P S Different windows To help you know which window you should type your code in, we’ve given each window in IDLE a different color. Shell window Editor window ▽ Give the shell a test run Type each of these code snippets into the shell window and press the enter/return key after each one. The first line displays a message and the second line does a calculation. Can you work out what the third line does? ▽ Working in the shell The code you type can be run straight away, and any messages or "bugs" (errors) are displayed. You can use the shell window like a notepad, to test out snippets of code before you add them into a bigger program. This line shows which version of Python you have. You type in code at the >>> prompt. Python 3.6.0a4 (v3.6.0a4:017cf260936b, Aug 15 2016, 13:38:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type 'copyright', 'credits' or 'license()' for more information. >>>from turtle import * >>>forward(200) >>>left(90) >>>forward(300) >>> Python 3.6.0a4 Shell IDLE File Edit Shell Debug Window Help The text here will depend on which operating system you have. These four lines of code are a simple drawing program; try it out for yourself. US_018-019_Using_Idle.indd 18 12/01/17 2:34 pm