Computational Programming With Python
Computational Programming With Python
Why Python?
Python is...
It has plenty of libraries among others the scien fic ones: linear algebra;
visualisa on tools; plo ng; image analysis; differen al equa ons solving; symbolic
computa ons; sta s cs; etc.
It has possible usages: Scien fic compu ng, scrip ng, web sites, text parsing, data
mining, ...
Premisses
December 2016
Examples
Python may be used in interac ve mode
In [2]: x = 3
In [3]: y = 5
In [4]: print(x + y)
Note:
In [2] is the prompt in IPython shell. It counts the statements. In the more basic
Python shell the prompt string is >>>
IPython
IPython is an enhanced Python interpreter.
[-3. 2.5]
More examples
Compu ng and :
(-1+1.2246467991473532e-16j)
In [7]: print(2**100)
1267650600228229401496703205376
** power
In [8]: 2**(2+2)
Out[8]: 16
In [9]: 1j**2
Out[9]: (-1+0j)
Variables
A variable is a reference to an object. One uses the assignment operator = to assign a
value to a variable.
In [10]: my_int = 42
my_float = 3.14
my_complex = 5+1j
my_sum = my_float+my_complex
print(my_sum)
print(type(my_sum))
(8.14+1j)
<class 'complex'>
For complex numbers, j is a suffix to the imaginary part. The code 5+j will not work.
Strings
Strings are "lists" of characters enclosed by simple or double quotes.
This is
a long,
long string.
Import modules
Importing Numpy
In order to use standard mathema cal functons in Python, you must import some module.
We will use numpy .
You can import numpy and then use dot-nota on a er the name numpy :
print(numpy.exp(1j*numpy.pi))
print(np.exp(1j*np.pi))
More about imports
You can choose what you want to import to avoid dot-nota on:
print(exp(1j*pi))
Choose Editor and the tab Advanced settings , then click Edit template for
new modules . Here you can enter the code for imports that will be included in all new
files.
One accesses elements of a list using zero-based indices inside square brackets.
List examples
In [12]: L1 = [1, 2]
print(L1[0])
print(L1[1])
# print(L1[2]) raises IndexError
1
2
a
[3, 4]
3
Negative indices
In [14]: L2 = ['a', 1, [3, 4]]
print(L2[-1]) # last element
print(L2[-1]) # second last element
[3, 4]
[3, 4]
List utilities ‐ range
range(n) can be used to fill a list with elements star ng with zero.
In [15]: L3 = list(range(5))
print(L3)
[0, 1, 2, 3, 4]
List utilities ‐ len
The func on `len(L) gives the the length of a list:
3
2
4
5
List utilities ‐ append
Use the method append to append an element at the end of the list.
c
d
List comprehension
A convenient way to build up lists is to use the list comprehension construct, possibly with
a condi onal inside.
De nition
Example:
print(L2)
print(L3)
Mathematical notation
This is very close to the mathema cal nota on for sets. Compare:
with
In [19]: L1 = [1, 2]
L2 = [3, 4]
L3 = L1 + L2
print(L3)
[1, 2, 3, 4]
More operations on lists
Logically, mul plying a list with an integer concatenates the list with itself several mes:
n*L is equivalent to
In [20]: L = [1, 2]
print(3*L)
[1, 2, 1, 2, 1, 2]
for loops
A for loop allows to loop through a list using an index variable. This variable takes
succesively the values of the elements in the list.
Example:
2
4
20
Repeating a task
One typical use of the for loop is to repeat a certain task a fixed number of mes:
In [ ]: n = 30
for i in range(n):
do_something # this gets executed n times
Indentation
The part to be repeated in the for loop has to be properly indented.