COMPUTING - arghbox
[Pages:225]P
dmy
G
Usin
.
YTHON PROGRAMMIN Key Stage 3
COMPUTING
STUDENT REFERENCE AND EXERCISES
PART II
g Minecraft Pi and Codeca
Minecraft Pi Book
Craig Richardson June 13, 2013
This book is licensed under the Creative Commons license of Attribution-NonCommercialShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
You are free:
to Share -- to copy, distribute and transmit the work to Remix -- to adapt the work
Under the following conditions:
Attribution -- You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
Noncommercial -- You may not use this work for commercial purposes. Share Alike -- If you alter, transform, or build upon this work, you may distribute the
resulting work only under the same or similar license to this one.
With the understanding that:
Waiver -- Any of the above conditions can be waived if you get permission from the copyright holder.
Public Domain -- Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license.
Other Rights -- In no way are any of the following rights affected by the license: ? Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations; ? The author's moral rights; ? Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
Notice -- For any reuse or distribution, you must make clear to others the license terms of this work.
More information can be found on the Creative Commons site:
Disclaimer: Any reference or resemblance to the intellectual property of an individual or organisation is used for educational purposes only.
This book is not affiliated or endorsed by the Raspberry Pi Foundation, Mojang/Minecraft, Codecademy or any organisation mentioned in this book.
Contents
1 Introduction
1
1.1 Using this Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Standards Used in this Book . . . . . . . . . . . . . . . . . . . . 2
2 Getting Started
5
2.1 Programming with Python . . . . . . . . . . . . . . . . . . . . . 6
2.1.1 The Minecraft Pi API . . . . . . . . . . . . . . . . . . . . 6
2.1.2 Writing and Running Code . . . . . . . . . . . . . . . . 7
3 Python Syntax
11
3.1 Minecraft Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.1.1 Teleport the Player . . . . . . . . . . . . . . . . . . . . . 12
3.1.2 Teleport the Player Precisely . . . . . . . . . . . . . . . 15
3.1.3 Teleportation Tour . . . . . . . . . . . . . . . . . . . . . 16
3.1.4 Stop Smashing Things . . . . . . . . . . . . . . . . . . . 18
3.2 Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . 19
3.2.1 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2.2 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2.3 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.2.4 Changing Values of Variables . . . . . . . . . . . . . . . 23
3.3 Whitespace and Statements . . . . . . . . . . . . . . . . . . . . 24
3.3.1 Statements and Line Breaks . . . . . . . . . . . . . . . . 24
3.3.2 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.4.1 Single Line Comments . . . . . . . . . . . . . . . . . . . 26
3.4.2 Multi-line Comments . . . . . . . . . . . . . . . . . . . . 27
4 Maths Operations
29
4.1 Minecraft Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.1.1 Stacking Blocks . . . . . . . . . . . . . . . . . . . . . . . 30
4.1.2 Super Jump . . . . . . . . . . . . . . . . . . . . . . . . . . 32
i
ii
CONTENTS
4.1.3 Set Block Below Player . . . . . . . . . . . . . . . . . . . 33 4.1.4 Speed Building . . . . . . . . . . . . . . . . . . . . . . . . 34 4.1.5 Proportions . . . . . . . . . . . . . . . . . . . . . . . . . . 36 4.2 Operators, Expressions and Statements . . . . . . . . . . . . . 37 4.2.1 Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.2.2 Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.2.3 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . 39 4.2.4 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.2.5 Exponentials . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.2.6 Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.3 Operator Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.4 Interchanging Variables and Values . . . . . . . . . . . . . . . 43 4.5 Shorthand Operators . . . . . . . . . . . . . . . . . . . . . . . . 44
5 Strings and Console Output
47
5.1 Minecraft Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.1.1 Hello Minecraft World . . . . . . . . . . . . . . . . . . . 48
5.1.2 Inputting Your Message . . . . . . . . . . . . . . . . . . 49
5.1.3 User Name . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.1.4 Mad Libs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.1.5 Create a Block with Input . . . . . . . . . . . . . . . . . 52
5.1.6 Sprint Record . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.2.1 Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.3 String Functions and Methods . . . . . . . . . . . . . . . . . . 56
5.3.1 len() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.3.2 .lower() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.3.3 .upper() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.4 str() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.4 Print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.4.1 Printing String Variables . . . . . . . . . . . . . . . . . 60
5.4.2 Joining Strings . . . . . . . . . . . . . . . . . . . . . . . . 61
5.4.3 Concatenating Integers, Floats and Booleans . . . . . 62
5.4.4 Placeholders in Strings . . . . . . . . . . . . . . . . . . . 62
5.4.5 raw_input() . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.4.6 input() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.5 Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.5.1 Getting the Current Date and Time . . . . . . . . . . . 66
6 Comparators and Control Flow
67
6.1 Minecraft Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 67
CONTENTS
iii
6.1.1 Swimming . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6.1.2 Do you want to stop smashing things? . . . . . . . . . 69 6.1.3 Bring us a shrubbery . . . . . . . . . . . . . . . . . . . . 72 6.1.4 Take a Shower . . . . . . . . . . . . . . . . . . . . . . . . 74 6.1.5 Secret Passage . . . . . . . . . . . . . . . . . . . . . . . . 75 6.2 Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 6.2.1 Equal To . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 6.2.2 Not Equal To . . . . . . . . . . . . . . . . . . . . . . . . . 78 6.2.3 Less Than . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 6.2.4 Less Than or Equal To . . . . . . . . . . . . . . . . . . . . 80 6.2.5 Greater Than . . . . . . . . . . . . . . . . . . . . . . . . . 81 6.2.6 Greater Than or Equal To . . . . . . . . . . . . . . . . . . 83 6.3 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 84 6.3.1 and . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 6.3.2 or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 6.3.3 not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 6.3.4 Boolean Operator Order . . . . . . . . . . . . . . . . . . 88 6.4 If, Else and Elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 6.4.1 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . 89 6.4.2 else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 6.4.3 elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 6.4.4 Nested If statements . . . . . . . . . . . . . . . . . . . . 95 6.4.5 Checking For Letters . . . . . . . . . . . . . . . . . . . . 96
7 Functions
99
7.1 Minecraft Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.1.1 A Forest . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.1.2 Arming TNT . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.1.3 Wool Colour . . . . . . . . . . . . . . . . . . . . . . . . . 103
7.1.4 Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
7.1.5 Import Block Module . . . . . . . . . . . . . . . . . . . . 106
7.2 Function syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
7.2.1 Calling a function . . . . . . . . . . . . . . . . . . . . . . 108
7.2.2 Return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
7.2.3 Multiple Arguments . . . . . . . . . . . . . . . . . . . . 110
7.3 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
7.3.1 Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
7.3.2 from . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
7.3.3 Import All * . . . . . . . . . . . . . . . . . . . . . . . . . . 113
7.4 Built-in Functions and Methods . . . . . . . . . . . . . . . . . . 114
7.4.1 max() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
iv
CONTENTS
7.4.2 min() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 7.4.3 abs() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 7.4.4 type() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
8 Lists and Dictionaries
119
8.1 Minecraft Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 119
8.1.1 Glitching Sign . . . . . . . . . . . . . . . . . . . . . . . . 119
8.1.2 Block by Numbers . . . . . . . . . . . . . . . . . . . . . . 121
8.1.3 Team Camera . . . . . . . . . . . . . . . . . . . . . . . . . 122
8.1.4 Dictionary of Wool . . . . . . . . . . . . . . . . . . . . . 123
8.1.5 Hacking a Friend's Game . . . . . . . . . . . . . . . . . 124
8.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
8.2.1 Defining a list . . . . . . . . . . . . . . . . . . . . . . . . 126
8.2.2 Accessing a list item . . . . . . . . . . . . . . . . . . . . 127
8.2.3 Changing a list item . . . . . . . . . . . . . . . . . . . . . 128
8.3 List Capabilities and Functions . . . . . . . . . . . . . . . . . . 128
8.3.1 Adding an item . . . . . . . . . . . . . . . . . . . . . . . 129
8.3.2 List Length . . . . . . . . . . . . . . . . . . . . . . . . . . 130
8.3.3 List Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . 130
8.3.4 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
8.3.5 Inserting an Item . . . . . . . . . . . . . . . . . . . . . . 132
8.3.6 Removing an Item . . . . . . . . . . . . . . . . . . . . . . 133
8.3.7 Looping through a list . . . . . . . . . . . . . . . . . . . 134
8.3.8 Sorting a list . . . . . . . . . . . . . . . . . . . . . . . . . 135
8.3.9 Adding Together Items in a List . . . . . . . . . . . . . . 136
8.4 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
8.4.1 Defining a Dictionary . . . . . . . . . . . . . . . . . . . . 137
8.4.2 Accessing Items in Dictionaries . . . . . . . . . . . . . 138
8.4.3 Changing/Adding an Item with a Dictionary . . . . . 138
8.4.4 Deleting Items in Dictionaries . . . . . . . . . . . . . . 139
9 Functions and Lists
141
9.1 Minecraft Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 141
9.1.1 Pixel Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
9.1.2 Shadow Castle . . . . . . . . . . . . . . . . . . . . . . . . 143
9.2 Using Functions with Lists . . . . . . . . . . . . . . . . . . . . . 144
9.2.1 Lists as Arguments . . . . . . . . . . . . . . . . . . . . . 144
9.2.2 Loops and Lists in Functions . . . . . . . . . . . . . . . 145
9.2.3 Modifying Each List Item . . . . . . . . . . . . . . . . . 145
9.2.4 Functions to Modify Each Item in a List . . . . . . . . . 147
9.2.5 More On range() . . . . . . . . . . . . . . . . . . . . . . . 147
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- descriptor term acceptable use of technology
- minecraft in the classroom ideas inspiration and
- terminal markings and connections part winding
- the ultimate player s guide to minecraft
- dket ⋙ star wars epic star wars minecraft creations
- a python book beginning python advanced python and
- computing arghbox
- plugin development for dynamics 365
- mount rainier national park npmaps
- hacking secret ciphers with python
Related searches
- formula for computing interest on a loan
- computing average product cost calculator
- computing formula standard deviation
- computing the inverse of a matrix
- major computing trends
- current trends in computing technology
- computing system definition
- formula for computing compound interest
- computing sample standard deviation
- cloud computing applications list
- cloud computing benefits for businesses
- advantages of cloud computing for business