Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Python Projects for Kids
Python Projects for Kids

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

eBook
$20.98 $29.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Python Projects for Kids

Chapter 2. Variables, Functions, and Users

In the previous chapter, you learned how to install Python on your computer. You also learned how to use the print statement in Python and printed some messages using your Python shell. We are now going to jump into a lot of details so that we can build our first project together. It will include the following:

  • Variables
  • Variable names
  • Strings, integers, and floats
  • Functions

Variables

A variable is when one letter or word is used to represent a different letter, word, number, or value. One way to think of a variable is to imagine that you are programming a computer so that it can make memories. For example, my name is Jessica. If I am writing a computer program and I want that program to remember my name, I will assign my name to a variable. This will look like name = 'Jessica'. The variable is name. Jessica in the memory.

Perhaps I would like the computer to remember something else about me as well. Maybe I want the computer program to remember that I am 64 inches, or roughly 128 cm, tall. I will say height_inches = 64 or height_centimeters = 128. The variables here are height_inches and height_centimeters. The memories are my height in inches and my height in centimeters.

Why don't you try giving a computer the name variable with your name and then a height variable with your height?

First, open your Python shell and type the following code:

name...

Functions

Once we have variables, we can use them to do some pretty interesting things. The most interesting thing is to build functions. Python functions are blocks of code that we can build to do a specific job. We build these functions once, and then we can reuse them in our code just by typing the name. This is really helpful. For example, if I need to write a program that adds two numbers (a calculator, for example), I do not want to have to write three or four lines of code every time I want to add two numbers. Instead, I want to write one function that can add two numbers together, and then use that single line whenever I need to add numbers.

Before we begin building functions of our own, we need to also know that Python has a lot of amazing functions that are built in. Some of Python's functions are things we will use all the time. Others we won't talk about in this book, but as you become a more skilled programmer, you will learn more about Python's built-in functions...

Users interacting with your program

We just built a function that adds two numbers together. Learning to make a program that does math is interesting, but our function is limited because our addition() function needs to have variables changed manually to calculate results for different numbers.

What if there was a way to get information from the user and store THAT information in a variable so that it could be used by addition or subtraction functions each time? Anyone who has used a calculator of any kind already knows that this is possible. Python has a function called raw_input() that allows us to tell the program to ask the user a question. The raw_input() function is incredibly useful. We can get every kind of information from the user this way, and we can make interactions between the user and the computer based on the user input.

