Hello Raspberry Pi! Python programming for kids and other beginners Ryan Heitz MANNING SHELTER ISLAND Licensed to Stephanie Bernal <[email protected]> For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2016 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without elemental chlorine. Manning Publications Co. Development editor: Dan Maharry 20 Baldwin Road Copyeditor: Tiffany Taylor PO Box 761 Proofreader: Alyson Brener Shelter Island, NY 11964 Technical proofreader: Romin Irani Typesetter: Marija Tudor Cover designer: Leslie Haimes ISBN: 9781617292453 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 Licensed to Stephanie Bernal <[email protected]> To Juliana, Daniel, and John Licensed to Stephanie Bernal <[email protected]> Licensed to Stephanie Bernal <[email protected]> Brief contents PART 1 GETTING STARTED 1 1 Meet Raspberry Pi 3 2 Exploring Python 33 PART 2 PLAYING WITH PYTHON 65 3 Silly Sentence Generator 3000: creating interactive programs 67 4 Norwegian Blue parrot game: adding logic to programs 91 5 Raspi’s Cave Adventure 121 PART 3 PI AND PYTHON PROJECTS 149 6 Blinky Pi 151 7 Light Up Guessing Game 176 8 DJ Raspi 204 v Licensed to Stephanie Bernal <[email protected]> Licensed to Stephanie Bernal <[email protected]> Contents Preface xiii Acknowledgments xv About this book xvii PART 1 GETTING STARTED 1 1 Meet Raspberry Pi 3 What is the Raspberry Pi? 4 Exploring your Raspberry Pi’s parts: hardware 4 Giving your Pi a cozy home: Pi cases 6 ❍ The brain of your Pi: system on a chip 7 ❍ Connecting a keyboard and mouse: USB ports 8 ❍ Storing memories: your Pi gets a memory card 10 Connecting a TV or monitor: HDMI port 13 ❍ Other ports and connections 17 ❍ Powering your Pi: microUSB power port 17 It’s alive! Plugging in the Pi 18 Getting your Pi running: software 19 Installing the Raspbian operating system 19 ❍ Configuring the operating system: making it yours 21 ❍ Saving your configura- tion and rebooting 24 Getting around: learning Raspbian 26 Finding and opening applications on your Raspberry Pi 26 Your files and folders 26 ❍ Writing code 28 Fruit Picker Extra: shopping at the Pi Store 29 vii Licensed to Stephanie Bernal <[email protected]> viii Contents Challenge 30 Scavenger hunt 31 Summary 31 2 Exploring Python 33 Playing with Python 33 Discovering Python’s mathematical operators 35 Adding and subtracting 35 ❍ Multiplying and dividing 37 Figuring out whole numbers and remainders 38 Exponents 38 ❍ Square roots 39 ❍ Challenge: stacking Pis! 39 Storing information using variables 41 Creating variables and assigning values 42 ❍ Displaying variable values 42 ❍ Storing strings in variables 45 Changing the value of variables 46 Displaying text on a screen 50 Using the print function 50 ❍ Troubleshooting 51 Creating programs 52 Writing Python programs with IDLE 53 ❍ Starting a new program 54 ❍ Saving programs 56 ❍ Python interpreting the program 57 Fruit Picker Extra: creating documents 57 Writing silly things and saving them 57 Challenges 60 The matrix 61 ❍ Building a brick wall 61 Pi electrons 62 Summary 62 PART 2 PLAYING WITH PYTHON 65 3 Silly Sentence Generator 3000: creating interactive programs 67 Creating a welcome message 68 Starting a new program 69 ❍ Saving the program 71 Licensed to Stephanie Bernal <[email protected]> Contents ix Adding notes in your code 73 Using hashtags for comments 73 Getting and storing information 75 Joining strings 77 Using more than one input 79 ❍ Building the sentence 80 Troubleshooting 81 Completing the program: displaying the silly sentence 83 Fruit Picker Extra: Minecraft Pi 85 What's Minecraft? 85 ❍ Launching Minecraft Pi 86 Python programming interface to Minecraft Pi 88 Challenges 88 Knight’s Tale Creator 3000 88 ❍ Subliminal messages 89 Summary 90 4 Norwegian Blue parrot game: adding logic to programs 91 Displaying the game introduction 92 Creating the game welcome message and instructions 94 Collecting input from the player 101 Using if statements to respond to users in different ways 105 Practicing if statements 108 Using while loops to repeat things 110 A closer look at while loops 112 ❍ Breaking out of a while loop 113 ❍ Practicing while loops 114 Using Python code libraries to generate random numbers 115 Fruit Picker Extra: Scratch 118 Challenges 119 Summary 120 5 Raspi’s Cave Adventure 121 Project introduction: Raspi’s Cave Adventure 122 Left cave 124 ❍ Right cave 124 Licensed to Stephanie Bernal <[email protected]>
Description: