data types

Lists in Python

Using single numbers or data points will only get us so far. If we want to analyse data sets or do anything with more than one number, it would be very helpful to have tools that grouped numbers. Fortunately, Python gives us several of these which will help to solve different problems. This article looks at one of these tools – ‘Lists’. We will learn what a list is, how we can use them in functions, and methods that help us to get more use out of them.

Lists are our simplest way to group numbers. Surrounded by [square brackets], they are simply a list of values – whether that is numbers, text, variables or even another list. Let’s put our ‘goals for’ and ‘goals against’ from our team’s first 9 games into a list:

In [1]:
GF = [0,0,3,2,1,5,3,1,2]
GA = [2,0,2,2,3,2,3,2,4]

[0, 0, 3, 2, 1, 5, 3, 1, 2]
[2, 0, 2, 2, 3, 2, 3, 2, 4]

Accessing numbers in lists is easy. Let’s take the numbers from our first game.

We do this by calling the list name, and adding square brackets with a number on the end. We will get the value held in the positon of the number.

This order starts at 0 – so for the first game, we will put 0. The second, we put 1, the third, we put 2 and so on.

In [2]:

Looks like we lost 2-0. Let’s make sure:

In [3]:
GF[0] - GA[0]

Yup, a loss and -2 goal difference.

Let’s iterate through this list to get our goal difference and also our points for the season so far.

In [4]:
#Define variables for points and goal difference
#Set goals for and against

P = 0
GD = 0
GF = [0,0,3,2,1,5,3,1,2]
GA = [2,0,2,2,3,2,3,2,4]

#We have 9 games, so we iterate with a range of 9 in a for loop
for i in range(9):
    #If we score more, give us 3 points and add the goal difference
    if GF[i] > GA[i]:
        P += 3
        GD += (GF[i] - GA[i])
    #If we draw, give us a point. Goal difference will stay the same.
    elif GF[i] == GA[i]:
        P += 1
    #If we lose, we get no points, but update the goal difference
        GD += (GF[i] - GA[i])



In those 9 games, we received 9 points and had a -3 GD. We can change the lists to be any length, and change the number in the ‘range’ to calculate this over any number of games!

‘Range’ is a function that generates a series of numbers between two points. By default, it starts at 0 and goes to whatever number we give it. Let’s take a look at some examples:

In [5]:
#One argument in range()
#Give me the list of 10 numbers, starting at 0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [6]:
#Two arguments in range()
#Give me all numbers between these two
[5, 6, 7, 8, 9]
In [7]:
#Three arguments in range()
#Give me all numbers between the first two, in steps of the third number
[0, 2, 4, 6, 8]

We then used this range as our counter for how many times we used the for loop. Keep this in mind for future loops!

Selecting a number from a list

Selecting a number from a list is easy. Just pop the index (the location of the number) in square brackets after the list name.

Select multiple with a colon between two numbers – including the first of them, but not the last.

In [8]:
Players = ["Didek","Bascin","Smacer","Boras","Finnon"]

#Select the 4th player - remember that the count starts at 0!

#Select the middle 3 players (Bascin, Smacer and Boras)
['Bascin', 'Smacer', 'Boras']

List Methods

A method is a function that belongs to a data type. It is used to manipulate or utilise this data type.

For lists, this might involve adding something to the end, alphaetising our list or finding the location of something within it.

A method is used by calling the name of the variable, then the method and its arguments – you can find a few below:

In [9]:
GF = [0,0,3,2,1,5,3,1,2]
GA = [2,0,2,2,3,2,3,2,4]

#Let's add a new score to our goals for and goals against lists
#The append() method will help us here


#Show the scores, 3-1 should be the final column
[0, 0, 3, 2, 1, 5, 3, 1, 2, 3]
[2, 0, 2, 2, 3, 2, 3, 2, 4, 1]
In [10]:
GF = [0, 0, 3, 2, 1, 5, 3, 1, 2, 3]
GA = [2, 0, 2, 2, 3, 2, 3, 2, 4, 1]

#I want to reverse the scores, to see the 5 most recent ones
#Reverse() will make this light work

[3, 2, 1, 3, 5]
[1, 4, 2, 3, 2]

There are plenty more methods for you to learn about on your journey learning Python – I’m sure you’ll find lots more on the pages here, but a quick Google will take you to many more.


Lists are a simple way to store lots of numbers under one variable. This is very useful for keeping records of things, or for running formulas that take more than one number.

We have learned that they are created with square brackets, can be navigated to find individual datapoints or ‘slices’ of then.

We also learned about methods – functions that act on a particular class. For lists, we saw them easily add more data points, or reverse the order of them.

There are more ways to store multiple data points – take a read on ‘tuples’ and ‘dictionaries’ to learn more!

Posted by FCPythonADMIN in Python Basics, 0 comments

Numbers, Strings and Variables in Python

Football is full of numbers, names, lists, teams and a million other ‘things’ that make up our understanding of what is happening.

In the same way, Python has lots of different ways of classifying things that it can understand. For example, it uses numbers to count, ‘strings’ for names and even has ways to group these things – like we would need for a league. This introductory post takes a look at a couple of these data types.


As its simplest, Python is a calculator:

In [1]:
In [2]:
In [3]:
In [4]:
# ** = to the power of
In [5]:


While a calculator is incredibly useful, we can give these numbers a name and a placeholder so that they are a bit more tangible and applicable to a problem. For example, if I want to keep track of our top scorer’s shots and goals:

In [6]:
Ronney_Goals = 15
Ronney_Shots = 63

I could now calculate Ronney’s conversion rate! Dividing goals by shots, I can see how many shots it takes him to score.

In [7]:
Ronney_Conversion = Ronney_Goals/Ronney_Shots


Now that Python has some interesting information on Ronney, it wants to share it with the world. As we can see above, sharing a number by itself doesn’t tell us a great deal. However, a string of text can give us a bit of context.

Surround a piece of text in quotation marks (be consistent with single or double quotes) to create a string:

In [8]:
"Ronney is truly a great player, his conversion rate speaks for itself."
'Ronney is truly a great player, his conversion rate speaks for itself.'

…and let’s use the print() and str() commands to add the evidence to our commentator’s opinion.

In [9]:
print("His conversion rate of " + str(Ronney_Conversion) + " is sublime")
His conversion rate of 0.23809523809523808 is sublime

That is a bit specific, Merse, but I appreciate the information! Let’s take a look at those two commands:

print() – Python, please print everything that I put into the brackets. I might break up what I want to print with the ‘+’ symbol.

str() – You can’t print numbers? OK, then please consider this number as a string.


In this introductory post, we have seen that Python can be used as a calculator at its very simplest. However, we have seen that we can make it a bit more useful with variables, even using a variable to help our commentator give a great bit of insight, combining it with a string.

We even saw a couple of commands with print() and str().

Next up, take a look at how we compare and evaluate this information in Python to make decisions with our code.

Posted by FCPythonADMIN in Python Basics, 0 comments