Introduction To Programming With Python 3
Introduction To Programming With Python 3
Orestis Malaspinas
1/64
Introduction to high performance
computing for modeling
2/64
Why do we need programming in this course?
3/64
Computers are fast but... we always want more
4/64
End of module
Introduction to high performance
computing
Coming next
Concepts of code optimization
5/64
Concepts of code optimization
6/64
Computers are fast but... we always want more
7/64
Computers are fast but... we always want more
8/64
Benchmark: Sum of n = 106 integers
By coding naively C++ is 100 times faster than Python but with NumPy the performance is
equivalent.
9/64
End of module
Concepts of code optimization
Coming next
Concepts of parallelism
10/64
Concepts of parallelism
11/64
A computer is fast... many computers together
are faster
Performance
• One modern processor ∼ 100 Gflops = 1011 flops.
• The fastest supercomputer (as of 4.1.2015) ∼ 52 Pflops = 52 · 1018 flops.
12/64
A computer is fast... many computers together
are faster
13/64
End of module
Concepts of parallelism
Coming next
Palabos, a parallel lattice Boltzmann
solver
14/64
Palabos, a parallel lattice Boltzmann
solver
15/64
The Palabos library
Palabos, http://www.palabos.org
• C++ parallel open-source library. • Fluid flow solver based on LBM.
• Parallelism completely transparent. • Multi-physics modules.
Example: Air-conditioning
16/64
Parallelism in Palabos: Multi-Block approach
17/64
End of module
Palabos, a parallel lattice Boltzmann
solver
Coming next
An introduction to Python 3
18/64
An introduction to Python 3
19/64
Introduction to Python 3
20/64
Literature on Python 3
21/64
Interpreted vs Compiled languages
Interpreted Compiled
• Program is directly run by the interpreter. • Program transformed into machine code and
• The interpreter translates a code into its saved into an executable file.
subroutines that are precompiled. • The executable file can then be run.
• Examples: Python, JavaScript, Ruby, ... • Examples: C/C++, Fortran, ...
22/64
Interpreted vs Compiled languages
23/64
End of module
An introduction to Python 3
Coming next
Running a Python program
24/64
Running a Python program
25/64
The interactive shell
26/64
Spyder 3
27/64
Running a Python script
28/64
End of module
Running a Python program
Coming next
Variables and data types
29/64
Variables and data types
30/64
Variables
Container to store values that can accessed and changed.
31/64
Variables: identifiers
Rules for variable identifiers
1. The first character can be ANY Unicode letter or the underscore.
2. The other characters can be ANY Unicode letter or digit or the underscore.
3. All the Python keywords are forbidden.
4. Identifiers are case sensitive.
Examples
32/64
Data types
Built-in types
Numbers Strings
1. Integers (arbitrary size) Immutable chain of Unicode characters.
>>> type(2378) >>> ’Single line strings defined with single quotes’ 1
<class ’int’> ’Single line strings defined with single quotes’ 2
>>> "Or with double quotes" 3
’Or with double quotes’ 4
2. Floating-point numbers >>> c = ’’’Triple quotes for multiline 5
>>> type(3+6j)
<class ’complex’>
33/64
Data types (continued)
Built-in types
Other types Examples
1. Boolean type 1 >>> type(True)
>>> print(type(True),type(False)) 2 <class ’bool’>
<class ’bool’> <class ’bool’> 3 >>> bool("")
4 False
5 >>> bool("hi")
2. None Type 6 True
7 >>> bool(0)
>>> type(None)
8 False
<class ’NoneType’>
9 >>> bool(10)
10 True
11 >>> print(True+True,True+False)
12 2 1
34/64
Data types (continued)
Built-in types
Sequence Types Examples
1 >>> a = [1,7,29,1,39]; a[2]
1. list : mutable sequence. 2 29
>>> type([1,’is an int’,2.0,’is a float’]) 3 >>> a[3] = "a"; print(a)
<class ’list’> 4 [1, 7, 29, ’a’, 39]
5 >>> b=(’njd’,[1,1],1.0)
6 >>> b[0] = 1
2. tuple : immutable sequence. 7 Traceback (most recent call last):
>>> type((1,’is an int’,2.0,’is a float’)) 8 File "<stdin>", line 1, in <module>
<class ’tuple’> 9 TypeError: ’tuple’ object does not
10 support item assignment
11 >>> b[1][0] = 4
3. range : immutable number sequence. 12 >>> print(b)
13 (’njd’, [4, 1], 1.0)
>>> type(range(10)) 14 >>> print(list(range(4)))
<class ’range’> 15 [0, 1, 2, 3]
35/64
End of module
Variables and data types
Coming next
Operators
36/64
Operators
37/64
Operators: on numeric types
38/64
Operators: on sequence types
Operators (sequence types) Examples
• Concatenation + . 1
2
>>>
>>>
a = [1,2,3,4,"b"]
a[0]
• Copies of a sequence * . 3 1
4 >>> a[0] = 9; print(a)
• Access to element [] . 5 [9, 2, 3, 4, ’b’]
6 >>> a+a
• Slice : . 7 [9, 2, 3, 4, ’b’, 9, 2, 3, 4, ’b’]
8 >>> 2*a
9 [9, 2, 3, 4, ’b’, 9, 2, 3, 4, ’b’]
10 >>> a[1:4]
11 [2, 3, 4]
12 >>> a[0:4:2]
13 [9, 3]
14 >>> a[2:]
15 [3, 4, ’b’]
16 >>> a[:2]
17 [9, 2]
39/64
Operators: Boolean operations and comparisons
40/64
End of module
Operators
Coming next
Control structures: conditional
statements
41/64
Control structures: conditional
statements
42/64
Control structures: conditional statements
43/64
Conditional statements (example)
44/64
Control structures: conditional statements
Example Output
1 x = int(input("Input an integer: ")) Input an int: 10
2 a = "even" if x%2 == 0 else "odd" 10 is an even number.
3 print(x, " is an ", a, " number.")
Input an int: 15
15 is an odd number.
45/64
End of module
Control structures: conditional
statements
Coming next
Control structures: loops
46/64
Control structures: loops
47/64
Control structures: for loop
Loop over any kind of ordered sequence.
The for statement Example
for variable in sequence: animals = ["cat", "dog", "cow", "bird"] 1
statements_1 for x in animals: 2
[else: if x=="cow": 3
statements_2] break 4
print(x) 5
else: 6
Remarks print("no cow in my list") 7
8
• Indentation is important! cat 9
dog 10
• The sequence can be of any type.
• If sequence is numbers use range.
• The else statement is optional.
• break statement exits the loop.
48/64
Control structures: while loop
Perform an action as long as a condition is satisfied.
49/64
End of module
Control structures: loops
Coming next
Functions
50/64
Functions
51/64
Functions
Definition and use
√
• Provided some input parameters compute one or more results (f : x → x).
• More generally a set of statements that can be reused in a program.
• A basic building block of a program.
Syntax Remarks
def function_name(parameter_list): • The return and comments statements are optional.
[""" comments (doc) """]
statements • The return statement ends the function and returns
[return]
the result.
• Functions always return None by default.
• The number of parameters can be zero or more.
• Indentation is important.
52/64
Functions (example)
Code Result
1 def isPrime(number): isPrime(number)
2 """Check if number is prime""" Check if number is prime
3 if number <= 1:
4 return False 0 is not a prime number
5 1 is not a prime number
6 for i in range(2,number): 2 is a prime number
7 if number % i == 0: 3 is a prime number
8 return False 4 is not a prime number
9 return True 5 is a prime number
10 6 is not a prime number
11 help(isPrime) 7 is a prime number
12 for j in range(0,13): 8 is not a prime number
13 if (isPrime(j)): 9 is not a prime number
14 print(j, " is a prime number") 10 is not a prime number
15 else: 11 is a prime number
16 print(j, " is not a prime number") 12 is not a prime number
53/64
Functions: Scope of variables
Variables have the scope of the block they are declared in and can be used after their point of
declaration.
Global variable Error
1 def foo(): 1 def baz():
2 print(a) 2 print(a)
3 3 a="Hi!"
4 a = "Hello!" 4 print(a)
5 foo() 5
6 Hello! 6 a = "Hello!"
7 baz()
Local variable 8 Traceback (most recent call last):
9 File "scope.py", line 27, in <module>
1 def bar(): 10 foo()
2 a="Hi!" 11 File "scope.py", line 22, in foo
3 print(a) 12 print(a)
4 13 UnboundLocalError: local variable ’a’
5 a = "Hello!" 14 referenced before assignment
6 bar()
7 Hi!
54/64
Functions: Mutability/Immutability
Number List String, Tuple
1 def foo(a): 1 def bar(a): 1 def baz(a):
2 a=4 2 a[1]=-2 2 a[1]="n"
3 print(a) 3 print(a) 3 print(a)
4 4 4
5 a=2 5 a=[2,4,5] 5 a="abcdef"
6 foo(a) 6 bar(a) 6 baz(a)
7 print(a) 7 print(a) 7 print(a)
8 4 8 [2, -2, 5] 8 Traceback (most recent call last):
9 2 9 [2, -2, 5] 9 File "immutable_mutable.py",
10 line 37, in <module>
11 foo(a)
12 File "immutable_mutable.py",
13 line 33, in foo
14 a[1]="n"
15 TypeError: ’str’ object does not support
16 item assignment
55/64
Modules
• Using alias
Creation and use
>>> import checkPrime as cp
• Saving the functions into .py files. >>> cp.isPrime(8)
False
• Using the import...[as]... keyword.
• All math functions are in math
module
56/64
End of module
Functions
Coming next
NumPy
57/64
NumPy
58/64
NumPy
60/64
NumPy: ndarray manipulations
Matrix-Scalar manipulations
Example NumPy Example List
1 import numpy as np 1 Y = [[0]*2, [0]*2]
2 2 X = [[1]*2, [1]*2]
3 X = np.ones((2,2)) 3 for i in range(2):
4 print(1+2.5*X) 4 for j in range(2):
5 [[ 3.5 3.5] 5 Y[i][j]=1+2.5*X[i][j]
6 [ 3.5 3.5]] 6
7 print(Y)
8 [[ 3.5 3.5], [ 3.5 3.5]]
61/64
NumPy: ndarray manipulations
Matrix-Matrix manipulations
Example NumPy Example List
1 import numpy as np 1 X = [list(range(2)), list(range(3,5))]
2 2 Y = [list(range(2)), list(range(3,5))]
3 X = np.arange(4).reshape(2,2) 3 Z = [[0]*2, [0]*2]
4 Y = np.arange(4).reshape(2,2) 4 for i in range(2):
5 Z = X+Y 5 for j in range(2):
6 print(Z) 6 Z[i][j]=X[i][j]+Y[i][j]
7 print(Z*Z) 7 print(Z)
8 [[0 2] 8 ZZ = [[0]*2, [0]*2]
9 [4 6]] 9 for i in range(2):
10 [[ 0 4] 10 for j in range(2):
11 [16 36]] 11 ZZ[i][j]=Z[i][j]*Z[i][j]
12 print(ZZ)
13 [[0, 2], [4, 6]]
14 [[0, 4], [16, 36]]
62/64
NumPy: linalg
63/64
End of module
NumPy
End of Week 2
Thank you for your attention!
64/64