Introduction To Python
Introduction To Python
Introduction to Python
We use Jupyter notebook for programming in Python. To open a new notebook, go to Anaconda
Navigator (if you don’t have it yet, please download it). After it has launched, look for “Jupyter
notebook” and click on the “Launch” button. On the next screen you will see buttons for
“Upload” and “New” on the top right. Click on “New” and then “Python 3” on the dropdown
list that appears.
You will now have an open workbook and see “In [ ]:”. You can type your commands here.
You can type more than one command at a time (on separate lines) and then click on “Run”
or use the keyboard shortcut shift-enter to execute the commands.
For the next operations you need to import the math module first, type: import math and run
the command. You only have to do this once in a workbook. The following can now be used:
Exercise: √
Play around with using Python as a calculator. For example, find the values of 2 + 3 6 and
35 − cos(10). Now assign these to variables a and b (using a=... and b=...) and add the two.
You can use print(a) for example if you want to see the value stored in the variable a.
Note that you don’t have to re-type the expressions. You can just add things to cells that you
already ran.
Remark: The above functions are used when it is applied to a single number a. When these
operations need to be applied to a list (or vector) of numbers, you have to use numpy. instead
of math. . (See “Important notes” in section 1.2 of this document.)
1
1.1 The ‘for’ loop
To learn how a ‘for’ loop works, enter the following lines and run the code.
sum = 0
k = 41
sum = sum + k**2
k = 42
sum = sum + k**2
k = 43
sum = sum + k**2
print(sum)
43
X
The code above calculates k 2 = 412 + 422 + 432 . The following is a much better way of
k=41
calculating it, without so much repetition of commands:
sum = 0
for k in [41,42,43]:
sum = sum + k**2
print(sum)
The following will do the exact same thing:
sum = 0
for k in range(41,44):
sum = sum + k**2
print(sum)
Important notes:
for tells Python that you are going to use a loop (i.e. something that will happen more
than once). The notation for this is to start with the word for, then give a variable
that will change every time the loop is executed (in this case k), then the values that the
variable should take on, and then finally the command is ended with a :.
The indented commands after the : are then the things inside the loop and in this case
executed 3 times, once for each integer value of k between 41 and 43.
Note that range can be used instead of manually typing all the options (if we want a loop
to run many times). The function range needs 2 input values, the first is the number
where it should start counting and then the last is the number AFTER the last number
you want, so in this case it is 44 (43 + 1).
In Python the indentation indicates the end of the for loop. Therefore the print(sum)
command is only executed once, after the loop is done.
Exercise
10
X
Use Python to calculate i3 . You should get 3025. Ask your tutor/lecturer if you are not
i=1
able to get this answer.
2
x=[Link](−1,3.1,0.1)
y= x**3 − 3*x**2 + 3
for xp, yp in zip(x,y):
xprint=format(xp,'.2f')
yprint=format(yp,'.2f')
print(xprint, '\t', yprint)
Important notes:
To construct the x-values we use [Link](−1,3.1,0.1). This sets up values
from −1 to 3 with stepsize 0.1. Note that we use 3.1 instead of 3 for the endpoint, since
Python does not use the last value (look at how range worked again).
The for loop prints the values of x and y as a vertical list. The format commands are
used since we don’t want to display too many decimals in our list. The 't' is just used
to add some space between the x and y values in the printed list.
When you assign values to y using the whole list of values in x as above, you cannot use
the commands [Link](x) etc as in the table. You have to use [Link](x) etc
in order to apply the operation to all the values in the variable x.
This is not the only way to set up the vectors x and y, nor the only way to print a table.
You may use other commands, or the tabulate environment:
Can you see that x is a vector that contain all the endpoints in the subinterval? What is the
meaning of y?
3
You may want to use [Link]() to insert a grid on your graph - it makes it easier to
see where the graph cuts the axis.
Final remarks
When you get an error and you want to fix your mistake, it is not necessary to retype (or
copy) everything. You can just fix the mistake where it is and run the code again.
You will not be expected to know all the code/commands on this document, but rather
understand it. In the class tests incomplete programs or programs with errors will be
given, so you should be able to complete it and/or fix it.
The code in this document is not the only (or necessarily the best) way of programming
these problems. If you have another way of doing it you can use that, as long as you don’t
use other packages and you still understand the code given on this document.