Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Scratch to Python: Moving from Block- to Text-based Programming. Join the course to learn more.

Skip to 0 minutes and 3 seconds Up until now, the Python programs that we’ve been writing have been mostly toy programs and games. Python can also be used to solve real world problems. How would you go about creating a program that you can use in your classroom to pick a student’s name at random? If we break this problem down, you need to enter names as a string and convert the string to a list. And then second, choose a random name from that list and output it. Let’s take a look at two functions which will help you do this. So firstly, to convert a string to a list, you can use the split function.

Skip to 0 minutes and 43 seconds So in this example, I have a string of vowels that each letter is separated by a comma. Now I can use the split function to create a list of these vowels. So I’m going to say vowels, I’m going to say split, and I’m going to tell it I wish to split this string up by commas. So for every comma, I should get a different element in my list. Let’s just print my list to the screen so we can see what that looks like. So again, if I run my program, what we end up with is a list and each element in the list is a separate vowel and it has indeed split it by a comma.

Skip to 1 minute and 27 seconds So to get a random item from a list, you can use the choice function.

Skip to 1 minute and 34 seconds So in this example, I have a list of vowels. Now the choice function is part of the random modules. I need to import it. So I’m going to say, from random import choice. And it’s best practise to put all your inputs at the start of your program. So now I have my choice function. I can say, OK, well, the vowel I want, use choice to pick a random one from my list. And again, if I print that to the screen so we can see what’s happening– now if I run my program, I get “o” out. And then hopefully if I run it again, I should get a different value and get “a”.

Skip to 2 minutes and 18 seconds Now you have these commands at your disposal. Your challenge is to write a program that will pick a random name from a string separated by commas. The article below also contains a list of student names and their test scores. Can you write a second program that sorts their scores from highest to lowest? Share your solutions in the comments section below. Let us know if you get stuck and don’t forget to help each other out. Good luck.

Programs for class

Up until now, the Python scripts that we have been writing have been mostly toy programs and games. It is important to realise that Python can be used to solve real-world problems as well. This is why Python has become one of the most popular programming languages in the world. In this section, you’re going to create some useful Python scripts of your own.

Look at this list of popular websites. You’ll notice that the top three sites all use Python (along with other languages).

That’s right — Python can be used to produce scripts and applications that are actually useful in your everyday life. At the Raspberry Pi Foundation, many of us write little scripts that help us out with our work and lives. For instance, we have a little program we use to fetch pictures of the car park via our internal social network, so that we can see how many parking spaces remain.

One thing that programmers love, more than almost anything else, is efficiency. There is a long-standing saying amongst programmers that “you don’t reinvent the wheel”. What this means is that there is no point in writing code to solve a problem if another programmer has already solved it. Some problems such as finding the square root of a number or picking a random number are so common that the code to solve them is part of the language’s standard library. This means it’s easy to import it into your program and use it right away. To use other not so common solutions to problems, such as drawing graphs or interacting with Twitter, you need to download modules before including them in your programs. (We’ll be looking at how to do that in Week 4!)

Let’s have a look at a couple of these functions and modules, and apply them to real-world problems that an educator might have.

Pick a random name

Many educators have their own way of choosing names of learners from their classes in a random fashion. Some may use lollipop sticks in a jar, while others might use a specialised app. There’s nothing better than solving this kind of task with your own little custom app though.

Let’s start with a simple description of the solution:

  1. Enter names as a string, separated by commas
  2. Convert the string to a list of names
  3. Choose a random name from the list
  4. Output the random name

There are a couple of problems here that you might not have encountered before. Let’s tackle them one by one.

Convert a string to a list

This is such a common problem that the solution has been built into the Python programming language. Let’s have a look at how it works:

vowels = "a e i o u"
list_of_vowels = vowels.split()
print(list_of_vowels)

The .split() is a built-in Python method which will split any string into a list, and defaults to splitting where there is a space. You can pass in other characters for it to use though.

vowels = "a,e,i,o,u"
list_of_vowels = vowels.split(',')
print(list_of_vowels)

Picking random items from a list

Although this is quite a common task, the commands for dealing with randomness are not baked into Python. They are part of the standard library, though. This means that you don’t have to download the code, but it does need to be imported into your program so you can access it.

from random import choice

my_list = ['a', 'e', 'i', 'o', 'u']
my_vowel = choice(my_list)
print(my_vowel)

When importing libraries, always place the import lines at the top of you code.

Coding the solution

Now you have these commands at your disposal, can you write a program that will pick a random student’s name from a string of student names separated by commas, such as the one below?

names = 'Alice,Bob,Carol,Chuck,Craig,Dan,Erin,Eve,Fay,Frank,Grace,Heidi,Judy,Mallory,Olivia,Oscar,Peggy,Sybil,Trent,Trudy,Victor,Walter'

Challenge

Another common task for educators is to deal with test scores. Imagine you have a long string with a student’s test score, followed by their name. Can you write a program that can sort test scores, lowest to highest and/or highest to lowest?

Here’s some example data you can use:

'54 - Alice,35 - Bob,27 - Carol,27 - Chuck,05 - Craig,30 - Dan,27 - Erin,77 - Eve,14 - Fay,20 - Frank,48 - Grace,61 - Heidi,03 - Judy,28 - Mallory,05 - Olivia,44 - Oscar,34 - Peggy,30 - Sybil,82 - Trent,75 - Trudy,92 - Victor,37 - Walter'

There’s going to be no specific help here, but one thing text-based programmers rapidly learn is to use the internet to find solutions to problems — here are a couple of links to StackOverflow entries that might help you out.

Sort a list of strings

Reverse a list

Share your solutions using a link from Pastebin. Remember that you can also ask for help in the comments section if you need it — as well as helping each other out if you can.

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation