Appendix B
Quick Reference
Table 1 Python input, output, variables
Leaving reminders by using Using constants
comments
# whole-line comment SIZE = 100 # upper case name
print("hello") # end-of-line comment
Printing to the screen Reading from the keyboard
(Python V2)
print(10) # print a number name = raw_input("your name?")
print("hello") # print a string age = int(raw_input("how old?"))
print(a) # print a variable nextYearAge = age + 1
Storing values in variables Using Boolean variables
a = 10 # an integer (whole) number anotherGo = True
b = 20.2 # a decimal number while anotherGo:
message = "hello" # a string choice = int(raw_input("choice?"))
finished = True # a Boolean if choice == 8:
high_score = None # no value anotherGo = False
Converting strings to (integer) Converting numbers to strings
numbers
size = int(size) print("your age is:" + str(age))
Calculating with number Calculating absolute values and
variables differences
b = a + 1 # addition absolute = abs(-1) # absolute is 1
c = a - 1 # subtraction difference = abs(x1-x2)
d = a * 2 # multiplication smallest = min(x1, x2)
e = a / 2 # division largest = max(x1, x2)
f = a % 2 # remainder after division
BC1
02_9781118946916-[Link] 1 10/16/2014 10:50:30 AM
Table 2 Python loops
Looping Using counted loops
a = 0 for a in range(10):
while a<100: print(a) # 0..9
print(a) for a in range(5, 50, 5):
a = a + 1 print(a) # 5..45 in steps of 5
Looping through all characters Splitting comma-separated
in a string strings
name = "Charlotte" line = "one,two,three"
for ch in name: data = [Link](",")
print(ch) for d in data:
print(d)
Table 3 Python conditions
Using if statements Using if/else statements
if a==42: if a==5:
print("the ultimate answer") print("correct")
else:
print("wrong")
Using elif/else for multiple choices Checking multiple conditions
with and, or
if a==1: if choice>0 and choice<=8:
print("option A") print("in range")
elif a==2: if choice<1 or choice>8:
print("option B") print("out of range")
else:
print("anything else")
Checking for different or same Checking for less with if
with if
if a!=1: if a<1:
print("not equal") print("less than")
if a==1: if a<=1:
print("equal") print("less than or equal")
Checking opposites with not Checking for greater with if
playing = True if a>1:
if not playing: print("greater than")
print("finished") if a>=1:
print("greater than or equal")
BC2 ADVENTURES IN MINECRAFT
02_9781118946916-[Link] 2 10/16/2014 10:50:30 AM
Table 4 Python functions
Defining and calling functions Passing parameters to functions
def myname(): def hello(name):
print("my name is Laura") print("hello " + name)
myname() hello("Andrew")
myname() hello("Geraldine")
Returning values from functions Writing to global variables inside
functions
def smallest(a, b): treasure_x = 0
if a<b:
return a def build():
return b global treasure_x
print(smallest(10, 20)) treasure_x = 10
Table 5 Python lists
Creating lists Adding to the end of a list
a = [] # an empty list [Link]("hello")
a = [1, 2, 3]#an initialised list
Printing the contents of a list Working out the size of a list
print(a) print(len(a))
Accessing items in a list by Accessing the last item in a list
their index
print(a[0]) # 0=first item, 1=second print(a[-1])
Removing the last item from a list Looping through all items in a list
word = [Link]() # remove last item for name in ["David","Gail","Janet"]:
print(word) # item just removed print(name)
Table 6 Other Python modules
Delaying for a short amount of time Generating random numbers
import time import random
[Link](1) # wait 1 second print([Link](1,100))
Getting the current date/time Using maths functions
import datetime import math
dateNow = [Link]() radians = [Link](angle)
import time sin = [Link](radians)
timeNow = [Link]() cos = [Link](radians)
squareRoot = [Link](number)
APPENDIX B QUICK REFERENCE BC3
02_9781118946916-[Link] 3 10/16/2014 10:50:30 AM
Table 7 File processing
Reading lines from a file Writing lines to a file
f = open("[Link]", "r") f = open("[Link]", "w")
tips = [Link]() [Link]("Victoria:26000\n")
[Link]() [Link]("Amanda:10000\n")
for t in tips: [Link]("Ria:32768\n")
print(t) [Link]()
Getting a list of matching Stripping unwanted white space
filenames from strings
import glob a = "\n\n hello \n\n"
names = [Link]("*.csv") a = [Link]()
for n in names: print(a)
print(n)
Table 8 Accessing hardware
Configuring GPIO pins Reading and writing GPIO pins
import [Link] as GPIO #RaspberryPi [Link](5, True) # on (3.3V)
import [Link] as GPIO #Arduino [Link](5, False) # off (0V)
[Link]([Link]) if [Link](6) == False:
[Link](5, [Link]) #output print("pressed")
[Link](6, [Link]) #input
Safely cleaning up after using Using the 7-segment display module
the GPIO
try: import anyio.seg7 as display
do_something() # put code here LED_PINS = [10,22,25,8,7,9,11,15]
finally: ON = False # Common-Anode
[Link]() ON = True # Common-Cathode
[Link](GPIO, LED_PINS, ON)
Writing characters to the 7-segment Other 7-segment display functions
display
[Link]("3") [Link](True) # point on
[Link]("A") [Link](False) # point off
[Link]("up") [Link]()
[Link]([1,1,0,1,1,0,1,0])
BC4 ADVENTURES IN MINECRAFT
02_9781118946916-[Link] 4 10/16/2014 10:50:30 AM
Table 9 Minecraft API
These are the key functions of the Minecraft Python API. For a complete list of functions,
visit: [Link]/p/[Link].
Importing the Minecraft API Importing and using the block name
constants
import [Link] as minecraft import [Link] as block
b = [Link]
Creating a connection to Minecraft Posting a message to the Minecraft
chat
mc = [Link]() [Link]("Hello Minecraft")
Getting the player’s tile position Setting the player’s tile position
# what are the coordinates of tile x = 5
# that player is standing on? y = 3
pos = [Link]() z = 7
x = pos.x [Link](x, y, z)
y = pos.y
z = pos.z
Getting the player’s position Setting the player’s position
# get precise position of player # set precise position of player
# in the world (e.g. x=2.33) x = 2.33
pos = [Link]() y = 3.95
x = pos.x z = 1.23
y = pos.y [Link](x, y, z)
z = pos.z
Getting the height of the world Getting the block type at a position
# y position of first non-AIR block # id of block (e.g. [Link])
height = [Link](5, 10) blockId = [Link](10, 5, 2)
Setting/changing a block at a Setting/changing lots of blocks in
position one go
[Link](5, 3, 2, [Link]) [Link](0,0,0, 5,5,5, block.
[Link])
Finding out which blocks have Clearing any block hits
been hit
# which blocks hit since last time? # clear(ignore) hits since last time
events = [Link]() [Link]()
for e in events:
pos = [Link]
print(pos.x)
APPENDIX B QUICK REFERENCE BC5
02_9781118946916-[Link] 5 10/16/2014 10:50:30 AM
Table 10 Minecraft API block types
Here is a sample of the block types available in Minecraft. Where appropriate, the data
values that can be used are included, and the Pi and PC/Mac columns show whether this
block can be used on those platforms. For a complete list of block IDs and block data
values, visit: [Link]/p/[Link].
ID Constant Data ID Sub-type Pi PC/MAC
0 AIR - - Y Y
1 STONE - - Y Y
2 GRASS - - Y Y
3 DIRT - - Y Y
4 COBBLESTONE - - Y Y
5 WOOD_PLANKS 0 Oak Y Y
1 Spruce N Y
2 Birch N Y
3 Jungle N Y
7 BEDROCK - - Y Y
8 WATER - - Y Y
9 WATER_STATIONARY 0 High Y Y
..7 Low Y Y
10 LAVA - - Y Y
11 LAVA_STATIONARY 0 High Y Y
..7 Low Y Y
12 SAND - - Y Y
13 GRAVEL - - Y Y
14 GOLD_ORE - - Y Y
15 IRON_ORE - - Y Y
16 COAL_ORE - - Y Y
17 WOOD 0 Oak (up/down) Y Y
1 Spruce (up/ Y Y
down)
2 Birch (up/down) Y Y
3 Jungle (up/ N Y
down)
4 Oak (east/west) N Y
5 Spruce (east/ N Y
west)
6 Birch (east/ N Y
west)
7 Jungle (east/ N Y
west)
BC6 ADVENTURES IN MINECRAFT
02_9781118946916-[Link] 6 10/16/2014 10:50:30 AM
ID Constant Data ID Sub-type Pi PC/MAC
8 Oak (north/ N Y
south)
9 Spruce (north/ N Y
south)
10 Birch (north/ N Y
south)
11 Jungle (north/ N Y
south)
12 Oak (only bark) N Y
13 Spruce (only N Y
bark)
14 Birch (only N Y
bark)
15 Jungle (only N Y
bark)
18 LEAVES 1 Oak leaves Y Y
2 Spruce leaves Y Y
3 Birch leaves Y Y
20 GLASS - - Y Y
24 SANDSTONE 0 Sandstone Y Y
1 Chiselled Y Y
Sandstone
2 Smooth Y Y
Sandstone
35 WOOL 0 White Y Y
1 Orange Y Y
2 Magenta Y Y
3 Light Blue Y Y
4 Yellow Y Y
5 Lime Y Y
6 Pink Y Y
7 Grey Y Y
8 Light grey Y Y
9 Cyan Y Y
10 Purple Y Y
11 Blue Y Y
12 Brown Y Y
13 Green Y Y
14 Red Y Y
15 Black Y Y
continued
APPENDIX B QUICK REFERENCE BC7
02_9781118946916-[Link] 7 10/16/2014 10:50:30 AM
Table 10 continued
ID Constant Data ID Sub-type Pi PC/MAC
37 FLOWER_YELLOW - - Y Y
38 FLOWER_CYAN - - Y Y
41 GOLD_BLOCK - - Y Y
42 IRON_BLOCK - - Y Y
45 BRICK_BLOCK - - Y Y
46 TNT 0 Inactive Y Y
1 Ready to Y Y
explode
49 OBSIDIAN - - Y Y
50 TORCH 0 Standing on the Y Y
floor
1 Pointing east Y Y
2 Pointing west Y Y
3 Pointing south Y Y
4 Pointing north Y Y
53 STAIRS_WOOD 0 Ascending east Y Y
1 Ascending west Y Y
2 Ascending south Y Y
3 Ascending north Y Y
4 Ascending east Y Y
(upside down)
5 Ascending west Y Y
(upside down)
6 Ascending south Y Y
(upside down)
7 Ascending north Y Y
(upside down)
57 DIAMOND_BLOCK - - Y Y
80 SNOW_BLOCK - - Y Y
89 GLOWSTONE_BLOCK - - Y Y
246 GLOWING_OBSIDIAN - - Y N
247 NETHER_REACTOR 0 Unused Y N
1 Active Y N
2 Stopped/used up Y N
BC8 ADVENTURES IN MINECRAFT
02_9781118946916-[Link] 8 10/16/2014 10:50:30 AM
Table 11 MinecraftStuff API (MinecraftDrawing)
Importing the MinecraftStuff API Creating the MinecraftDrawing
Object
import [Link] as ↵ mc = [Link]()
minecraftstuff mcdrawing =
[Link](mc)
Drawing a line between two points Getting all the block positions of a
line, as a list
[Link](0, 0, 0, line = [Link](
0,0,0,10,10,10)
10, 10, 10, [Link])
pos1 = line[0]
print(pos1.x)
Drawing a sphere Drawing a circle
[Link](0, 0, 0, [Link](0, 0, 0,
radius, [Link]) radius, [Link])
Drawing a flat polygon(e.g. a triangle)
tri = []
filled = True
[Link](minecraft.Vec3(0,0,0))
[Link](minecraft.Vec3(10,0,0))
[Link](minecraft.Vec3(5,10,0))
[Link](tri, filled,
[Link])
Table 12 MinecraftStuff API (MinecraftShape)
Creating a MinecraftShape Drawing and clearing a shape
mc = [Link]() [Link]()
blocks =
[[Link](0,0,0, [Link]()
[Link]),
[Link](1,0,0,
[Link])]
pos = Vec3(0,0,0)
shape =
[Link](mc,
pos, blocks)
Moving a shape to a position Moving a shape by a number of blocks
[Link](10, 10, 10) ymove = 1
[Link](0, ymove, 0)
APPENDIX B QUICK REFERENCE BC9
02_9781118946916-[Link] 9 10/16/2014 10:50:30 AM
02_9781118946916-[Link] 10 10/16/2014 10:50:30 AM