INTRODUCTION TO
PYTHON PROGRAMMING
FISSION REACTOR PHYSICS 1
Prof. Antonio Cammi ; Carolina Introini
[Link]@[Link] ; [Link]@[Link]
WHY PYTHON
• General purpose code
• Open source and lightweight
• Many packages also for data science
• Many applications and fields
• Download:
• Site: [Link]
• Terminal (Ubuntu): sudo apt-get install python3
• Also includes the Python package manager (pip3)
• Programming can be done directly into the terminal
• iPython shell: sudo apt-get install ipython3
[Link]@[Link] 1
QUICK SETUP
• iPython shell: (in terminal) ipython3
• To close the shell: exit
• You can run terminal commands (mkdir, cd, ls) from within the shell
• Python scripts (extension .py)
• How to run in terminal: python3 [Link]
• Use print() to generate the desired output from the script
• Within the shell, use run [Link] to execute a Python script
• Within the shell, use edit [Link] to modify a Python script
• Anaconda suite ([Link]
• Python: [Link]
[Link]@[Link]
2
QUICK SETUP
• We are going to use Google Colab ([Link]
• No download required
• Interactive environment (write & run)
• Integration with Google Drive / GitHub
[Link]@[Link]
3
FLOWCHART IN PROGRAMMING
• Flowchart = diagrammatic
representation of an algorithm
• Useful to organise the logic steps of
the program we want to write and to
easily explain it to others in a
straightforward way
• Easy way to draw flowcharts: [Link]
(Google Drive Add-on)
[Link]@[Link]
4
FLOWCHART IN PROGRAMMING
[Link]@[Link]
5
FLOWCHART IN PROGRAMMING
[Link]@[Link]
6
PYTHON VARIABLES
• Specific, case-sensitive name
• Call up value through variable name
• Assignment: VariableName = value
[Link]@[Link] 7
PYTHON VARIABLES - TYPES
• Specific, case-sensitive name
• Call up value through variable name
• Different types = different behaviour
• To check variable type: type(VariableName)
[Link]@[Link] 8
PYTHON VARIABLES - TYPES
• Specific, case-sensitive name
• Call up value through variable name
• Variable type conversion: str(), int(), float(), bool()
[Link]@[Link] 9
PYTHON VARIABLES – BASIC OPERATIONS
[Link]@[Link] 10
PYTHON VARIABLES – MATH OPERATIONS
• For complex mathematical operations, use the math module
• Includes math constants pi, e, tau, infinity, NaN
• Arithmetic functions (factorials)
• Power, exponential, logarithmic, trigonometric functions
[Link]@[Link] 11
PYTHON LISTS
• Collection of values
• Each variable represents a single value
• May contain any type
• When we have multiple data, not
convenient to use single variables • Even different types
[Link]@[Link] 12
PYTHON LISTS
• Specific functionality and behaviour
• Compound data type
• Lists can contain other lists!
[Link]@[Link] 15
PYTHON LISTS - SUBSETTING
• Python index starts from 0!
• ListName[-1] = last value of the list (count backwards)
[Link]@[Link] 16
PYTHON LISTS - SUBSETTING
• Elements of the list are variables, so we can perform operations on them
[Link]@[Link] 17
PYTHON LISTS - SUBSETTING
• List slicing: [start : end] (start inclusive, end exclusive)
• Not specifying the ‘start’ index means starting from the beginning
• Not specifying the ‘end’ index means going up to the last element
• Remember: start counting from 0
[Link]@[Link] 18
PYTHON LISTS - SUBSETTING
• Subset a list of lists: first index indicates the list, second one the element
[Link]@[Link] 19
PYTHON LISTS - MANIPULATION
• Warning! When creating a copy of a list, any modification done on the copy
is reflected on the original!
• What is happening: both lists actually points at the same list!
[Link]@[Link] 20
PYTHON LISTS - MANIPULATION
• Correct method to copy lists: CopyList = list()
[Link]@[Link] 21
PYTHON LISTS - MANIPULATION
• Change list elements
[Link]@[Link] 22
PYTHON LISTS - MANIPULATION
• Add / remove (del()) list elements
• Note: add can only add elements at the end of the list
• Note: when deleting an element, the indexes after change!
[Link]@[Link] 23
PYTHON FUNCTIONS
• Same as MATLAB functions
• Solve a particular task and are reusable
• Many pre-written functions exist (print, type, str, int)
• General syntax for calling a function: output = function_name(input)
• Function help(function_name) to open up the documentation (exit with ‘q’)
[Link]@[Link] 24
PYTHON FUNCTIONS
• Function help() to open up the documentation (exit with ‘q’)
• For standard tasks, probably a pre-written function exists!
• Your best bet: check the Internet
[Link]@[Link] 25
PYTHON FUNCTIONS
• Function help() to open up the documentation (exit with ‘q’)
• ‘ndigits=None’ means that if the argument ‘ndigits’ is not specified, it has
value ‘None’ (meaning that it is an optional argument)
[Link]@[Link] 26
PYTHON FUNCTIONS
• How to create custom functions
[Link]@[Link] 27
PYTHON METHODS
• Some functions are defined only for specific types of variables
• To check all methods available for a specific type: help(type)
• Example: function len() and round()
[Link]@[Link] 28
PYTHON METHODS
• Methods are functions that belong to objects
• Example: list index method (return the index of the specified list element)
[Link]@[Link] 29
PYTHON METHODS
• Other useful list methods
• [Link](element): get the number of times an element appears in the list
• [Link](element): remove the first element of a list that matches the input
• [Link](): reverse the order of elements in the list
• help(list): open the documentation with all methods for type ‘list’
[Link]@[Link] 30
PYTHON PACKAGES
• The default Python distribution has only an handful of generic functions
• Huge code base: messy and hard to find what you need
• Lots of code you won’t use
• Maintenance problem
• Packages collect specific functions, methods and types
• Thousands of packages available for many applications
• Numpy ; Matplotlib ; Scipy ; Math
• How to install packages:
• In terminal pip3 install packagename
• Already installed in Colab!
[Link]@[Link] 31
PYTHON PACKAGES
• How to import and use packages
[Link]@[Link] 32
PYTHON PACKAGES
• How to import and use packages
[Link]@[Link] 33
THE NUMPY PACKAGE
• List have quite a significant limitation
• Many operations cannot be done on them
[Link]@[Link] 34
THE NUMPY PACKAGE
• Solution: the NumPy package
• Numpy arrays as an alternative to Python lists
• Allows calculations over entire arrays (element-wise calculations)
• Installation: in terminal pip3 install numpy
• Already included in Colab, only to import
[Link]@[Link] 35
THE NUMPY PACKAGE
[Link]@[Link] 36
THE NUMPY PACKAGE
• Numpy arrays can contain only one type!
• If different types are detected, Python changes some types to obtain an
homogeneous array (bool -> int -> float -> str)
• Different behaviour between lists and numpy arrays!
[Link]@[Link] 37
NUMPY - SUBSETTING
• Same as lists (index from 0, square brackets)
[Link]@[Link] 38
NUMPY – 2D ARRAYS
• Creation syntax: [Link]([[row #1], [row #2] …])
[Link]@[Link] 39
NUMPY – 2D ARRAYS
• Subsetting of 2D Numpy arrays
[Link]@[Link] 40
NUMPY – DOCUMENTATION
• All information are contained in the documentation
[Link]@[Link] 41
DATA VISUALISATION
• Package Matplotlib
• Installation: in terminal pip3 install matplotlib
• For most applications, the sub-package pyplot is used
[Link]@[Link] 42
DATA VISUALISATION
• Example: point-kinetic equation (1G) approximate solution
[Link]@[Link] 43
DATA VISUALISATION
• Example: point-kinetic equation (1G)
[Link]@[Link] 44
DATA VISUALISATION
• Example: point-kinetic equation (1G) – scatter plot
[Link]@[Link] 45
DATA VISUALISATION
• Example: point-kinetic equation (1G) – plot customisation
[Link]@[Link] 46
DATA VISUALISATION
• Example: point-kinetic equation (1G) – plot customisation
[Link]@[Link] 47
DATA VISUALISATION
• Example: point-kinetic equation (1G) – error bars
[Link]@[Link] 48
LOGIC OPERATORS
• Comparison operators return a Boolean value (True / False)
[Link]@[Link] 49
LOGIC OPERATORS
• When using numpy arrays: np.logical_and / np.logical_or / np.logical_not
[Link]@[Link] 50
IF/ELSE STATEMENT
[Link]@[Link] 51
LOOPS
• Loops are used to repeat statements, for example to repeat the if/else
statement over all elements of the array.
• Two types of loop: while loop and for loop
• While loop: repeat the instruction until the exit condition is met
[Link]@[Link] 52
LOOPS
• Two types of loop: while loop and for loop
• For loop: repeat the instruction for a certain number of times
[Link]@[Link] 53
DATA VISUALISATION + FOR LOOP
• Example: fission spectrum X(E) defined as:
• x(E)dE = average number of fission neutrons emitted with energy E in [E to E + dE] per
fission neutron
[Link]@[Link] 54
DATA VISUALISATION + FOR LOOP
• Example: fission spectrum X(E) defined as:
• x(E)dE = average number of fission neutrons emitted with energy E in [E to E + dE] per
fission neutron
[Link]@[Link] 55
DATA VISUALISATION + FOR LOOP
• Example: fission spectrum X(E) defined as:
• x(E)dE = average number of fission neutrons emitted with energy E in [E to E + dE] per
fission neutron
[Link]@[Link] 56
INTRODUCTION TO
PYTHON PROGRAMMING
FISSION REACTOR PHYSICS 1
Prof. Antonio Cammi ; Carolina Introini
[Link]@[Link] ; [Link]@[Link]
Receiving hours: Wednesday 16:00 – 18:00
Building B12 (campus LaMasa, Department of Energy)