Coding Club Level 2 Python Next Steps Roffey C. instant download
Coding Club Level 2 Python Next Steps Roffey C. instant download
download
https://ebookname.com/product/coding-club-level-2-python-next-
steps-roffey-c/
https://ebookname.com/product/next-generation-video-coding-and-
streaming-1st-edition-bing/
https://ebookname.com/product/leading-your-business-to-the-next-
level-rodney-page/
https://ebookname.com/product/embedded-c-coding-standard-michael-
barr/
https://ebookname.com/product/rockets-and-people-hot-days-of-the-
cold-war-first-english-language-edition-edition-boris-chertok/
Smooth Homogeneous Structures in Operator Theory 1st
Edition Daniel Beltita
https://ebookname.com/product/smooth-homogeneous-structures-in-
operator-theory-1st-edition-daniel-beltita/
https://ebookname.com/product/geomancist-second-edition-a-fated-
mates-paranormal-series-7-forbidden-arts-a-paranormal-romance-
series-book-5-charmaine-pauls/
https://ebookname.com/product/pll-performance-simulation-and-
design-3rd-edition-dean-banerjee/
https://ebookname.com/product/the-secrets-of-getting-better-
grades-study-smarter-not-harder-2nd-edition-brian-marshall/
https://ebookname.com/product/focus-on-grammar-2-workbook-a2-4th-
edition-samuela-eckstut-didier/
Java Web Services Up and Running Second Edition Martin
Kalin
https://ebookname.com/product/java-web-services-up-and-running-
second-edition-martin-kalin/
Cod
i
Clu ng
Python b
Next
Steps level 2
Chris Roffey
Cod
i
Clu ng
Python b
Next
Steps level 2
Chris Roffey
University Printing House, Cambridge CB2 8BS, United Kingdom
www.cambridge.org
Information on this title: www.cambridge.org/9781107623255
© Cambridge University Press 2013
This publication is in copyright. Subject to statutory exception
and to the provisions of relevant collective licensing agreements,
no reproduction of any part may take place without the written
permission of Cambridge University Press.
First published 2013
Reprinted 2013
Printed in Poland by Opolgraf
A catalogue record for this publication is available from the British Library
ISBN 978-1-107-62325-5 Paperback
Cambridge University Press has no responsibility for the persistence or accuracy
of URLs for external or third-party internet websites referred to in this publication,
and does not guarantee that any content on such websites is, or will remain,
accurate or appropriate.
Contents
Introduction 4
Acknowledgements 106
Contents 3
Introduction
Who is this book for?
This book is the Level 2 core book in the Coding Club series of books. To get the most out
of this title, you should be familiar with the Python 3 programming language and know
about variables, while loops and if, elif and else statements. Therefore, we advise that you
first read Python Basics before reading this book. Python: Next steps is aimed at 12–13 year
olds but is accessible to older children and even adults who want to learn about computer
programming.
Introduction 4
What you need?
Any computer can run Python 3. If your computer does not already have Python 3 installed
there is a section on the companion website (www.codingclub.co.uk) that guides you through
the installation. This takes about five minutes! That is all you need to get started.
Start files for all the projects in the book are available to download from the companion
website so you do not get lost in the bigger projects. There are also finished files for each
project, should you get stuck, and answers to the puzzles and challenges.
There are four ways in which this book tries to help you to learn:
1 Typing in the code – this is important as it gets you to work through the code a line at a
time (like computers do) and will help you remember the details in the future.
2 Finding and fixing errors – error messages in Python give you some clues as to what has
gone wrong. Solving these problems yourself will help you to be a better programmer.
However, if you get stuck, the code can be downloaded from the companion website
(www.codingclub.co.uk).
Introduction 5
3 Experimenting – feel free to experiment with the code you write. See what else you can
make it do. If you try all the challenges, puzzles and ideas, and generally play with the
code, this will help you learn how to write code like a professional.
4 Finally, this book will not only provide the code to build some pretty cool, short projects –
it will also teach you how the programs were designed. You can then use the same
methods to design your own applications.
A word of warning
You may be tempted to simply get the code off the website instead of typing it out yourself.
If you do this you will probably find that you cannot remember how to write code so easily
later. In this book you will only be asked to type small chunks of code at a time – remember
that this will help you understand every detail of each of your programs.
Introduction 6
Chapter 1
Data types
In this chapter you will:
• make a version of MyMagic8Ball that is much shorter than the one from Python Basics.
Data types
In Python Basics you learned about strings (bits of text), integers (whole numbers) and
I’m back!
floats (numbers with a decimal point). These are examples of data types. There are more!
In this chapter we will look at some new data types: tuples, lists and dictionaries. These new
data types are all called container data types because they store more than one piece of
data. For example, they can store several strings. They do so in different ways and have their
own advantages and disadvantages.
A string is rather like a container because it stores a whole sequence of letters or numbers (or
a mixture of both). In Python Basics we learned that there are several functions we can use on
strings. We can also use many of these functions on tuples, lists and dictionaries.
Chapter 1: Data types 7
Tuples
A tuple is the simplest of our new data types. They can store strings, integers and other data
types. Here is an example of a tuple that stores four strings, each separated by a comma:
Each value in a tuple is separated by a comma. Unlike variables, we cannot change what is
stored in a given tuple.
Each value in the tuple has an index starting from 0. So, print(my_tuple[1])for the
example above produces the output two. Look at how this works below.
A tuple.
Chapter 1: Data types 8
MyMagic8Ball
In Python Basics we wrote a small application called MyMagic8Ball that used the random
module and the functions print(), input() and randint(). Here is the code:
import random
# write answers
ans1="Go for it!"
ans2="No way, Jose!"
ans3="I'm not sure. Ask me again."
ans4="Fear of the unknown is what imprisons us."
ans5="It would be madness to do that!"
ans6="Only you can save mankind!"
ans7="Makes no difference to me, do or don't - whatever."
ans8="Yes, I think on balance that is the right choice."
print("Welcome to MyMagic8Ball.")
import random
answers = (
"Go for it!",
"No way, Jose!",
"I'm not sure. Ask me again.",
"Fear of the unknown is what imprisons us.",
"It would be madness to do that!",
"Only you can save mankind!",
"Makes no difference to me, do or don't - whatever.",
"Yes, I think on balance that is the right choice."
)
print("Welcome to MyMagic8Ball.")
print("shaking ...\n" * 4)
# exit nicely
input("\n\nPress the RETURN key to finish.")
We are going to use a function from Python’s random module so we need to import it.
The tuple
We have to separate the strings in the tuple answers with commas. Starting a new line after
each comma makes the code much easier to read.
The input() function listens to the keyboard entry and waits for the return key to be
pressed. It then returns the keyboard input as a string, which we store in the variable
question.
This line of code asks the randint() method in the random module to select a random
number from 0 to 7. This number is then stored in the variable called choice. (A method is
a function in a class.)
Finishing off
print(answers[choice])
This uses the random number choice as the index in the answers tuple. This line selects
the string that was randomly chosen from the tuple and prints it.
Experiment
The two scripts are available from the companion website
(www.codingclub.co.uk). Try them both out and check that they
do the same thing.
Just as with tuples, each value in the list has an index starting from 0 and each value is
separated by a comma.
You can see that both a list and a tuple provide the same output. So, when would we use a
list instead of a tuple? We would choose a list rather than a tuple if we want our program to
add, remove or change an item within the list.
or
key value
You might have noticed that dictionaries require a different structure within the brackets to
assign keys to the values. They use a colon ‘:’ to separate the value from its key.
Useful functions
Table 1.1 provides a list of useful functions you can use on strings, tuples, lists and
dictionaries. You can also find it in Appendix 1. The table assumes the following containers
have been created:
Chapter summary
In this chapter you have:
We will explore these new data types further in this book. Here are just a few ideas
that will help you refresh your coding skills from Python Basics. (As dictionaries are the
hardest to use, we will wait until you have learned a little bit more before providing any
puzzles involving them.)
Challenge
This is a challenge from Python Basics so although you may be a bit rusty you should be
able to manage it. Hopefully it brings back happy memories for you.
1 Add some code to myMagic8Ball2.py (Code Box 1.2) so that the Magic8Ball says “Hi”
and asks for the user’s name at the start of the game.
2 It should then store the input in a variable such as user_name.
3 Change the code so that the Magic8Ball talks to the user using their name. At the end
for example, it could say: “Thanks for playing, [Name]. Please press the RETURN key to
finish.”
You are destined to
There are several ways to do this. become a famous computer
To see one answer go to www.codingclub.co.uk/book2_resources.php. scientist one day!
Idea
Change the Magic8Ball game into a fortune cookie game. You could call it
myFortuneCookie.py.
In the source code downloaded from the companion website, in the folder for Chapter 2,
you will find a file called myGlossary_Start.py. This file provides you with outline source
code for a glossary application containing the complete glossary for this book. This is also
provided for reference in Code Box 2.1.
##### main:
window = Tk()
window.title("My Coding Club Glossary")
# create label
The glossary has been stored as a dictionary data type. The key is the glossary word and the
value is the definition.
key value
We have used single speech marks rather than the usual double ones so that we can use
double speech marks in the definitions without having to escape them all.
Incense Cedar
INCENSE CEDAR
(Libocedrus Decurrens)
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com