0% found this document useful (0 votes)
228 views

Introduction To Python Programming: Fission Reactor Physics 1

This document provides an introduction and overview of Python programming. It discusses why Python is useful, how to set up Python, variables and data types in Python, lists, functions, packages like NumPy, and 2D arrays in NumPy. The key topics covered include installing Python and related tools, the Python interactive shell, creating and running Python scripts, defining variables and common data types, working with lists through indexing, slicing, and common manipulation methods, defining and calling functions, installing and importing external Python packages, and creating and accessing elements of 2D NumPy arrays.

Uploaded by

Leonardo Monaco
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
228 views

Introduction To Python Programming: Fission Reactor Physics 1

This document provides an introduction and overview of Python programming. It discusses why Python is useful, how to set up Python, variables and data types in Python, lists, functions, packages like NumPy, and 2D arrays in NumPy. The key topics covered include installing Python and related tools, the Python interactive shell, creating and running Python scripts, defining variables and common data types, working with lists through indexing, slicing, and common manipulation methods, defining and calling functions, installing and importing external Python packages, and creating and accessing elements of 2D NumPy arrays.

Uploaded by

Leonardo Monaco
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

INTRODUCTION TO

PYTHON PROGRAMMING
FISSION REACTOR PHYSICS 1
Prof. Antonio Cammi ; Carolina Introini
[email protected] ; [email protected]
WHY PYTHON
• General purpose code
• Open source and lightweight
• Many packages also for data science
• Many applications and fields
• Download:
• Site: https://www.python.org/downloads
• 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

[email protected] 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 filename.py
• Use print() to generate the desired output from the script
• Within the shell, use run filename.py to execute a Python script
• Within the shell, use edit filename.py to modify a Python script
• Anaconda suite (https://www.anaconda.com/products/individual)
• Python: https://anaconda.org/anaconda/python

[email protected]
2
QUICK SETUP
• We are going to use Google Colab (https://colab.research.google.com/)
• No download required
• Interactive environment (write & run)
• Integration with Google Drive / GitHub

[email protected]
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: draw.io
(Google Drive Add-on)

[email protected]
4
FLOWCHART IN PROGRAMMING

[email protected]
5
FLOWCHART IN PROGRAMMING

[email protected]
6
PYTHON VARIABLES
• Specific, case-sensitive name
• Call up value through variable name
• Assignment: VariableName = value

[email protected] 7
PYTHON VARIABLES - TYPES
• Specific, case-sensitive name
• Call up value through variable name
• Different types = different behaviour
• To check variable type: type(VariableName)

[email protected] 8
PYTHON VARIABLES - TYPES
• Specific, case-sensitive name
• Call up value through variable name
• Variable type conversion: str(), int(), float(), bool()

[email protected] 9
PYTHON VARIABLES – BASIC OPERATIONS

[email protected] 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

[email protected] 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

[email protected] 12
PYTHON LISTS
• Specific functionality and behaviour
• Compound data type
• Lists can contain other lists!

[email protected] 15
PYTHON LISTS - SUBSETTING
• Python index starts from 0!
• ListName[-1] = last value of the list (count backwards)

[email protected] 16
PYTHON LISTS - SUBSETTING
• Elements of the list are variables, so we can perform operations on them

[email protected] 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

[email protected] 18
PYTHON LISTS - SUBSETTING
• Subset a list of lists: first index indicates the list, second one the element

[email protected] 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!

[email protected] 20
PYTHON LISTS - MANIPULATION
• Correct method to copy lists: CopyList = list()

[email protected] 21
PYTHON LISTS - MANIPULATION
• Change list elements

[email protected] 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!

[email protected] 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’)

[email protected] 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

[email protected] 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)

[email protected] 26
PYTHON FUNCTIONS
• How to create custom functions

[email protected] 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()

[email protected] 28
PYTHON METHODS
• Methods are functions that belong to objects
• Example: list index method (return the index of the specified list element)

[email protected] 29
PYTHON METHODS
• Other useful list methods
• list.count(element): get the number of times an element appears in the list
• list.remove(element): remove the first element of a list that matches the input
• list.reverse(): reverse the order of elements in the list
• help(list): open the documentation with all methods for type ‘list’

[email protected] 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!

[email protected] 31
PYTHON PACKAGES
• How to import and use packages

[email protected] 32
PYTHON PACKAGES
• How to import and use packages

[email protected] 33
THE NUMPY PACKAGE
• List have quite a significant limitation
• Many operations cannot be done on them

[email protected] 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

[email protected] 35
THE NUMPY PACKAGE

[email protected] 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!

[email protected] 37
NUMPY - SUBSETTING
• Same as lists (index from 0, square brackets)

[email protected] 38
NUMPY – 2D ARRAYS
• Creation syntax: np.array([[row #1], [row #2] …])

[email protected] 39
NUMPY – 2D ARRAYS
• Subsetting of 2D Numpy arrays

[email protected] 40
NUMPY – DOCUMENTATION
• All information are contained in the documentation

[email protected] 41
DATA VISUALISATION
• Package Matplotlib
• Installation: in terminal pip3 install matplotlib
• For most applications, the sub-package pyplot is used

[email protected] 42
DATA VISUALISATION
• Example: point-kinetic equation (1G) approximate solution

[email protected] 43
DATA VISUALISATION
• Example: point-kinetic equation (1G)

[email protected] 44
DATA VISUALISATION
• Example: point-kinetic equation (1G) – scatter plot

[email protected] 45
DATA VISUALISATION
• Example: point-kinetic equation (1G) – plot customisation

[email protected] 46
DATA VISUALISATION
• Example: point-kinetic equation (1G) – plot customisation

[email protected] 47
DATA VISUALISATION
• Example: point-kinetic equation (1G) – error bars

[email protected] 48
LOGIC OPERATORS
• Comparison operators return a Boolean value (True / False)

[email protected] 49
LOGIC OPERATORS
• When using numpy arrays: np.logical_and / np.logical_or / np.logical_not

[email protected] 50
IF/ELSE STATEMENT

[email protected] 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

[email protected] 52
LOOPS
• Two types of loop: while loop and for loop
• For loop: repeat the instruction for a certain number of times

[email protected] 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

[email protected] 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

[email protected] 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

[email protected] 56
INTRODUCTION TO
PYTHON PROGRAMMING
FISSION REACTOR PHYSICS 1
Prof. Antonio Cammi ; Carolina Introini
[email protected] ; [email protected]

Receiving hours: Wednesday 16:00 – 18:00


Building B12 (campus LaMasa, Department of Energy)

You might also like