Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

Scratch and Python Syntax

Explore common concepts in block- and text-based programming.

One aspect of text-based languages that many learners struggle with is understanding the specific syntax (the rules of the language) required. Mistakes in a program are often due to not following these rules, and these mistakes are called syntax errors. Therefore, it is helpful to show the parallels and differences between a language that a learner has already mastered and the one they are trying to learn.

In the next two steps I’ll show you 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 when a value is assigned to it.
    • In Python, it is necessary to surround strings (any text) with either single (') or double (") quotes.




score = 0
welcome = 'Welcome to the game'


Increment Variable



    • In Scratch, a variable’s value can be increased or decreased. To do this, you use a change block with a variable and the number to increment by, using a negative number to decrease the value.


    • To increase or decrease the value of a variable in Python, you must calculate the new value using the value stored in the variable, and assign this new value to the variable.




score = score + 1


Simple Output



    • In Scratch, you make a sprite talk to provide output to the user of the program.


    • Python uses print statements to output text, numbers, or symbols.


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




print('Your score is amazing!!!')


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.




while not score > 10:



    • The example above can be simplified in Python though. Using the while loop, it is easier to check that the variable is less than or equal to 10.



while score <= 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.




while True:
 lives = lives + 1


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.




if score > 10:
 print("Well done you are doing a great job!")



if score > 10:
 print ("Well done you are doing a great job!")
 print ("Keep going try to get to 10 points!")



if score > 10:
 print ("Well done you are doing a great job!")
elif score < 10:
 print ("Keep going try to get to 10 points!")
 print ("Yes! You have made it to 10 points!")


Testing for Equality



    • In Scratch, you can use a operator block with an 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.




score == 10




Have a look at the Python code below, then see if you can write the same program in Scratch using the blocks you have seen in this step.


score = 0
welcome = "Welcome to the game"
while score <= 20:
	score += 2
	print("You get 2 bonus points for being under 20!!!")
if score >= 20:
	print("Well done you have completed the game!")
	print("You need to get to 20 to complete the game")

Were there parts of the program which you thought were easier to create in Scratch?

Share your thoughts and Scratch programs by pasting the link to your program in the comments.

This article is from the free online

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

Created by
FutureLearn - Learning For Life

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.

Start Learning now