Scratch and Python Basics
![Scratch And Python Syntax Scratch And Python Syntax](https://cdn-wordpress-info.futurelearn.com/wp-content/uploads/5915415d-a820-487a-8cd8-9ef82421434b.png.webp)
One aspect of using a text-based language that many learners struggle with is understanding the specific syntax (the rules of the language) required. Mistakes made in a program are often due to these rules not being followed, and are called syntax errors. It is therefore helpful to show the parallels and differences between a language that a learner has already mastered and the one they are trying to learn.
Shown below are a few Scratch blocks and their equivalent code in Python. The list is far from exhaustive, and is intended as a reference guide rather than an exercise to be worked through.
Variables in Scratch and Python
-
- In Scratch, a variable needs to be created before it can be assigned a value, whereas in Python a variable is created upon assignment with a value.
- In Python, it is necessary to surround strings (any text) with either single (
'
) or double ("
) quotes.
Increment a Variable
-
- In Scratch, a variable’s value can be increased or decreased.
-
- In Python, a variable’s value can be increased or decreased by reassigning it to itself with the addition or subtraction of a number.
Scratch and Python Outputs
-
- In Scratch, you make a sprite talk to provide output to the user of the program.
-
- Python uses
print
statements to output to the shell.
- Python uses
-
- Again in Python, you need to use single or double quotes if you are printing strings.
Conditional Loops in Scratch and Python
-
- Scratch’s conditional loop repeats until a certain statement is
True
.
- Scratch’s conditional loop repeats until a certain statement is
-
- Python’s conditional loop repeats as long as a certain statement is
True
.
- Python’s conditional loop repeats as long as a certain statement is
-
- There needs to be a colon (
:
) at the end of the statement in Python.
- There needs to be a colon (
-
- Notice that the code that is inside the loop is indented. Indentation is normally four spaces or a single tab. This can be compared to the way the Scratch conditional loop block brackets the code within it.
-
- The example above isn’t the simplest way of doing this in Python though. Using the
while
loop, it is easier to check that the variable is less than or equal to10
.
- The example above isn’t the simplest way of doing this in Python though. Using the
while foo <= 10:
print(foo)
Infinite Loops in Scratch and Python
-
- Scratch has a specific type of loop that is infinite.
-
- In Python, a conditional loop is used that always evaluates to
True
.
- In Python, a conditional loop is used that always evaluates to
Conditional Selection
-
- Scratch has two selection blocks that can be used. If multiple conditions are required, they need to be nested within each other.
-
- Python has three selection statements:
if
,elif
, andelse
. Again colons (:
) and indentation are needed.
- Python has three selection statements:
Testing for Equality
-
- In Scratch, you can use the equal sign (
=
) to test if one value is the same as another value.
- In Scratch, you can use the equal sign (
-
- In Python, a single equal sign is reserved for variable assignment, so a double equal sign (
==
) is used to test for equality.
- In Python, a single equal sign is reserved for variable assignment, so a double equal sign (
Lists in Scratch and Python
-
- Scratch lists are made in much the same way that variables are made.
-
- In Python, you use square brackets (
[]
) when creating a list, with commas between each pair of items.
- In Python, you use square brackets (
-
- You can add to a list in both Scratch and Python.
-
- You can also remove items from lists in both languages. In Scratch the first item in a list is at position
1
. In Python, however, the first item in a list is at position0
. That’s because in Python, you always start counting from0
.
- You can also remove items from lists in both languages. In Scratch the first item in a list is at position
Randomness
-
- Scratch has a
random
block that can be used to generate random numbers.
- Scratch has a
-
- In Python, you need to import the
random
module.
- In Python, you need to import the
-
- Both languages can also select random items from a list:
Concatenation in Scratch and Python
-
- Join strings together in Scratch using the
join
block.
- Join strings together in Scratch using the
-
- In Python, you can use the addition operator (
+
) to join strings.
- In Python, you can use the addition operator (
Indexing
-
- In both languages, you can find an item in a list or string using the item index.
Input
-
- You can collect user input in Scratch by using the
ask
block.
- You can collect user input in Scratch by using the
-
- In Python you use the
input()
function.
- In Python you use the
Type Casting in Python
-
- In Scratch, strings and integers are intelligently detected. In Python, it is necessary to type cast when converting from one to another. For instance, you can change a string to an integer and back again as follows:
number = 6
number_as_string = str(6)
number_as_integer = int(number_as_string)
Python to Scratch Challenge
Have a look at the Python code below, then see if you can write the same program in Scratch.
name = input("What is your name?")
print("Hello " + name + ". It is nice to meet you")
age = input("How old are you?")
age = int(age)
print("You were born in "+ str(2017 - age))
Which parts of the program were easier to create in Python, and which parts were easier to create in Scratch?
Challenge
Have a look at the Scratch code below. Can you recreate it in Python?
Sharing Python Code
If you are using Python that has been installed on your computer, then the best way to share your code with others is through a sharing service. The one that most software developers use for simple code snippets is GitHub Gists. You can create an account, and then have as many private or public gists as you like. An even simpler solution is to use a pasting service such as Pastebin.
Scratch to Python: Moving from Block- to Text-based Programming
![](https://ugc.futurelearn.com/uploads/images/da/de/promo_small_dade0a32-926a-4b48-91a3-ebcba5c348c4.jpg)
Scratch to Python: Moving from Block- to Text-based Programming
![](https://ugc.futurelearn.com/uploads/images/da/de/promo_small_dade0a32-926a-4b48-91a3-ebcba5c348c4.jpg)
Reach your personal and professional goals
Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.
Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free