Skip main navigation

Useful Python Scripts

Python Scripts are not limited solely for use in 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.

Python Scripts are not limited solely for use in 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.

Python Script Exercise

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 your 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'

 

Python String 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

This article is from the free online

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

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education