Appendix B

Quick Reference

Table 1 Python input, output, variables

Leaving reminders by using comments

Using constants

# whole-line comment

print("hello") # end-of-line comment

SIZE = 100 # upper case name

Printing to the screen

Reading from the keyboard (Python V3)

print(10) # print a number

print("hello") # print a string

print(a) # print a variable

name = input("your name?")

age = int(input("how old?"))

Storing values in variables

Using Boolean variables

a = 10 # an integer (whole) number

b = 20.2 # a decimal number

message = "hello" # a string

finished = True # a Boolean

high_score = None # no value

anotherGo = True

while anotherGo:

choice = int(input("choice?"))

if choice == 8:

anotherGo = False

Converting strings to (integer) numbers

Converting numbers to strings

size = int(size)

size = size + 1

age = str(age)

print("Your age is " + age)

Calculating with number variables

Calculating absolute values and differences

b = a + 1 # addition

c = a - 1 # subtraction

d = a * 2 # multiplication

e = a / 2 # division

f = a % 2 # remainder after division

absolute = abs(-1) # absolute is 1

difference = abs(x1-x2)

smallest = min(x1, x2)

largest = max(x1, x2)

Table 2 Python loops

Looping

Using counted loops

a = 0

while a<100:

print(a)

a = a + 1

for a in range(10):

print(a) # 0..9

for a in range(5, 50, 5):

print(a) # 5..45 in steps of 5

Looping through all characters in a string

Splitting comma-separated strings

name = "Charlotte"

for ch in name:

print(ch)

line = ...

Get Adventures in Minecraft, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.