We can use the Python shell to test how the raw_input() function works. Try typing these two lines of code into your Python shell:

  name = raw_input(&apos...

Using the text editor and the command line

So far, we have used the Python shell to write and test code. The shell is great because we type a line of code or even a few lines of code, and then we run them immediately to see whether they work. However, you may have noticed that there isn't a way to save any of the code that we write.

For a program to run, it needs to have all of the code available. Using a text editor is just like writing a report, an email, or a paper: we write our code and save it; then, we go back to edit it if we want to. In order to make Python use and understand our file, we need to use the command line and tell Python to run the file.

To perform our next task as well as the remaining tasks in the book, we will use our text editor side by side with our terminal/command prompt. Let's walk through the setup of the text editor and command line right now.

The first thing you need to do is as follows:

  • Make a special folder where you can store your code files, and...

Build your own function – name()

So, you have learned about variables and how they store information. You have also learned about how these variables can be used inside of a function. Finally, you have learned how to use special Python functions, such as input(), to help get information from users and store it in the computer. You are ready to build your own function using variables and input().

Set up your project file

The function that we will build now is called name(). The purpose of this function will be to ask the user their name, store (remember) the name, and then print out a friendly message to the user.

To start this function, do the following:

  1. Open a new file in your text editor.
  2. Go to Save and name the file name.py.

    Tip

    You need to use .py at the end of all of your code files so that the files run in the terminal/command prompt. Python only recognizes .py files.

  3. Save the file in the folder you made for all of your Python work.

Begin your project

Once you have set up a project file...

Variables


A variable is when one letter or word is used to represent a different letter, word, number, or value. One way to think of a variable is to imagine that you are programming a computer so that it can make memories. For example, my name is Jessica. If I am writing a computer program and I want that program to remember my name, I will assign my name to a variable. This will look like name = 'Jessica'. The variable is name. Jessica in the memory.

Perhaps I would like the computer to remember something else about me as well. Maybe I want the computer program to remember that I am 64 inches, or roughly 128 cm, tall. I will say height_inches = 64 or height_centimeters = 128. The variables here are height_inches and height_centimeters. The memories are my height in inches and my height in centimeters.

Why don't you try giving a computer the name variable with your name and then a height variable with your height?

First, open your Python shell and type the following code:

name = 'yourname'...

Functions


Once we have variables, we can use them to do some pretty interesting things. The most interesting thing is to build functions. Python functions are blocks of code that we can build to do a specific job. We build these functions once, and then we can reuse them in our code just by typing the name. This is really helpful. For example, if I need to write a program that adds two numbers (a calculator, for example), I do not want to have to write three or four lines of code every time I want to add two numbers. Instead, I want to write one function that can add two numbers together, and then use that single line whenever I need to add numbers.

Before we begin building functions of our own, we need to also know that Python has a lot of amazing functions that are built in. Some of Python's functions are things we will use all the time. Others we won't talk about in this book, but as you become a more skilled programmer, you will learn more about Python's built-in functions.

Built-in functions...

Users interacting with your program


We just built a function that adds two numbers together. Learning to make a program that does math is interesting, but our function is limited because our addition() function needs to have variables changed manually to calculate results for different numbers.

What if there was a way to get information from the user and store THAT information in a variable so that it could be used by addition or subtraction functions each time? Anyone who has used a calculator of any kind already knows that this is possible. Python has a function called raw_input() that allows us to tell the program to ask the user a question. The raw_input() function is incredibly useful. We can get every kind of information from the user this way, and we can make interactions between the user and the computer based on the user input.

We can use the Python shell to test how the raw_input() function works. Try typing these two lines of code into your Python shell:

  name = raw_input('What...
Left arrow icon Right arrow icon

Key benefits

  • 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.

Description

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.

Who is this book 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 14, 2016
Length: 192 pages
Edition : 1st
Language : English
ISBN-13 : 9781785285851
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Apr 14, 2016
Length: 192 pages
Edition : 1st
Language : English
ISBN-13 : 9781785285851
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 93.97
Raspberry Pi Projects for Kids (Second Edition)
$26.99
Python Projects for Kids
$38.99
JavaScript Projects for Kids
$27.99
Total $ 93.97 Stars icon

Table of Contents

12 Chapters
1. Welcome! Let's Get Started Chevron down icon Chevron up icon
2. Variables, Functions, and Users Chevron down icon Chevron up icon
3. Calculate This! Chevron down icon Chevron up icon
4. Making Decisions – Python Control Flows Chevron down icon Chevron up icon
5. Loops and Logic Chevron down icon Chevron up icon
6. Working with Data – Lists and Dictionaries Chevron down icon Chevron up icon
7. What's in Your Backpack? Chevron down icon Chevron up icon
8. pygame Chevron down icon Chevron up icon
9. Tiny Tennis Chevron down icon Chevron up icon
10. Keep Coding! Chevron down icon Chevron up icon
A. Quick Task Answers Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.2
(5 Ratings)
5 star 20%
4 star 20%
3 star 20%
2 star 40%
1 star 0%
Angie Jones Nov 30, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great starter book for learning Python! I love the fun exercises and also the summary questions throughout. Nicely written and easy to follow.
Amazon Verified review Amazon
Amazon Customer Jul 17, 2016
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book is for kids 10 and up who have no programming experience, and I feel this would not be a good book to give the average kid if they have never programmed before, because this probably will not make them want to learn or be interested (because it is very verbose and the tone resembles a textbook 99% of the time). I think it would may be perfect for someone who is already interested, and wants to learn another programming language, because they can skim through it rather quickly and pick up the key parts to the language, since many languages share a lot of similar things (like if statements and while loops, etc.)Also take a look at raspberry pi books/tutorials.The activities included are a build a terminal based calculator, make a terminal based number guessing game, create a two player terminal game by guessing what's in each other's backpack (this is where I stopped), a pygame activity (graphical) that looks a lot like the game pong.I am a college student who wanted to learn a new language an I had access to this book through my library, so I said why not? I have been programming in Java, and thought I would go through this book to learn the basic syntax of the Python language. It was perfect for what I needed it for.
Amazon Verified review Amazon
Mike Driscoll Jun 24, 2016
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
To be completely transparent, Packt Publishing sent me a review copy of this book.First off, I didn't actually read every single word in this book. I call this the skimming review method. Personally I prefer to read a book at my own pace and review it accordingly, however I have been asked repeatedly by Packt to finish this review so this is what you get. My first impression was that this book would teach youngsters how to program in Python by creating mini-games. However we don't really get into games until chapter 5. We don't learn anything about pygame until chapter 8. Let's go over each chapter and see what they're about before I really dig in thoughChapter one is your basic intro to what Python is and how to install it. The author chose to use Python 2.7 for this book over Python 3. The rest of the chapter is about creating a "Hello World" application and a work folder.Chapter two is about variables and functions. This chapter is pretty brief, but I thought it covered the topics well enough. The biggest thing it does is explaining to the reader how to create a function and save it to a file.For chapter three, we actually get to create a calculator of sorts. It's text-based only and doesn't really do anything to handle bad inputs from the user. In fact, one big knock against this book is that it doesn't talk about exception handling at all. I also have a couple of problems with this chapter. I believe that page 34 is showing the wrong screenshot as the accompanying text is talking about casting from one type to another while the screenshot doesn't show any kind of casting whatsoever. The other issue is that on page 41, the text states that you can run the script as written in the book. However I don't see anything in the code that actually calls any of the functions, so if you run this code, you will get nothing outputted to the terminal.Chapter four is all about conditional statements and loops. The purpose of this chapter is to enhance the calculator application you wrote in the previous chapter such that it keeps running until the user asks it to quit.In chapter five, we learn how to create easy and hard levels for our game. The game is the "Higher or Lower" game. You will learn about what a Boolean is, how to import a library, and global variables.Chapter six dives into some of Python's more interesting data types, the list and dictionary. The premise of this chapter is to teach the reader how to store data. While I agree that lists and dictionaries are a good format, I wonder if learning about pickle, json or yaml might have been good to learn about here too. Admittedly, I don't think this book talks about File I/O, so those topics are probably considered to be out of scope.For chapter seven, the reader learns how to create a two player game that the author dubs "What's in Your Backpack?" This chapter helps the reader layout a game that can keep score, restart the game or stop the game. You will also learn how to create a player profile, which is formatted as a dict. This seems like a good place to use a class to me, especially if we're going to be using pygame in the next chapter, but I realize the target audience is supposed to be kids. Anyway, you will also get to add items to a virtual backpack, which is kind of fun to learn the author's implementation.We finally reach pygame in chapter eight where you learn how to install pygame. You will also learn how to set up the screen size and color as well as create stationary and moving objects.Chapter nine builds on chapter eight by teaching the reader how to create a tennis game (i.e. pong). It introduces the reader to the concepts of game programming and how to outline your project before coding it. This chapter is actually split into four sections after this point. The first section basically creates all the pieces of the game that you will need. Section two will teach you how to move the paddles and section three will teach you how to move the ball. Section four is about how to run the game and keep score.The final chapter encourages the readers to keep coding! The text tells its readers where to go from here. For example, it talks about how they will need to learn about classes and objects to promote code reuse. It also mentions that you can add music and graphics with pygame. Then it talks about redesigning your game or trying to create your own versions of classic games. Finally it talks about other uses and libraries for Python, such as SciPy, iPython, and MatPlotLib.When I first heard about this book, I immediately thought of Jason Briggs' book, <a href="http://amzn.to/293Q6Ye" target="_blank">Python for Kids</a> and the Sande's book, <a href="http://amzn.to/291PDHo" target="_blank">Hello World!: Computer Programming for Kids and Other Beginners</a>. Both of these books are longer and contain a lot more information than "Python Projects for Kids" does. I personally think that of the three, I would choose the Sande book as the easiest for kids to get into. Briggs covers a lot more interesting topics, but he may go just a tad too fast depending on the child. As for "Python Projects for Kids", I feel like there are too many items that aren't covered (classes, exceptions, many Python data constructs, etc). It also feels like pygame itself isn't really covered. There seemed to be a big build up to get to pygame and then there just wasn't much content when we finally got there.If I were to lay out a strategy for learning Python for children, I would start with Sande and then if the child wanted to learn about games, I would move on to Sweigart's books on creating games with Python (<a href="http://amzn.to/28RS7pa" target="_blank">Invent Your Own Computer Games with Python</a> and <a href="http://amzn.to/293SL46" target="_blank">Making Games with Python & Pygame</a>. Then I might move onto something else, like some of the Python for Minecraft books.As for this book, I just don't know where it would fit. I believe it was written well but needed some additional polish to push to the top of the heap.
Amazon Verified review Amazon
Amazon Customer Oct 24, 2016
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I am currently using this book to teach middle and high school students to program in Python. I love the organization and flow of the book, but will not use it for my class next school year, as it is riddled with errors. Also, the programming assignment in Chapter 7, What's in Your Backpack?, is totally inappropriate for beginning Python students.
Amazon Verified review Amazon
Simon May 27, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
There are plenty of better books, With this one you will need to download: Xcode (which needs additional files); Quartz, and homebrew, if you wish to use pygame. I did download them and the terminal no longer works properly leaving me in a further stew. Personally I have never found a Packt book any good, and hope you will fair better as I am sure some people probably do. I for one will have to count the cost of buy from them more than once! I asked Packt for some help and after a week and a half I finally told them where to go. God help the children learning Python!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.