Python and Scratch – GCSE syllabus part 1

My intention in this post is to take a look at both Python and Scratch, checking them against the OCR GCSE Computing syllabus.  This should then act as a reference when introducing the topics in Scratch and then moving each into the realm of Python.

Programming constructs: sequence, conditionals and iteration plus algorithm understanding: IF and CASE statements, FOR, WHILE and REPEAT loops

control loop blocks in Scratch - repeat x times, repeat until, forever etc

control loop blocks in Scratch

In Scratch: most of these blocks are clear to use.  I’ve included the STOP SCRIPT block in here as it seems to be the equivalent of the BREAK statement, stopping execution of the code block.

Each of these blocks is C shaped, making it straightforward to see what code needs to be executed within the loop.  The student still needs to keep a careful eye on them, however – a series of IF statements can cause confusion, as there is a tendency not to notice whether they are nested or stacked.

The IF/ELSE block provides another useful structure, but this needs to be used carefully as again confusion can be caused if nesting multiple IFs.

code for control loops in python

Python control loops

In Python each piece of code needs to be typed, and the block is defined by very careful use of indenting.

The IF/ELIF/ELSE in Python provides a neater way to produce a series of IF statements, and serves in place of the SWITCH CASE statements of other languages and in the pseudocode used for algorithm description, where the CASE statement is a list of options, and the instructions for each option, although in CASE the choice is usually made on a variable value rather than a logical test , if I remember correctly (SWITCH variable CASE “y” do this, CASE “z” do that, CASE “n” do something else).  In Scratch, this would have to be done with a series of IF/ELSE IF/ELSE statements and tests, where as usual they would have to be placed very carefully.

The important thing to notice in Python layout is that there are no brackets around the logical tests and no formal line ending such as the semicolon in Java, but the start of the block is shown by a colon :

The editor should then indent the start of the next line automatically.  Each line that is indented forms part of the loop, and the non-indented code (or code back to the next level of indenting) will not be executed until the loop is completed.  You do usually need to delete the whitespace yourself to end the code block (one exception to this is the block to define a function – a RETURN line will automatically end the block and reduce the indentation).

While it is not essential to use whitespace this way in other languages, following this sort of layout carefully does lead to far more understandable code, and so it is a very good habit to get into for whichever language you intend to move onto.  Many editors provide support in laying out code neatly, and it may be possible, depending on the editor, to collapse sections of code for easier viewing.

The colour coding of the editor helps in forming good code as well: the secret to maintaining the colour coding after saving the file with the IDLE is to save it with the extension .py – thanks to the reader who reminded me of that one.  While the IDLE will save and recognise the file as Python, it doesn’t actually save it automatically with the right extension!

Notepad++ is another editor which will quite happily save and colour code Python files, incidentally, and will also cope with other languages, such as Java and HTML – again, the careful use of whitespace in these languages leads to better understanding of the code.

python for loop examples

Python FOR loop examples

FOR loops become very interesting in Python, because they provide the opportunity to do more than cycle through a series of numbers.  You specify a range to step through, and the loop will run for each step within the range, but the range may be a number as in a standard for loop (Scratch’s equivalent would be the repeat loop) or it may be items in a list, for example.

In this example code, the results produce the numbers 0-9 (range starts from 0 if not specified, and stops at the value before the end of the range), the numbers 1-9 and then the individual letters in the string “Hello World”.

In Scratch, the equivalent block would be REPEAT XXX, where XXX is the number of times you want the loop carried out.

Python also proves more powerful than Scratch as well because the value of the number of the loop can be used in the body of the program instead of creating another variable, as in these examples where I print out the number of the current step.

The WHILE loop in Python is the equivalent of the REPEAT UNTIL block in Scratch, although the WHILE loop will continue as long as the test is true, while the REPEAT UNTIL will continue until the test is false.  The FOREVER IF loop in Scratch I am not so sure about, as it appears a little confusing – it is really a FOREVER loop with an IF statement inside it, rather than a WHILE block which will finish as soon as the test condition is false.

Scratch logic blocks - greater than less than, AND, OR, NOT etc

Scratch logic blocks

So that’s a look at the basic looping and decision making structures in Python and Scratch.  As to the contents of the conditionals, these both follow the standard content:

In Python equal to becomes == the double equals sign, as a single equals = is used for assignment – when I forgot this, the editor seemed to pick it up and flag it as an error, which is useful.

Also in Python, True and False both start with a capital letter and NOT becomes !, as in x!=4 (x is not equal to 4).  In both languages, conditional statements can be nested as necessary.

Still to look at: data types, input/output, file handling systems, basic string manipulation, one dimensional arrays.variables, operators and assignments.


About emmyleigh
Writer/editor/proofreader who loves technology

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: