This lesson is being piloted (Beta version)

Introduction to using Python


Teaching: 15 min
Exercises: 40 min
  • What is Python?

  • How do I use the Python interpreter?

  • How do I use variables?

  • Explain the benefits of Python.

  • Assign values to variables.

  • Use properties and methods on variable ‘objects’.

  • Convert Python variables to other variable types.

Introduction to Python

See topic video lecture, and PowerPoint slides used with per-slide notes.

Getting Started with Python

Normally, you write Python programs in a Python script, which is basically a file of Python commands you can run (or within a Jupyter notebook, which is an interactive script of Python commands that gives you results of your commands as you enter them). We’ll come on to writing Python scripts later. But to start with, we’ll take a look at the Python interpreter. It’s similar to the shell in how it works, in that you type in commands and it gives you results back, but instead you use the Python language.

It’s a really quick and convenient way to get started with Python, particularly when learning about things like how to use variables, and it’s good for playing around with what you can do and quickly testing small things. But as you progress to more interesting and complex things you need to move over to writing proper Python scripts, which we’ll see later.

We first need to download the training materials from the GitHub code repository online in a Zip file and unpack its contents to our home directory.

Go to in a browser (any will do, although Firefox is already installed on the provided laptops). Select the green Code button, and then select Download ZIP, and then in Firefox selecting Save File at the dialogue prompt. This will download all the files within a single archive file. After it’s finished downloading, we need to extract all files from the archive. Find where the file has been downloaded to (on the provided laptops this is /home/dtcse/Downloads, then start a terminal. You can start a terminal by right-clicking on the desktop and selecting Open in Terminal. Assuming the file has downloaded to e.g. /home/dtcse/Downloads, type the following within the Terminal shell:

unzip /home/dtcse/Downloads/

As a reminder, the first cd command without any arguments changes our working directory to our home directory (on the provisioned laptops, this is /home/dtcse).

The second command uses the unzip program to unpack the archive in your home directory, within a subdirectory called software-engineering-day1-gh-pages. If you do ls to list the files in your home directory, you should see this new subdirectory (amongst other directories and possibly files):

software-engineering-day1-gh-pages  Documents  Music     Public  Templates
Desktop                             Downloads  Pictures  snap    Videos

Now the software-engineering-day1-gh-pages subdirectory is a little long to easily work with, so we’ll rename it to something shorter:

mv software-engineering-day1-gh-pages se-day1

So using Bash’s mv command, this directory is now known as se-day1:

Desktop    Downloads  Pictures  se-day1  Templates
Documents  Music      Public    snap     Videos

Next, change to the code directory within that new directory:

cd se-day1/code

Running the Python Interpreter

Next, start the Python interpreter from the shell with:


And then you are presented with something like:

Python 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

And lo and behold! You are presented with yet another prompt. So now, we’re actually running a Python interpreter from the shell - it’s only yet another program we can run from the shell after all. But note that shell commands won’t work again until we exit the interpreter. Whilst we’re in the Python interpreter, we can only use Python commands.

You can exit the interpreter and get back to the shell by typing:

>>> exit()

…or alternatively pressing the Control and D keys at the same time.

Using either method, you’ll see:


So now you’re back in the shell.


Let’s take a look at how variables are handled within the Python interpreter. Start the Python interpreter again from the shell with:


Compared to some other languages such as C++ or JavaScript, variables in Python do not need to be declared before assigning something to them, for example:

six = 2*3

Note that in terms of naming variables, Python’s variables must begin with a letter.


If we look for a variable that hasn’t ever been defined, we get an error telling us so:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'seven' is not defined

You can also assign an arbitrary number of variables on the same line:

one, two = 1, 2

Sorting out references

What does the following program print out?

first, second = 1, 2
third, fourth = second, first
print(third, fourth)


2 1

Although we commonly refer to variables even in Python (because it is the common terminology), we really mean names or identifiers. In Python, variables are name tags for values, not labelled boxes that contain a value.

So to better understand our labels and boxes: each box is a piece of space (an address) in computer memory. Each label (variable) is a reference to such a place.

When the number of labels on a box (“variables referencing an address”) gets down to zero, then the data in the box cannot be found any more.

After a while, the language’s “garbage collector” will wander by, notice a box with no labels, and throw the data away, making that box available for more data.

Older languages like C and Fortran don’t have garbage collectors. So a memory address with no references to it still takes up memory, and the computer can more easily run out.

So in Python, when I write:

number = 1
number = 2

The following things happen:

  1. A new integer object ‘1’ is created, and an address in memory is found for it.
  2. The variable “number” will refer to that address.
  3. A new integer object ‘2’ is created, and a different address in memory is found for it.
  4. The variable “number” is moved to refer to that different address.
  5. The old address, containing ‘1’, now has no labels.
  6. The garbage collector frees the memory at the old address.

Objects and Types

An object, like number, has a type. We can use type() to tell us the type of the variable. For our variable above:


Note we don’t need to use print - the Python interpreter will just output the result:

<class 'int'>

Depending on its type, an object can have different properties: data fields inside the object.

Consider a Python complex number for example, which Python supports natively:

z = 3+1j

We can see what properties and methods an object has available using the dir function:


You can see that there are several methods whose name starts and ends with __ (e.g. __init__): these are special methods that Python uses internally, and most of the time, we don’t need to concern ourselves with them. We will discuss some of them later on in this course as they become useful. The others (in this case, conjugate, img and real) are the methods and fields through which we can interact with this object.

<class 'complex'>

A property of an object is accessed with a dot. The jargon is that the “dot operator” is used to obtain a property of an object.

Other Basic Python Data Types

Since we’re not declaring the type of a variable, how does it work it out?

Python is an interpreted language that is dynamically typed, which means the type of a variable is determined and bound to the variable at runtime from its given value. So when we assign a floating point number, for example, it’s type is inferred:


weight_kg = 55
weight_lb = 2.2 * weight_kg
print('Weight in lb', weight_lb)

Note we can add as many things that we want to print by separating them with a comma.

For a float, a number after a point is optional. But the dot makes it a float.

Weight in lb 121.00000000000001

So the thing with floats is that they are representation of a real number. Representing a third or the root of 2 would be impossible for a computer, so these are really approximations of real numbers using an ubiquitous standard (IEEE-754).

As the example above shows, we can print several things at once by separating them with commas.

An important thing to remember, particularly in numerical analyses, is that a float in Python is double precision.

What’s inside the box?

Draw diagrams showing what variables refer to what values after each statement in the following program:

weight = 70.5
age = 35
weight = weight * 1.14
age = age + 20


Note that before, we also used a string in our use of print. In Python, we can use either single quotes or double quotes, or even both if we need to include quotes within a string, e.g.:

given = 'Joe'
middle = "Frederick"
family = "'Bloggs'"
full = given + " " + middle + " " + family

Here we use the + operator to concatenate strings together.

Joe Frederick 'Bloggs'

With quotes, the main thing is to be consistent in how you use them (i.e. not like we’ve used them above!).

We’ve looked at properties on objects. But many objects can also have methods (types of functions) associated with them, which we can use to perform operations on the object.

For strings, we also can do things like:


Which returns the upper case version of the string.


Note it isn’t changing given’s string itself, it’s returning a new string in uppercase.

There are other methods we can use on strings, such as:

'    Hello'.strip()

We’ll be looking at classes and objects in more detail later today.


We can use boolean variables to capture True or False, useful in conditionals and loops, e.g.:

is_joe = (given == 'Joe')
flag = False
print(is_joe, flag)
True False

No Value?

We can also assign variable with no value:

nothing = None

None is the special Python value for a no-value variable.

If that’s the output, what’s the type of nothing?

<class 'NoneType'>

Converting Between Types

With floats, ints and strings, we can use in-built functions to convert between types:

age, house_number = 30, '76'
print(str(age), float(age), int(house_number), float(house_number))
30 30.0 76 76.0

Key Points

  • Python is an interpreted, dynamically typed language.

  • Run the interpreter from the command line by typing python.

  • Use variable = value to assign a value to a variable in order to record it in memory.

  • Variables are created on demand whenever a value is assigned to them.

  • Use print(something) to display the value of something.

  • None as an empty variable value has its own type.

  • Convert a variable to another type by using new_type_name(variable).