3.6

## Raspberry Pi Foundation

Skip to 0 minutes and 1 secondUp until now, the Python scripts that we've been writing have been mostly toy programmes and games. It's 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. Look at this list of popular web sites and you'll notice the top three sites all use Python, along with some other languages. Python can be used to produce scripts and applications that can actually be useful. At the Raspberry Pi Foundation, many of us write little scripts that helps out with our working lives.

Skip to 0 minutes and 32 secondsFor instance, we have a little programme used to send us pictures of the car park over our internal social network when prompted, so that we can see how many car parking spaces remain. Now, 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's no point in writing code to solve a problem if another programmer has already solved it. Some problems, such as finding a 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, which means it is easy to import into your programme.

Skip to 1 minute and 12 secondsOther not so common solutions to problems, such as drawing graphs or interacting with Twitter, will require you to download the modules if you want to use them in your programmes. So 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.

Skip to 1 minute and 32 secondsMany 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. You can start with the description of the solution to the problem. We want to enter names as a string separated by commas, convert the string to a list of names, then choose a name from that list. And finally, we want to output that name. Converting a string to a list is such a common problem that the solution has been built into the Python programming language. Let's have a look at how this works.

Skip to 2 minutes and 11 secondsI can create a list vowels for instance-- A, E, I, O, U-- all separated by a space. If I run my programme, I can now query that in the interpreter and see the string of vowels. To convert this into a list, I'm going to create a new variable called list_of_vowels. I'm going to set that to be vowels and then use the dot split syntax, which will split my vowels up into individual characters. Now when I query list_of_vowels, I can see a list of vowels being produced for me. However, if my vowels were separated by commas, as opposed to spaces-- so I'm going to edit my vowel string here-- the dot split syntax will no longer work on the string.

Skip to 3 minutes and 1 secondIf I run this programme again, and then query what list_of_vowels is now, I can see that it's just a single item in a list. So what I need to do is within the dot split, I need to say that I'm going to split the string at a comma. Now then, I can print a list of vowels at the end of my programme. And when I run this, my list of vowels will be produced in the interpreter. The second part is picking random items from a list. Now although this is a common task, the commands for dealing with randomness are not baked into the language itself. They are what's called part of the standard library.

Skip to 3 minutes and 40 secondsThis means that you don't have to download the code, but it does need to be imported to get access to it in your programme. So here's my list of vowels-- A, E, I, O, U-- separated by commas. Now to pick a random item from this list, I'm going to need to use the random module. So I type from random, then the function I want to use is called choice-- so from random import choice Now, I can create a new variable, which I'll call my_vowel, and I'm going to set it to choice then my_list. And then, finish it off by printing out what the value of my_vowel is.

Skip to 4 minutes and 22 secondsNow each time I run this programme, a new vowel will be chosen from the list, and output in the interpreter.

Skip to 4 minutes and 33 secondsNow that you have these commands at your disposal, can you make a programme that will pick a random student's name from a string of students names, each separated by a comma, such as the one in the text below. As a further 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 programme that can sort the test scores lowest to highest and or highest to lowest? There's an example of the data in the text below.

Skip to 5 minutes and 5 secondsThere's going to be no specific help here, but one thing text-based programmers rapidly learn to do is to use the internet to find solutions to problems. So have a look at a couple of Stack Overflow questions that might help you out. always, share your solutions by linking in the comments below.

# Scripts 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.

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