Skip main navigation

Scratch and Python Basics

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.
Scratch And Python Syntax

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.

 

 

1-6-1.png

 

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.

 

 

1-6-2.png

 

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.

 

    • Again in Python, you need to use single or double quotes if you are printing strings.

 

 

1-6-3.png

 

Conditional Loops in Scratch and Python

 

 

    • Scratch’s conditional loop repeats until a certain statement is True.

 

    • Python’s conditional loop repeats as long as a certain statement is True.

 

    • There needs to be a colon (:) at the end of the statement in Python.

 

    • 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.

 

 

1-6-4.png

 

 

    • 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 to 10.

 

 

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.

 

 

1-6-5.png

 

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, and else. Again colons (:) and indentation are needed.

 

 

1-6-6.png

 

1-6-7.png

 

1-6-8.png

 

Testing for Equality

 

 

    • In Scratch, you can use the equal sign (=) to test if one value is the same as another value.

 

    • In Python, a single equal sign is reserved for variable assignment, so a double equal sign (==) is used to test for equality.

 

 

1-6-9.png

 

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.

 

 

1-6-18.png

 

 

    • You can add to a list in both Scratch and Python.

 

 

1-6-11.png

 

 

    • 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 position 0. That’s because in Python, you always start counting from 0.

 

 

1-6-12.png

 

Randomness

 

 

    • Scratch has a random block that can be used to generate random numbers.

 

    • In Python, you need to import the random module.

 

 

1-6-13.png

 

 

    • Both languages can also select random items from a list:

 

 

1-6-14.png

 

Concatenation in Scratch and Python

 

 

    • Join strings together in Scratch using the join block.

 

    • In Python, you can use the addition operator (+) to join strings.

 

 

1-6-15.png

 

Indexing

 

 

    • In both languages, you can find an item in a list or string using the item index.

 

 

1-6-16.png

 

Input

 

 

    • You can collect user input in Scratch by using the ask block.

 

    • In Python you use the input() function.

 

 

1-6-17.png

 

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?

1-6-19.png

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.

This article is from the free online

Scratch to Python: Moving from Block- to Text-based Programming

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education