Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Scratch to Python: Moving from Block- to Text-based Programming. Join the course to learn more.
Cartoon image of Scratch block and Python block

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.

Variable assignment

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


Simple output

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


Conditional loops

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


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

Infinite loops

  • Scratch has a specific type of loop that is infinite.
  • In Python, a conditional loop is used that always evaluates to True.


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.




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.



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


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


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



  • Scratch has a random block that can be used to generate random numbers.
  • In Python, you need to import the random module.


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



  • Join strings together in Scratch using the join block.
  • In Python, you can use the addition operator (+) to join strings.



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



  • You can collect user input in Scratch by using the ask block.
  • In Python you use the input() function.


Type casting

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

Cheat sheets for these comparisons are available at the bottom of this step in both digital and print formats.


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?


Have a look at the Scratch code below. Can you recreate it in Python?


A quick note on sharing

We love it when our learners share their code in the comments, either to get some constructive feedback or when helping others. However, the comments section is designed for commenting, not for displaying formatted code like this.

If you want to share your Scratch scripts, then you can share the link to your code. Similarly, if you are using Trinket in the first few weeks of the course, then just create links to the code you want to share.

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.

Share this article:

This article is from the free online course:

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

Raspberry Pi Foundation