Scratch and Python Basics (2024)

Home / / / Scratch to Python: Moving from Block- to Text-based Programming / Scratch and Python Basics

  • Scratch and Python Basics (1)

    Learn more about this course.

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 Basics (2)

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.

Want to keep learning?This content is taken from Raspberry Pi Foundation online course,Scratch to Python: Moving from Block- to Text-based ProgrammingView Course

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.
Want to keep learning?This content is taken from Raspberry Pi Foundation online course, Scratch to Python: Moving from Block- to Text-based ProgrammingView Course

Scratch and Python Basics (3)

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 Basics (4)

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.

Scratch and Python Basics (5)

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.

Scratch and Python Basics (6)

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

Scratch and Python Basics (7)

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.

Scratch and Python Basics (8)

Scratch and Python Basics (9)

Scratch and Python Basics (10)

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 and Python Basics (11)

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.

Scratch and Python Basics (12)

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

Scratch and Python Basics (13)

    • 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 and Python Basics (14)

Randomness

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

Scratch and Python Basics (15)

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

Scratch and Python Basics (16)

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.

Scratch and Python Basics (17)

Indexing

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

Scratch and Python Basics (18)

Input

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

Scratch and Python Basics (19)

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 = 6number_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.

Want to keep learning?This content is taken from Raspberry Pi Foundation online courseScratch to Python: Moving from Block- to Text-based ProgrammingView Course

See other articles from this course

This article is from the online course:

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

Scratch and Python Basics (20)

Created by

Scratch and Python Basics (21)

Join Now

This article is from the free online

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

Scratch and Python Basics (22)

Created by

Scratch and Python Basics (23)

Join Now

Scratch and Python Basics (24)

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

Register to receive updates

  • Create an account to receive our newsletter, course recommendations and promotions.

    Register for free
Scratch and Python Basics (2024)
Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 5952

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.