Python Projects for Kids - Jessica Ingrassellino - E-Book

Python Projects for Kids E-Book

Jessica Ingrassellino

0,0
31,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Unleash Python and take your small readers on an adventurous ride through the world of programming

About This Book

  • Learn to start using Python for some simple programming tasks such as doing easy mathematical calculations.
  • Use logic and control loops to build a nice interesting game.
  • Get to grips with working with data and, once you're comfortable with that, you'll be introduced to Pygame, which will help you wrap up the book with a cool game.

Who This Book Is For

This book is for kids (aged 10 and over). This is book is intended for absolute beginners who lack any knowledge of computing or programming languages and want to get started in the world of programming.

What You Will Learn

  • Start fiddling with Python's variables, build functions and interact with users
  • Build your own calculator using the Math Library
  • Train Python to make logical decisions
  • Work with moving 2D objects on-screen
  • Understand the Pygame Library and build your very own game!
  • Write a cool program to manage inventories in your backpack

In Detail

Kids are always the most fast-paced and enthusiastic learners, and are naturally willing to build stuff that looks like magic at the end (when it works!). Programming can be one such magic. Being able to write a program that works helps them feel they've really achieved something. Kids today are very tech-savvy and cannot wait to enter the fast-paced digital world.

Because Python is one of the most popular languages and has a syntax that is quite simple to understand, even kids are eager to use it as a stepping stone to learning programming languages.

This book will cover projects that are simple and fun, and teach kids how to write Python code that works.

The book will teach the basics of Python programming, installation, and so on and then will move on to projects. A total of three projects, with each and every step explained carefully, without any assumption of previous experience.

Style and approach

The book will take a light approach in guiding the little readers through the world of Python. The main idea is to teach by example and let the readers have as much exercises to do, so that they learn faster and can apply their own ideas to the existing examples. The book should get them thinking, by the end, on where they can go next with such a powerful tool at their disposal.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 228

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Python Projects for Kids
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Welcome! Let's Get Started
Python projects for you
What can you do with Python?
Why you should learn Python
The prerequisites of Python
Setting up your computer
For Mac and Ubuntu Linux users
Python 2.7
Terminal – the command line and the Python shell
Text editor
For Windows users
Command prompt
Text editor
Write and run your first program in the command line
Make yourself a work folder
A quick task for you
Summary
2. Variables, Functions, and Users
Variables
Naming variables – conventions to follow
What can variables remember?
Strings
Integers
Floating point numbers (floats)
Combining strings, integers, and floats
Functions
Built-in functions
Parts of a function
Users interacting with your program
Using the text editor and the command line
Build your own function – name()
Set up your project file
Begin your project
Writing code
Running your program
Going the extra mile
A quick task for you
Summary
3. Calculate This!
The calculator
Basic functions
Operations on two numbers
Convert data into numbers – int() and float()
Floating point to whole number conversion
Whole number to floating point conversion
Text strings fail in int() and float()
Creating our first calculator file
New functions – subtraction, multiplication, and division
Subtraction
Multiplication
Division
Finding a remainder – modulo
Running your program
A quick task for you
Summary
4. Making Decisions – Python Control Flows
Is it equal, not equal, or something else?
Conditional statements – if, elif, else
Getting better input
if
elif
else
Loops
while
Global variables and the quit() function
Using the quit() function
Using the while loop to control the program
for
Bonus – count_to_ten() function
A quick task for you
Summary
5. Loops and Logic
Higher or Lower
Game file set up
Importing libraries
Setting and initializing global variables
What is a Boolean?
Building the easy version
Compare numbers
play_again()
Start, stop, play again
start_game()
play_again()
Play testing
Building the hard version
Comparing numbers – the hard version
Play test the whole program!
A quick task for you
Summary
6. Working with Data – Lists and Dictionaries
Lists
Parts of a list
Working with a list
Changing the list – adding and removing information
Adding items to the list
Removing items from the list
Lists and loops
Dictionaries
Key/value pairs in dictionaries
Changing the dictionary – adding and removing information
Adding items to the dictionary
Changing the value of an existing item
Removing items from the dictionary
List or dictionary
A quick task for you
Summary
7. What's in Your Backpack?
Setting up our coding environment
Planning to program your game
Skills needed to make a program
Score, play again, or quit?
Getting and storing player information
Making a players list
Player profiles
Player profiles – how do they work?
Add players to profile
Adding items to the virtual backpack
Limiting items in a virtual backpack
Testing your code so far
A game loop
Bringing back the while loop
Comparing guesses with backpack items
Keeping score
Ending the game
Testing your game
A quick task for you
Summary
8. pygame
What is pygame?
Installing pygame
Installing pygame – Windows
Installing pygame – Mac
Installing Xcode
Installing Homebrew
Installing programs with homebrew
Installing pygame – Ubuntu Linux
Installing pygame – Raspberry Pi
pygame
Initializing pygame
Setting up the game screen – size
Setting up the game screen – color
Making stationary objects
while loop – viewing the screen
Making more shapes
Rectangle
Ellipse
Experimenting with shapes
More advanced shapes
Making moving objects
Moving objects with the keyboard
A quick task for you
Summary
9. Tiny Tennis
Introduction to game programming principles
The game plan
Creating an outline of game parts
Section 1 – imports, globals, and drawings
Importing libraries
Introducing globals
Defining a color
Adjusting the screen size
Drawing the screen
Creating screen labels
Ball – the starting location
Ball – setting the speed and direction
Ball – setting the size
Paddles – starting location and size
Initializing the score
Testing section 1
Section 2 – moving the paddles
Pre-loop actions
Creating the while loop
Moving the paddles – keyboard events
Exiting the game – escape key
Paddle control – player 1
Paddle control – player 2
The increase and decrease value (-= and +=)
Testing section 2
Section 3 – moving the ball
Moving the ball – updating the location
Collision detection
Collision of the ball with the top and the bottom of the screen
Collision of the paddle with the top and the bottom of screen
Collision of the ball with the paddles
Testing – section 3
Section 4 – draw screen and track the score
The render screen – show what's happened
Displaying player scores
Ending the program
Play Tiny Tennis!
Summary
10. Keep Coding!
What we learned and your next steps
Classes and objects – very important next steps!
More fun with games
Adding music to games
Adding graphics to games
Remake or design games
Other games
PB-Ball
Snake
Other uses of Python
SciPy
iPython
MatPlotLib
Raspberry Pi
Coding challenges
Summary
A. Quick Task Answers
Chapter 1, Welcome! Let's Get Started
Chapter 2, Variables, Functions, and Users
Chapter 3, Calculate This!
Chapter 4, Making Decisions – Python Control Flows
Chapter 5, Loops and Logic
Chapter 6, Working with Data – Lists and Dictionaries
Chapter 7, What's in Your Backpack?
Chapter 8, pygame
Index

