Scratch vs Python Examples
This page is for comparing Scratch code to Python code to help you use your knowledge of one to help learn the other. They aren’t as different as you might think.
Comments
Comments are bits of text in the code that don’t do anything and are there for helping people reading the code to understand it.
// This is a Scratch comment.
// You can create them by right clicking on the script background or a code block.
# This is a Python comment.
# Anything after a hash symbol (#) on a line is a comment
Variables
Set a number variable
set (age) to [9]
age = 9
Set a text variable
In computer language a chunk of text is also called a string
set (name) to [Scratch]
In Python, a string variable needs to be wrapped in quote marks. They can be either single or double quotes.
name = 'Scratch'
or
name = "Scratch"
Double quotes are good if you want to put a single quote or apostrophe in your string:
telling_off = "Don't pick your nose!"
Otherwise Python can’t tell the difference between the apostrophe and the end of the string.
Add a number on to a variable (the long way)
set (score) to ((score) + (10))
score = score + 10
Add a number on to a variable (the short way)
change (score) by [10]
score += 10
Subtract a number from a variable (the short way)
change (score) by [-10]
score -= 10
Adding strings to each other
set (full name) to (join (first name) (last name))
full_name = first_name + last name
Note: you may notice that we didn’t add a space between those names - whoops! That’s right, the full names will be all squished up. Here’s a way to add the space:
set (full name) to (join (first name) (join ( ) (last name)))
full_name = first_name + ' ' + last name
Input and Output
Display some text on the screen:
say [Hello world!]
print('Hello world!')
Ask for some text and display it:
ask [How many cookies can you eat?] and wait
say (join (answer) [ cookies is a lot! ])
cookies = input('How many cookies can you eat?')
print(cookies + ' cookies is a lot!')
Custom code
If you have some code that gets repeated a lot, it is a good idea to make a reusable block of code you can call from other bits of code. In Scratch these are called custom blocks, and in Python they are called functions.
define greeting (name)
say (join [Hello ] (name))
when flag clicked
greeting [Matilda]
In Python def
is short for ‘define function’
def greeting(name):
print("Hello " + name)
greeting('Matilda')
Comparisons
Comparisons are tests where the answer can only be True or False. They can can be used as conditions in decisions or loops.
< (age) = (10) > // Check age equals 10
< (age) > (10) > // Check age is greater than 10
< (age) < (10) > // Check age is less than 10
< not < (age) = (10) >> // Check age does NOT equal 10
< <(age) > (5)> and <(age) < (15)> > // Check age is greater than 5 and less than 15
age == 10
age > 10
age < 10
age != 10
age > 5 and age < 15
In Python a double ==
is for comparing if two things are equal, while a single =
is for setting a variable to a value.
Decisions
if then
Use this code when you want something to happen only if the condition is True.
if < (age) > (20) > then
say [You're old! ]
if age > 20:
print("You're old!")
if then else
Use this code when you want two different things to happen depending on whether the condition is True or False.
if < (age) > (20) > then
say [You're old! ]
else
say [You're young ]
if age > 20:
print("You're old!")
else:
print("You're young")
Loops
Loop forever
forever
do something :: looks
while True:
something()
Loop until
The Scratch loop runs while the condition is False, but the Python loop runs while the condition is True. This is why each one has a different condition.
repeat until < (score) = (0) >
do something :: looks
while score > 0:
something()
Loop a number of times
repeat (7)
do something :: looks
for i in range(7):
something()
Random numbers
set (dice roll) to (pick random (1) to (6))
In Python you need to import a random module to get the extra code for random numbers. At the top of your program, add the following line:
from random import randint
or:
from random import *
The second example imports all sorts of other random number stuff too.
Then later on in your program when need a random number, you can use an example like this:
dice_roll = randint(1, 6)