E001 - Getting Started

In this exercise, you learn about some basic concepts of programming, commonly used tools, and how to write and execute a script. You may come across some important vocabulary that you are not yet familiar with. We will explore this with the Python programming language.

Information

Topic

Concepts

  • programme/script

  • IDE, terminal emulator, console

  • data types

  • modules

Skills

  • work with an IDE (basics)

  • create and execute a (Python) script

  • define variables

  • using built-in functions

Anaconda - It Won’t Bite!

In this course, we will be working with the Anaconda Python distribution. It comes with many tools that a programmer working with Python may need. We will be using the following components:

  • Python - the programming language we will use for this course!

  • Spyder - an integrated development environment (IDE) for Python.

  • numPy - a standard Python package for linear algebra and other important elements in scientific computing. We will use this later in the course for some calculations!

  • matplotlib -a 2D Python plotting library. This is how we visualise data and our results!

Go ahead and launch the Anaconda Navigator to get an overview of everything Anaconda related:

../../../_images/anaconda-navigator.jpg

Spyder - The Arachnophobe-Friendly Version

Spyder is an IDE for Python, and it includes a code editor (a text editor with code-highlighting and some other useful features), debugger and a console. This is where we will be developing our Python code. It can be launched directly or from the Anaconda Navigator. Once you have launched it, you should be looking at something like this:

../../../_images/spyder.jpg

The code editor is where you write, edit and save your programmes, which are a series of commands to be issued to the computer upon programme execution. The console is where you can execute either single Python commands or a set of commands you saved as a script. Python scripts have a .py extensions. Consequently, the script files you save may look like this:

myScript.py
wow_such_script.py

It is good practice to either use camelCase (first example) or snake_case (second example) when naming your files. Spaces should be avoided.

Simple Maths

Before we start writing commands in a script using the editor in Spyder, we have a closer look at the console and try out a few commands for doing simple maths in Python.

Operator

Description

Examples

+

addition

20 + 22

-

subtraction

2077 - 93

*

multiplication

578 * 4

/

division

1332/2

**

exponent

16**2

Type the examples into the console and hit enter on your keyboard after each entry. First, let’s try adding two values:

20 + 22

… now try subtraction:

2077 - 93

… multiplication:

578 * 4

… division:

1332/2

… and the exponent operator:

16**2

After hitting enter, the console should have returned the result of your operations in the next line. The results should have been:

42
1984
2312
666
256

Simple Functions

A function is a unit of code performing a specific task. Functions typically take one or several input arguments and return a value that is the result of performing the task under conditions set by the input arguments. Python comes with many standard functions you find in virtually all programming languages. One such function is the print function. The print function allows you to display the value of an input argument/variable. Note how in the example above, the console returned the result of your mathematical operations? Usually, you will have to explicitly ask for values to be displayed like above.

Let’s try the print function to display the words “wow, many print”. Type the following into the console and hit enter

print('wow, many print')

The console should have returned “wow, many print”. In this case, the input argument was “wow, many print”, i.e. the value to be displayed, and the function print displayed it.

Modules

Doing maths only with the operators above can be tedious. Luckily, Python has a math module that comes with some additional, useful functions. Modules are part of a programme (a component) that contain one or several functions. To get access to these functions, you first have to import the module (whether you’re working directly in the console or on a script). You can import the Python math module in several ways:

1 - simple import

import math

Import the module, and then use the sin() function of the math module by typing:

math.sin(2.0)

This should yield:

0.9092974268256817

Note that you have to add math. in front of your function to declare that you are using the sin() function of the math module. If you simply type sin(2), you will get an error message! However, you can make it work by importing the module’s functions as below.

2 - import everything from a module

from math import *

Now, if you simply type

sin(2.0)

you will get the same result as from typing math.sin(2). However, in most cases it is cleaner to be explicit in where (which module) you get the function from.

3 - import as [your choice]

If you do not wish to write out the full name of the module (some can be quite long), you can import it with a name of your choice To try this, let’s shorten math to mt by importing the module as follows:

import math as mt

If you now type

mt.sin(2.0)

you will get the same result as typing math.sin(2) before.

Note

In all the working cases above, the function we called was sin() and the input argument for the sin calculation was the number 2. Note that the input arguments for functions are always written into the brackets behind the function.

Combining Functions

Using functions separately and having to temporarily store the values in variables (more on variables later) can be inefficient and tedious. Luckily, functions can be combined. In the example below, we print (1st function) the result of taking the square root of 64 (2nd function):

print( math.sqrt(64) )

Note that some functions (can) take several input arguments. In case of the print function, we can pass a list of items to be displayed. In the example below, we add ‘square root of 64 =’ in front of the calculation:

