Potato Pirates Python PDF
Potato Pirates Python PDF
Contents
1. Getting Started
a. What is Python? 3
b. What is a programming language? 3
c. Downloading & installing IDLE 4
d. My first Python program 5
e. Python Shell vs Python Script 5
f. How to run my Python program? 6
2. Chapter 1: Variables
a. What are variables? 9
b. Get the value of a variable 10
c. Comments 10
d. Update the value of a variable 11
e. Why do we use variables? 11
f. Mathematical operators 12
g. Strings 13
h. String concatenation 14
i. String indexing 15
j. Data types 16
k. Type casting 16
l. Exercise 17
3. Chapter 2: For-Loop
a. What is a for loop? 20
b. Breakdown (for loop) 21
c. Nested for loop 22
d. Breakdown (nested for loop) 23
e. Exercise 24
7. Answer Sheet 42
1. Go to https://www.python.org/downloads/
2. Download Python 3. The first number determines the main
version. (version 3.6.4 is the latest version at the time of writing).
Install
1. Run the installation file which has been downloaded.
2. Click “Continue”
3. Accept the software license agreement.
4. You may customise or accept the default installation folder. If
unsure, just accept the default settings.
5. Installation should begin. Please wait for a few minutes.
6. Voila! You are now ready to program some Python code.
Opening IDLE
Windows Macintosh OS
1. Go to Start button. 1. Press cmd + space
2. Type “IDLE” in the “Search 2. Type “IDLE”
programs and files textbox”.
2. Writing Code
Type the following code:
print (“Ahoy Matey”)
Let’s keep
moving
Congratulations forward!
! You just learnt
to write your first
line of code.
crew = 10
crew = 10
Value
The value stored in this variable “crew” is 10.
Later on, you will see that we can store other
types of values instead of just numbers.
© Copyright Codomo Pte Ltd 2018 9
Variables
Get the value of a variable
After storing a value in a variable we can retrieve it by “calling” its
name. The code below demonstrates how to retrieve a value stored
in a variable.
Python Code:
crew = 10 # Set variable “crew” as 10
print(crew) # print the value of “crew”
Shell:
Comments ( # symbol )
A comment is a programmer-readable explanation which is usually
used to explain the meaning of certain lines of code. Words
appearing after # will be ignored by the computer. Very useful!
BODMAS
Order Multiplication: * Subtraction: -
Python Code:
control1 = "for 3 times, " # Create variable “control1”
action1 = "roast" # Create variable “action1”
print(control1) # print the value of control1
print(action1) # print the value of action1
Console:
Python Code:
Console:
Note: foo and bar are just variable names. They don’t mean anything.
bar = “POTATO”
0 1 2 3 4 5
Let’s extract the letter ‘A’. Below is the Python code to extract it.
Python Code:
bar = "POTATO" # Create variable “bar”
foo = bar[3] # Get the letter ‘A’
print(foo) # print the concatenated string
Console:
Data Type
Integer Lists
String Dictionaries
Character Tuples
Type casting
Python includes methods to convert from one data type to another.
Run the code below on IDLE to convert.
Convert Integer to String
x = 3 # Create variable “x” that stores the integer 3
x_str = str(x) # Converts x from integer to string
print(x_str + " potatoes") # prints the concatenated string
# The final result should print “3 potatoes”
k = 20 + 10 * 20 -20
print(k)
k = ___
z = 100 % 49
print(z)
z = ___
x = 5
x = x + 1
print(x) x = ___
y = x + 10
y = 2*x + 1
print(y) y = ___
Data Types
for i in range(0,3,1):
print("Mash")
Type this out in IDLE and see what it does. When you’re done,
let’s take a closer look at each part of the control statement.
for i in range(0,1000,1):
Legend
The letter ‘i’ is a variable that we use to represent data in the range function.
Start: When the loop starts, ‘i’ will carry this value
Stop: The loop ends if ‘i’ is more than or equal to this value
Step: Change ‘i’ by this value from one iteration to the next
Below shows the process of how a for loop will work. On the first
iteration, the variable “i” will carry the value of 0. On the next
iteration, it will increase by 1. The iteration continues until the
condition is no longer met.
Iteration Value of i
3rd 2
.. ...
1000th 999
Loop-ception.
What a beauty!
Inner Loop
for i in range(0,3,1):
for j in range(0,3,1):
print("Fry")
Outer Loop
for i in range(0,3,1):
for j in range(0,3,1):
print("Fry")
Code:
for i in range(5, __, 5):
enemy_crew = enemy_crew - 1
Below shows the process of how a while loop will work. Assuming
the enemy begins with 7 crew members. On the first iteration,
while loop will check if the crew is greater than 4. Since 7 > 4 is
true, it will execute Mash to reduce the crew by 2. On the next
iteration, the Mash action will run again as 5 > 4. Finally, it stops at
the third iteration as 3 > 4 is false.
3rd 3 FALSE NA
Logical Operators
Logical operators are typically used with boolean values. When
they are, they return a boolean value. Below are some examples.
Python Code:
crew1 = 3
crew2 = 5
isDead = False
print(crew1 == 3)
print(crew2 <= 2)
print(crew1 > crew2)
print(crew1 > crew2 and crew2 == 5)
print(crew1 > crew2 or crew2 == 5)
print(not isDead)
Console:
crew = 20
while (crew == 10):
crew = crew - 1
print(crew)
crew = ___
This is the
enemy
A B C
3) Which card stack can deal the highest damage to the enemy?
Code all 3 card stacks in Python.
Type the code out in IDLE and see what it does. Remember to
declare the variable “crew”. When you’re done, let’s take a closer
look at the if statement.
Legend
Condition: A boolean expression. It will return True or False
In Potato Pirates
Draw 3 cards
In Python
crew = 2
if (crew == 1):
print("get a new ship and a potato")
elif (crew == 2):
print("pick a card from the discard pile")
elif (crew == 3):
print("draw 3 cards")
else:
print("you get nothing")
NOTE: The conditions will be checked in order and only the first true statement will
be run!
© Copyright Codomo Pte Ltd 2018
37
If-Else and Switch (Exercise)
Convert Potato Pirates to Python
crew = 10 crew = 10
name = “PotatoPirates” crew = crew + 2
print (crew)
Equal == Add +
And and
Not equal != Subtract -
Or or
Less than < Multiply *
Not not
Greater than > Divide /
1. 2. 3.
x = 10 a = 10 b = 12
x = x + 5 a = a / 2 b = b % 3
print(x) print(a) print(b)
1. 2.
a = "Hello " word1 = "POTATO PIRATES"
b = "Potato King" s_letter = word1[13]
print(a+b) print(s_letter)
Data Types
1.
x = "10"
x_int = int(x)
print(x_int + 20)
2.
y = 7
y_str = str(y)
print(y_str + " Potato Kings")
Basic Questions
1. 2.
for i in range(1,101,1): for i in range(3,21,1):
print(i) print(i)
1.
enemy_crew = 10
for i in range(0,2,1):
enemy_crew = enemy_crew - 1
print(enemy_crew)
2.
y = 2
enemy_crew = 30
for i in range(0,3,1):
for j in range(0,y,1):
enemy_crew = enemy_crew - 3
print(enemy_crew)
3. 3
4. 11, 12, 13, 14, or 15
Basic Questions
1. 2.
crew = 10 crew = 10
while (crew >= 1): while (crew >= 5):
print(crew) print(crew)
crew = crew - 1 crew = crew - 1
3a. 3b.
crew = 16 crew = 16
while (crew > 5): for i in range(0,3,1):
crew = crew - 3 crew = crew - 3
print(crew) print(crew)
print(crew)
print(enemy_crew)
2.
enemy_crew = 10
else:
while (enemy_crew > 6):
enemy_crew = enemy_crew - 2
print(enemy_crew)
if (crew == 1):
print("I am dying!")
elif (crew == 2):
print("Am I dying?")
elif (crew == 3):
print("I am an unlucky pirate")
elif (crew == 4):
print("I shall not give up!")
else:
print("There's nothing to worry about")