Python Projects for Kids

Python Projects for Kids

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: April 2016

Production reference: 1070416

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78217-506-3

www.packtpub.com

Credits

Author

Jessica Ingrassellino

Reviewer

David Whale

Commissioning Editor

Veena Pagare

Acquisition Editor

Aaron Lazar

Content Development Editor

Sachin Karnani

Technical Editor

Rupali R. Shrawane

Copy Editor

Sonia Cheema

Project Coordinator

Nikhil Nair

Proofreader

Safis Editing

Indexer

Rekha Nair

Production Coordinator

Melwyn Dsa

Cover Work

Melwyn Dsa

About the Author

Jessica Ingrassellino is a multi-talented educator, business leader, and technologist. She received her EdD from Teachers College, Columbia University for music education with an emphasis on assessment.

Jessica is currently employed as the lead software engineer in testing at Bitly, New York City. She transitioned from a teaching career of 10 years to a technology career through a balance of freelance work and social media exposure. Jessica's current work focuses on using Python to develop automated testing tools. She is an ASTQB certified quality assurance engineer with experience in testing web, mobile, and backend applications.

In addition to working at Bitly, Jessica remains committed to education and has founded http://www.teachcode.org/, a nonprofit that teaches computer programming skills to teachers and students in urban or underserved populations through Python and 2D game programming. This new initiative will give teachers the support they need through a standards-referenced curriculum, student-engaging activities, and access to experts in the field of technology.

I would like to thank my students for allowing me to have such fun teaching them Python and learning from their experiences as new programmers. I would also like to thank Cathy Kross and Alice McGowan for being willing to have me in their classes and school and interrupting their daily lives with my code-teaching experiments. Finally, I would like to thank my husband, Nick, for believing in me and helping me through some major writer's block. He never loses faith in me, and for that, I am eternally grateful.

About the Reviewer

David Whale is a software developer who lives in Essex, UK. He started coding as a schoolboy aged 11, inspired by his school's science technician to build his own computer from a kit. These early experiments lead to some of his code being used in a saleable educational word game when he was only 13.

