Python Vs Scratch

This is the second post in a series for the Naas-Sallins Coder Dojo about using the Python programming language. What I want to talk about here is how a more traditional programming language like Python compares with Scratch, by having a quick look at what they have in common and whats different.

In Scratch we are used to the different coloured sections which contain the various commands.

scratchlibraryThese commands are grouped together by the type of function they perform so we can see that all the commands to move a sprite are in the motion section. In Python we have the concept of libraries which we can import into our program to allow us to do particular things. For example we might import urllib2 to enable us to open a webpage. Again don’t worry about the details of this now, we will cover this as we go along.

Now in Scratch we are used to dragging our various commands for a sprite onto our scripts tab and putting them together to get the desired output. In Python however we need to type each command into our editor and we need to understand that how we layout those commands will change how our program runs. In Python the layout of the code matters more than in most other traditional programming languages since Python uses indentation to group statements together. Let me show you what I mean.

In Scratch we might have scratchifblock so we can see that if our variable x is equal to one we will move ten steps and than the sprite will say “this is an if block in Scratch”. We can see that the orange if-then includes the two other statements.

To do something similar in a Python program we would do something like this

if x ==1:
    currentpos = currentpos + 1
    print "if block in Python"

So what is important here is not the syntax of the Python commands but the fact that the two lines we want to run if the value of x are equal to one must be indented so that Python knows that they are all part of the same condition that needs to be run when the value of x is one. Some other languages enclose all the commands to be run when a condition is true in {} brackets but in Python its how the lines of code are indented that controls this.

Okay so that’s enough talk, how about some real code. Lets make a program to select 6 lotto numbers from 1 to 45 inclusive. If we were doing this in scratch we might do something like

scratchlottoWhich would give us 6 random numbers but wouldn’t check if we had the same number twice.

Now to try this in Python we need to start our editor so we can type in our commands so search for and run pythonwin.exe. It is a free editor that comes with Activestate Python which allows us to access the help documentation,  as well as enter and run our code.

Okay so the first thing we are going to do is set up our pot of 45 balls to choose from, in this case we are going to use a list. A list in Python is pretty much the same as our list from Scratch so we enter the following.

balls = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]

Here we set out list called balls to all the numbers from 1 to 45 inclusive, note the use of the [ ] square brackets to denote a list. Now we need the code that will pick our 6 balls at random from the 45. Finding the right information can be tricky but the we can search for that information both in the help files and by using Google.

Click on Help-Python Manuals and enter random in the search tab, If you select the first result you should get all the details of the functions built into the random library. As you can see it is a bit complicated and as you gain more experience you will be able to understand this but for the moment trust me when I say (a bit of example code will make it clearer) that the bit we want is

random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.”

Step one is to tell Python that we want to use the random library by using the import command

import random

then we need to use the random.sample command in our code so our full program becomes

""" Lotto number generator for dojo"""
import random
balls = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]
picks = random.sample(balls,6)
print picks

Here we have

A comment describing the program

Our import statement so we can use the methods associated with random

Our setup of the pot by putting the numbers 1 to 45 in a list called balls

Our selection of a set of picks by getting a sample of 6 items at random from the list called balls

Our print statement which displays the 6 random items from the list.

When you run the code hopefully you should get something like [32, 3, 38, 12, 25, 5]

Again, don’t worry if you didn’t get everything/anything just let me know where you got stuck and we will give you a hand at the next dojo session.

4 thoughts on “Python Vs Scratch

Leave a Reply to damianmooney Cancel 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