Page 1
Yes! Kids can learn PythonChapter 1
Copyright 2018-2020 © Kent Tong (i-Study)
1
Page 2
Go to https://p4kweb.appspot.com
Click this button.
Page 3
Step forward, execute and reset
1: Input this.
2: Execute. Then do it once more. 3: Reset the game &
then execute again.
Page 4
Command, function and call a function
step is a command/function.
To execute a command, you put parentheses () after it.
To call a function is the same as to execute a command.
Page 5
Exercise: walk to the treasure box
Page 6
Sample solution● Click the Exec button five times.
Page 7
Can input multiple commands
They will be executed one by one.
The whole thing is called a program.
Page 8
Exercise: Walk to the treasure box in one execution
Page 9
Sample solution
step()step()step()step()step()
Page 10
What if you spell the command incorrectly?
stop()step()step()step()step()
You will get an error message.
Page 11
Pick up the treasure box
pickup()
Page 12
Exercise: Walk to the treasure box & pick it up
Page 13
Sample solution
step()step()step()step()step()pickup()
Page 14
What does jump do? Try it
jump()
Page 15
Change to another background
Choose this background.
Page 16
Exercise: Jump over the hole & pick up the box
Page 17
Sample solution
step()step()jump()step()pickup()
Page 18
What does walk(3) do? Try it
Page 19
Argument
walk(3)
3 is called an argument here. It doesn't mean arguing with someone. It is the information you hand to the command.
Page 20
Exercise: Call walk & then pick up the box
Page 21
Sample solution
walk(2)jump()step()pickup()
Page 22
Not every function accepts an argument
walk(2) step(2)jump(3)
Page 23
Perform calculation
walk(1+2)walk(3-1)walk(2*3)
Page 24
What does print do? Try it
print(123)print(2*6)print(2*(3+1))print(10/2)
Page 25
It outputs the values
The output goes to here.
Page 26
Calculate & print the result● A slice of cake costs 6 dollars. A bottle of soda costs 5 dollars. How much do
they cost in total?
Page 27
Sample solution
print(6+5)
Page 28
Use variables to show the meanings of numbers
print(6+5) cake=6soda=5print(cake+soda)What does 6 mean?
Page 29
cake=6 creates a variable
cake=6soda=5print(cake+soda)
This line creates a variable named "cake" and its value is 6.
cake
6
Page 30
A variable must have two things: a name & a value
cake=6soda=5print(cake+soda)
cake
6
(name)
(value)
Page 31
Exercise: Draw a diagram to show the variable soda
cake=6soda=5print(cake+soda)
Page 32
Sample solution
cake=6soda=5print(cake+soda)
soda
5
Page 33
Looking up the value of a variable
cake=6soda=5print(cake+soda)
soda
5
cake
6
Page 34
What if there is a typo in the variable name?
cake=6soda=5print(coke+soda)
soda
5
cake
6
Can't find a variable named "coke".
Page 35
You will get the "not defined" error
Page 36
When creating a variable, a value is calculated first
a=2+6print(a)
The value is calculated first (8 in this case).
Then, let the variable point to that value.
a
8
Page 37
Draw a diagram to show the variable b
b=3*5print(b)
Page 38
Sample solution
b
15
Page 39
Can use a variable to store the result
cake=6soda=5print(cake+soda)
cake=6soda=5total=cake+sodaprint(total)
Page 40
Exercise: How many variables are created below? Draw a diagram to show eachcake=6soda=5total=cake+sodaprint(total)
Page 41
Sample solution: three
soda
5
cake
6
total
11
cake=6soda=5total=cake+sodaprint(total)
Page 42
Calculate & print the result while using variables● A slice of cake costs 6 dollars. You pay 10 dollars. What is the change?
cake=6soda=5total=cake+sodaprint(total)
For reference
Page 43
Sample solution
cake=6payment=10change=payment-cakeprint(change)
Page 44
Draw a diagram to show the variables below
cake=6payment=10change=payment-cakeprint(change)
Page 45
Sample solution
cake=6payment=10change=payment-cakeprint(change)
payment
10
cake
6
change
4
Page 46
Calculate & print the result while using variables● A slice of cake costs 6 dollars. How much are 3 slices of cake?
Page 47
Sample solution
cake=6slices=3total=cake*slicesprint(total)
Page 48
Calculate & print the result while using variables● 3 bars of chocolate cost 12 dollars. How much is a bar of chocolate?
Page 49
Sample solution
total=12bars=3chocolate=total/barsprint(chocolate)
Page 50
Exercise: Jump onto the platform & get the box
Choose this background first.
Page 51
Sample solution
walk(2)jump()walk(2)pickup()
Page 52
Exercise: go upstairs & get the box
Choose this background first.
Page 53
Sample solution
walk(4)jump()walk(4)jump()walk(4)pickup()
Page 54
Exercise: Use a variable to represent 4walk(4)jump()walk(4)jump()walk(4)pickup()
_____=4walk(_____)jump()walk(_____)jump()walk(_____)pickup()
Page 55
Sample solution
width=4walk(width)jump()walk(width)jump()walk(width)pickup()
the width of the staircase