Skip main navigation

Writing Your First Python Program

If you have already had a go at translating a Python program into a Scratch program, it’s now time to push yourself a little and see if you can turn a Scratch program into a Python program.

Picking unique usernames for websites and other software is never easy, so why not automate the process with a random username generator?

If you have already had a go at translating a Python program into a Scratch program, it’s now time to push yourself a little and see if you can turn a Scratch program into a Python program.

Using IDLE

I recommend that you use IDLE for this exercise rather than using Trinket.

Find IDLE in your application menu, and open it. The window that opens is called the shell, or the interpreter or sometimes repl. It doesn’t really matter what you call it, just be aware that on different websites and tutorial videos, it could be called any of these things.

The shell is used to write code that runs straight away. This is useful if you just want to test out a line of code or find out the value of some variable. You can have a go at writing a few lines in the shell now. Try the following:

>>> 2 + 2
4
>>> print('Hello')
Hello
>>> x = 100
>>> x
100

Python Files

When writing programs, you don’t write your code in the shell. Instead you use a file. Click on File and then New File in the menu bar to create a new Python file.

 

You’ll want to save this file straight away, since in IDLE you always need to save before running any code. Click on File and then Save, and call your program username_generator.py.

 

Let’s see how you write and run code in IDLE. In your username_generator.py file, write the following two lines of code:

 

print('No 3')
print('The Larch')

 

It doesn’t really matter what you use inside the print statement, so you can write any strings you like.

 

To run this code, you need to save the file again. Either click on File and then Save or alternatively hold down the Ctrl key and then press the s key on your keyboard.

 

To actually run the code, click on Run and then Run Module, or alternatively press the F5 key on your keyboard. (You might need to hold down the Fn key as well if your function keys are used for controlling volume and screen brightness.)

 

You should now see the results of the program as an output in the shell.

 

No 3
The Larch

 

Your First Python Program

 

Translating Scratch code into Python code helps learners avoid having to think about the logic of their program, and instead has them focus on the syntax of Python, so it’s a great way of introducing the language.

 

In this exercise, you’re going to make a username generator, that will combine random words and numbers together to make up unique usernames.

 

Let’s have a look at one version of the completed code for the username generator in Scratch, along with some of the data structures that are used.

 

Have a play with the program here.

 

And here is an image showing the data structures used and the main script:

 

2-3-2
2-3-3

 

Your task is to turn this into a Python script.

 

 

    • The first thing you will need to do is to create a couple of lists containing adjectives and nouns. You’ll need to type in the words yourself for now. Here’s some lists of adjectives and nouns that you can select from if you want.

 

 

 

 

    • Then you need to create two variables with values chosen at random from each of the lists.

 

 

 

 

    • Next you want to create two variables with values that are random numbers between 0 and 9. Here’s how this can be achieved for values between 1 and 10.

 

 

 

 

    • To finish off, concatenate (join) all the variables together.

 

 

 

There are a couple of small tweaks you will want to make.

 

 

    • Firstly, it is necessary to change the numbers that are picked randomly to strings. This is because Python can only join a string to another string, and not to a number. We’ll cover this in more detail later, but for now you need to know that to change a number to a string you can do the following:

 

 

my_num = str(6)

 

 

    • Secondly, there is no need to use two lines of Python code to import the code you need. You can simply write the following

 

 

from random import choice, randint

 

 

  • Have a go at writing your username_generator.py code, and run it using F5 when you are done.

You can see the full username generator project here. You can try out your new skills in a free Code Club – find out more at codeclubworld.org.

The screencast and code blocks are from from Scratch 2. Some of you may be using the newer version of Scratch that was released in January, Scratch 3. We’ll be updating this screencast to feature the latest version of Scratch shortly.

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