David has been developing software professionally ever since, mainly writing embedded software that provides intelligence inside electronic products, including automated machinery, electric cars, mobile phones, energy meters, and wireless doorbells.

These days, David runs his own software consultancy called Thinking Binaries, and he spends about half of his time helping customers design software for new electronic products, many of which use Python. The rest of the time, he volunteers for The Institution of Engineering and Technology, running training courses for teachers, designing and running workshops and clubs for school children, running workshops and talks at meet-up events all round the UK, and generally being busy with his Raspberry Pi, BBC micro:bit, and Arduino.

David was the technical editor of Adventures in Raspberry Pi, John Wiley & Sons, the coauthor of Adventures in Minecraft, and he is a regular reviewer and editor of technical books for a number of book publishers.

I was really pleased to be asked to review this exciting new coding book for children. Python is an excellent language for children to learn from a young age, and Jessica has done a great job at helping readers take their first few steps in coding with Python. I hope you will be inspired by the code and ideas in this book and come up with your own ideas to enhance and develop all of the programs further—this is just the start of your exciting new creative journey into coding with Python!

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Preface

As you can guess from the title, this book is designed to teach the basic concepts of Python to kids. This book uses several mini projects so that kids can learn how to solve problems using Python.

Python has grown to become a very popular language for programming web apps, analyzing data, and teaching people how to write code. Python is known for being a simple language to use because it is read much like natural languages, yet it is able to do data analysis very quickly, making it a great language to create websites that handle a lot of data. Another nice thing about Python that makes it fun to use is that people have been working on game libraries, such as pygame, so that people can create graphics programs with Python. The use of simple graphics to make short games is a fun way to learn programming constructs and is especially good for visual learners.

What this book covers

Chapter 1, Welcome! Let's Get Started, discusses Python and setting up a Python development environment on Windows, Mac, and Linux operating systems.

Chapter 2, Variables, Functions, and Users, covers Python data types and functions, as well as how to program Python to get information from the user, store that information, and use it later.

Chapter 3, Calculate This!, uses Python to make a calculator that has multiple mathematical functions. We also learn about working in our file structure and the proper way to save code files.

Chapter 4, Making Decisions – Python Control Flows, covers the use of if, elif, and else, as well as the use of for and while loops, in order to help create programs that make decisions based on user actions.

Chapter 5, Loops and Logic, builds upon what we have learned in the previous chapters and allows us to build a number guessing game. We will build easy and difficult versions of the game.

Chapter 6, Working with Data – Lists and Dictionaries, explains how to use lists and dictionaries to store data. The differences between lists and dictionaries are explained, and we spend time building small lists and dictionaries as well.

Chapter 7, What's in Your Backpack?, allows us to use functions, loops, logic, lists and dictionaries to build a different kind of guessing game. We will also learn about nesting dictionaries and lists.

Chapter 8, pygame, talks about a popular graphical library that is used in Python to make small games. We will learn the fundamental aspects of this library and experiment with some code.

Chapter 9, Tiny Tennis, this game is a clone of a popular game. We will re-create the game using all of the skills that we have learned throughout the book. This is the major project of the book.

Chapter 10, Keep Coding!, shows you all the opportunities that will arise once you read this book.

Appendix, Quick Task Answers, has the answers to all the quick task questions within the chapters.

What you need for this book

This book can be used with Windows 10, Mac OS X, or Ubuntu Linux operating systems. Other versions of these operating systems may work; however, this book has been written specifically to address these systems. Additionally, you will need the Internet to download some tools, such as recommended text editors, for your operating system. All recommended downloads are open source.

Who this book is for

This book is for kids who are ready to move from graphically-based programming environments, such as Scratch, and into text-based environments. Kids who are ready to create their own projects will engage with this book, especially those who have played games. No prior programming experience is needed to complete the projects in this book; this book is for kids aged 10 years and above, who are ready to learn about Python programming.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for WindowsZipeg / iZip / UnRarX for Mac7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/PythonProjectsforKids_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Welcome! Let's Get Started

If you've picked up this book, then you are taking your first step toward building amazing projects using code. Some of you might want to make games, while others might want to learn more about how all of your favorite websites and apps actually work. If you follow the exercises in this book, you'll be able to do the following:

Create fun games to play with your family and friendsLearn about the inner workings of your appsLearn how to take charge of your computer

Python projects for you

In this book, you will learn Python code. Specifically, you will learn how to design a computer program from the very beginning. It doesn't matter if you have never coded before because each exercise in this book is designed to get you ready to code. If you have coded before, you will find that this book has some really helpful exercises that can help make your code even better. Additionally, there are some more advanced projects toward the end of the book, which you should definitely take a look at!

What can you do with Python?

If you take a look at the Web and search for Python jobs, you will find that many of the highest paying jobs are in Python. Why?

Python is a very flexible and powerful language in the following ways:

It can be used in order to go through millions of lines of dataPython can search for information on a website without having to go to the website itselfIt is even used to host and design websites

So, what will it take to learn Python? If you have never programmed, you will probably want to follow each lesson in order so that you can build the skills you need to make a game or another kind of computer program. The final project in this book will be a game. If you have some other programming experience, such as making modifications to your computer games, using programs such as Scratch or Logo or trying some of the free programming classes on the Internet, then you might decide to skim this book first to see what you already know. It is still recommended that you follow the contents of this book in the order they are presented, as each project builds on the projects that were explained in the previous chapter.

Why you should learn Python

Python teaches all of the basics of an object-oriented programming language, and it is still very powerful. In fact, many Internet companies, most notably Mozilla Firefox and Google, use Python in part or all of their products! Python has also been used to build Django, a free framework to make websites.

It has also been used to build many small video games by people learning about it as well as more advanced programmers. Finally, Python can be used to quickly read and analyze millions of lines of data very quickly! By learning Python, you will be prepared to build a variety of interesting projects, and you will gain the skills necessary to learn other programming languages if you choose to do so.

The prerequisites of Python

Before you get started, you need the following basic materials:

A computer that can run Windows 7 or higher, Mac OS X 10.6 or higher, or Ubuntu 12.4 or higher. You may also use a Raspberry Pi as it comes preinstalled with Python, pygame, and the other software needed to complete the projects in this book.An Internet connection is necessary as some of the software you need to install on your computer might not be installed already. For example, Windows operating systems do not come with Python preinstalled, so an Internet connection will be needed; pygame is also not preinstalled on Windows, Mac, or Linux systems.Along with an Internet connection, you will also need a web browser, such as Firefox, Safari, Chrome, or Internet Explorer, which will allow you to visit the Python documentation pages.

Note

All of the code samples in this book are available for download on the Packt Publishing website.

Setting up your computer

There are many different computer operating systems, but the most common operating systems are Macintosh (Mac), Windows, and Linux. You should follow the installation steps that go with your operating system. There are some subtle but important differences between the systems.

For the projects in this book, we will be using Python 2.7. While there are higher versions than this (3.x), these versions do not work dependably with pygame on Windows, Mac, or Ubuntu Linux as yet. However, this book will be written to use conventions that work in both versions of Python so that projects are easily completed on Raspberry Pi (which uses Python 3.x that's been specially configured with pygame) with just a few modifications. These modifications will be duly noted.

For Mac and Ubuntu Linux users

Mac and Linux systems share enough similarities that people who use either Mac or Linux can follow the same set of instructions. These instructions will make note of any differences between Mac and Ubuntu Linux.

Python 2.7

At the time of writing, Mac OS X El Capitan comes with Python 2.7 preinstalled, so nothing extra needs to be done at this point.

Ubuntu Linux 15.10 has Python 2.7.10 installed by default, so users of this latest (as of writing this) version of Linux also need to do nothing extra at this point.

Terminal – the command line and the Python shell

Mac and Ubuntu Linux users have Python by default, but finding Python is tricky if you don't know where to look. There is a program called Terminal on both Mac and Linux operating systems. This program allows you to exercise a lot of control over your computer in these ways:

On a Mac, go to Finder | Applications | Utilities and click on Terminal. The terminal application will open up, and you should see a small, white window on your screen.Ubuntu users can search for terminal on their desktops, and the program will show up in their Start menu. When you click on the terminal, you will see a small, black window on your screen.The terminal also functions as a Python shell when a command is given to run Python. We will learn about this later.

Text editor

A text editor is a helpful tool for writing and editing Python programs. The terminal is a nice place to test snippets of Python code, but when we want to edit and save the code in order to use it over again, we will need a text editor. Although both Mac and Linux systems come with a text editor, there are some very nice, free editors that have good features. jEdit is one of these editors.

Note

For Mac and Linux, go to http://www.jedit.org/ and download jEdit. Follow the installation instructions.

To successfully complete all of the exercises in this book, you will often need to keep both the terminal and text editor open at the same time on your screen.

This is what the text editor application, jEdit, looks like in Mac and Linux:

For Windows users

Windows users, this setup might require help from your parents. Since Python is not installed by default on Windows, some system adjustments