Introduction to Python - Objects, Lists & Loops

This notebook aims to provide an introduction to Python and a description of the basic data types and structures which are the basic building blocks of any Python code.

By the end of this notebook, you should have a better understanding of the differences between integer, float & string objects as well as being able to make lists and tuples.


So what is an object in Python? An object is really just a label to a specified piece of data within the computer - the object is really an interface to a piece of computer data.

There are three main types of object which are regularly used when coding with Python, and these are:

  1. Integers, for example: 1, 5, 607694
  2. Floats, which are just another word for decimal numbers: 4.45, 3.14159, 5.0
  3. Strings, which is a collection of characters: 'hello', 'cat', 'dog', '44 friends'

Strings can be used in the widest range of circumstances and so are very useful descriptors. A string can also be contained with 'single quotes' or "double quotes". It doesn't really matter which you use, but it's good to be consistent. String can also be used across multiple lines in the following ways:

'''This string is contained
across multiple lines'''

"""This double quoted string is also
contained across multiple lines"""

We'll investigate what can be done with these objects now, using some examples below. In order to get any code to run in the boxes below, type in the code you'd like to run and then press Shift + Enter to execute it.

Looking first at integer numbers, we can do a lot of useful mathematical calculations with them. Run the following code snippet below to execute some examples:

Additionally, the print() statement has been introduced here, this allows the results of any calculations to be outputted to the console when run.

The variables a & b have been saved to this session now, and below if you were to type print(a) for example, the result would still be 4. Use the below box to practise any calculations of integers and print statements, should you wish.

Now we can do similar operations to above, but this time with a mixture of integers and floats:

Finally, we can move onto strings. A string was actually already used in the final print statement above, it just wasn't assigned to a variable like the floats were. We can define a string in a similar way to how we defined the integers and floats above. You can do a number of useful things to strings, especially if you have a long string which you'd like to split up into smaller strings.

However, you cannot do mathematical operations with strings unless you convert them to integer first, see the error message below:

It is relatively easy to convert from one type of object to another, providing it is possible. The following snippets of code can be used to convert from between the different types of object: int() - converts to an integer. float() - converts to a float. str() - converts to a string.

One thing to note though, is the way the conversion to integers occurs. In a conversion to an integer, the number will always round down, even if traditionally it should be rounded up. For example, if you were to run, int(4.2) and int(4.8), the result for both would be 4.

One way to avoid this is to use the round() function, which rounds a number to whichever order of significance you want. Shown below are some comparisons of the round as opposed to the int function.


Lists, and arrays, are very commonly used structures in Python (although as you shall see later on, there are many third party modules which extend and enhance the basic list concept) and it is important to understand how to use them when coding in Python.

A list can be either a series of numbers, strings, variables, other lists or any combination of the above. For most lists it is easier to have all elements within the list being the same type of variable, in terms of accessing elements within the list, especially if it is done iteratively in the case of long lists. In the box below, there are some examples of various types of list.

These lists can be indexed and sliced in Python, which means selecting just the data you want to use at any given point.

This is done by requesting just the index value you want from the list. In Python, the first value of a list is the 0th term, the second is the 1st term, the third is the 2nd term and so on. This is easier to explain with an example, so if you take the int_list above, and type in int_list[0], you will return the value 1, the first value in the list.

Try it below:

If you have a long list, you can also easily request the last value in the list by searching for the -1 term, for example int_list[-1] will return 5. This is because the python list structure is cyclical, and so if you go back one term from the first value in the list you get the last term. Similarly, int_list[-2] will return 4.

It is also possible to obtain just a subsection of a list, by putting a colon between two values. For example, the command float_list[2:4] will return [3.0,4.0] as it is returning everything after the third value but before the fifth value in the list (as 0 is the first index value as stated above). It is also possible to slice from just one end of the list, using the colon and only one number, for example float_list[2:] which returns [3.0,4.0,5.0]. Try out slicing the example lists below:

If you tried slicing mixed_list above you might have noticed that mixed_list[3] returned the list [5.0,6.0]. It is often useful (and indeed necessary as will be seen later when looking at GIS data & satellite images) to contain lists within other lists. To access an individual value within the inner list in mixed_list, you slice once again, and so the command mixed_list[3][0] will return the value 5.0.


Another very important concept not just in Python but in coding in general is the loop. A loop can be used to iterate through a list and perform the same operation on each value in the list.

The most commonly used loop in Python is the for loop, which has defined start and end points as well as a defined step size. To give an example:

for i in range(0,5,1):

In this example, the for loop will iterate over a range of numbers, from 0 to 4 (remembering the Python starts numbering from 0) in step sizes of 1. i is the iterable, and so we are printing the iteration at each value. Execute the below code and see that this is true. In truth, it is easier to simply write range(5): as the default is to iterate in steps of one and start at zero.

The iterable print out (i) is not particularly useful by itself. However, using the slicing knowledge you've just acquired, you can use for loops to iterate through printing each value of a list individually, by changing the slice from a specific known value (such as str_list[0]) to a changing value based on the loop iteration (such as str_list[i]). Try printing the values of str_list in a for loop below.

In some instances, we don't know the length of the list we are iterating through, so instead of counting through the length of the list manually, we can find out the length of the list in Python and then choose to iterate over it all.

This is done using the len() command. For example, len(str_list) will return the value 5. This could then be put into a for loop to iterate over it in the following way (executing the below function should yield a similar result to previously):

However, this looks a little clunky and there are cleaner and more efficient ways to iterate through a list. For example, the enumerate function effectively performs the role of both range(len()) and the print() function in the above example. Try the following example to see the enumerate function in action.

As you have seen in the enumerate function above, the actual value in the list str_list is saved to the variable val each iteration and so this can be used directly each iteration.

The for loop is a very useful and versatile tool. However, it can be a little slow in the case of a very long list or if a simple change is being made to a list. In these instances, it makes more sense to use a list comprehension, which is very similar to a for loop. They have the following form:

[print(i) for i in str_list]

This list comprehension will return exactly that found in the for loop printed above. This can also perform useful operations, such as the square of each value in a list. This is shown by running the example below:

In the above instance, using i**2 performs the square of each value in the list, and a double asterisk at any point denotes raising a value to a power, with the 2 signifying we will be raising it to the second power, the square. Try making a list comprehension which can find the cube of each term in float_list below.

Joomla3 Appliance - Powered by TurnKey Linux