print( 'square root of 64 =', math.sqrt(64) )

Executing this code in the console will give us:

square root of 64 = 8.0

Variables

Like a mathematical variable, a variable in your code can store values and has a specific name. This can be anything from x to u_wot_m8, but it is good practice to give your variables meaningful and understandable names. Let’s try storing the result of our calculation and the printed words above in variables.

x = math.sqrt(64)
a = 'square root of 64 ='

The result of our calculation is now stored in variable x, and our words are stored in a. Now we can pass them as arguments to the print function as such:

print( a, x )

This will yield the same result as above. Note how this can shorten your code a lot and allows you to use the result of your calculation in a lot of other operations without having to re-calculate it each time. Try this by using x in the operation below:

y = 0.5*x + 2
print( y )

Again, you stored the result of a calculation in a variable (y), and then displayed its (6) using the print function. You are also able to overwrite the data stored in a variable by assigning a new value to it, such as:

x = 0.5*x + 2

Before we move on to the next section, try one last thing - this time with the variable a, which stores some text:

y = 0.5*a

This didn’t work, did it? You got an error message that reads:

Traceback (most recent call last):

  File "<ipython-input-11-55b14d72f40d>", line 1, in <module>
    y = 0.5*a

TypeError: can't multiply sequence by non-int of type 'float'

Don’t get intimidated by error messages - they are your friends and help you fix your code. They usually show you the line in which the error occured. Since we have just written one line, the error occured in line 1. At the bottom of the message, it tells us what kind of error we have. In this case, we have a TypeError. This happens when you try to do an operation on a variable with an inappropriate/incompatible data type. Here, we tried to multiply a string (text) with a float (floating point number) 0.5. This brings us to the next topic: Data Types.

Data Types

Data types are categories for data, which specify which type of value a variable can take. These include:

Data Type

Explanation

Examples

Integer

whole numbers

42, 451

Float

(floating point) numbers with decimal points

20.77, 19.84, 2.312

Boolean

representing the two values of boolean logic

True, False

String

alphanumeric characters

dogge, system_shock_2019

There are ways to convert data types for specific purposes. For example, we may want to let a programme generate the name of output files automatically based on numbers used in your programme. First, let’s save an integeter in the variable year:

year = 2019

Let’s now try to generate a file name by combining (with a +) a string with the integer and pass it to a new variable (outfile) storing our file name.

outfile = 'myFile' + year

Again, you get a TypeError because you cannot add an integer to a string (‘myFile’). We will have to convert the integer to string with another built-in function: str()

outfile = 'myFile' + str(year)
print(outfile)

Typing the above into the console should give you:

myFile2019

Writing Scripts

Now, let’s use the editor. In your editor window, type the following:

# wow. such script. many calculation. wow.

# let's do some operations and save the results in variables
a=20 + 22
b=2077 - 93
c=578 * 4
d=1332/2
e=16**2

print(a, b, c, d, e)  # tell the computer to show us the values of each variable

Save the file in a location of your choice as test.py. You can execute the Python file from outside or inside your IDE. In your IDE, simply click the run button:

../../../_images/spyderRun.jpg

The commands will be executed in succession to give you:

42 1984 2312 666.0 256

Let’s have a look at what happened exactly: The first 3 lines were ignored, because line 2 was empty and lines 1 and 3 started with a hash (#). The hash tells the Python interpreter to ignore whatever comes behind it. This is extremely useful, since it allows you to add explanations to your code that will help you (and others) understand what’s going on. Such comments are an excellent way to document what we are doing to make sure that our code remains understandable. The following 5 lines of code perform the operations we did at the beginning of the exercise, and the results are saved in variables named a, b, c, d and e. Finally, the values of all the variables are displayed using the print function. Note that we have another comment in the same line. It is possible to add comments next to executed code. What comes behind # is ignored, but what comes before it in the same line will still be executed.

Note

Comments

It is good practice to explain every line of code in a script with a comment, and to add a few comments at the beginning of the script to explain what the script does. In a Python script, everything that follows the # in a specific line is interpreted a comment and will not be executed.

Your Task

Now you’ve covered enough of the basics of practical programming in the sections above to start writing in own script. To complete this exercise, write and submit a script that does the following:

  • calculates and displays the circumference and area of a circle with radius of 2

  • uses mathematical operators

  • loads a module and uses at least one function from it

  • stores values in variables somewhere in the script

  • combines 2 functions somewhere in the script

  • includes comments for every instruction (executed line of code) in the script

When you are happy with your script, save it as [your student number]_e001.[ext] and submit it.

Warning

Late submissions won’t be accepted!