0% found this document useful (0 votes)
2 views18 pages

Stage3 Python Intro 2021

Python is the primary programming language used in the Advanced Laboratories for data acquisition, analysis, and computational physics due to its ease of learning and extensive libraries. While it has some limitations in speed and complexity, it is widely supported and in demand in various fields, including physics and data analytics. The document outlines resources for learning Python, including tutorials and recommended practices for coding, while emphasizing the importance of originality and proper coding standards.

Uploaded by

prathameshratthe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Stage3 Python Intro 2021

Python is the primary programming language used in the Advanced Laboratories for data acquisition, analysis, and computational physics due to its ease of learning and extensive libraries. While it has some limitations in speed and complexity, it is widely supported and in demand in various fields, including physics and data analytics. The document outlines resources for learning Python, including tutorials and recommended practices for coding, while emphasizing the importance of originality and proper coding standards.

Uploaded by

prathameshratthe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Python in the

Advanced Labs
https://veritas.ucd.ie/~apl/apl/

John Quinn

1
A Unified Environment

Python is the programming framework for Data Acquisition, Data Analysis and
Computational Physics in the Advanced Laboratories.
2
Why Python?
• Unified approach: only one language to learn
• Python is a mature high-level language that is easy to learn (widely used for
teaching programming) but with many advanced features.

• “Batteries Included”: many many packages and libraries, especially scientific, are
available.

• Python and libraries are open source.


• Python is increasingly used in Physics, Astronomy and Industry/Finance especially
Data Analytics - Python skills are in demand!

• However Python is not perfect for everything and in cases where efficiency and
speed are needed, code is still written in C/C++ and called from Python.

• However, Python is the most generally-useful programming language for Physics


today, due to the scientific modules that are available and the ease of development!

3
Python in demand!

source

source

source

• Google: “most popular programming language”


4
Python issues
• It can be slow (there are ways to speed it up - generally not an issue in the labs.)
• It is a growing and evolving language:
• new features appear steadily
• there can be several ways to do something simple (e.g. string formatting)
• you never feel like you understand more than some small fraction of the language
• The number of libraries available is enormous:
• which one to chose and learn for a given task?
• In the Advanced Labs. we support standard Python, Numpy, Scipy (some of!).
• we do not support Pandas
• staff cannot be expected to know every library
• If you feel you need to use some other library for your analysis please consult with
the staff member in charge of the experiment.

5
Python Programming

• All you need is a text editor to type your programs and you can run them from the command line.
• For data analysis etc, where a document record is useful, Jupyter Notebook is often the best
choice.

• However, sometimes interactive iPython prompt or Python on the command line may be more
appropriate.

• There are also interactive development environments (IDEs) such as Thonny, Spyder …

6
When would you not want a Jupyter Notebook?

• Libraries: code reuse


• Scripting and automating tasks
• Interactivity
• Server-side processing
• Embedded systems programming:

7
Which Python?
• We use Python 3 (3.7 in installed on lab. computers)

• Anaconda Scientific Python Distribution from Continuum Analytics:


• Python 3.8 is now the default
• Completely free!
• Available for Linux, Mac OS X and Windows
• Includes hundreds of the most popular Python packages including
Scipy, Numpy, Matplotlib, iPython and Jupyter Notebook.
https://docs.anaconda.com/anaconda/packages/pkg-docs/

• Get from: https://www.anaconda.com/download/

8
How to learn Python?
• We assume everyone has basic Python from 2Y (Computational Science module)
• Three tutorials + four sets of exercises with online (zoom) support from staff:
• Wednesday, Sept. 15: Basic Python refresher + Exercises (2-6pm)
• Thursday, Sept. 16: Data Analysis I (2 × 2 hr session)
• Wednesday, Sept. 22: Numpy/Scipy tutorial + Exercises (2-6pm)
• Thursday, Sept. 22: Data Analysis II (2 × 2 hr session)
• Tuesday, Sept. 28: Data Acquisition in Python (4pm)
• Depending on how you get on, extra tutorials can be provided, or you can ask staff
for help on specific topic.

9
How to learn Python

Web site for the labs (under constant development): http://veritas.ucd.ie/~apl/apl


• Python: currently links to old slides - will be updated incrementally
• Python I: language fundamentals
• Python II: More advanced Python + Matplotlib/Numpy
• Data Acquisition in Python:
• Basic Concepts
• National Instruments USB-6008/6009
• IEEE-488 devices
• RBD 9103 Picoammeter
• (Data and Error Analysis)
• (Report writing)

10
How to learn Python

Recommended Online (Basic Python):


• Python Official Documentation: https://docs.python.org/ including a Tutorial.
• Tutorial: https://docs.python.org/3/tutorial/

Handy reference
• Python3 Cheat Sheet: http://perso.limsi.fr/pointal/python:memento

11
Python Basics
• Basic Python language features you should be(come) familiar with:
• variables: numbers (int, float), strings
• containers: lists, tuples and dictionaries
• indexing and slicing - also relevant for Numpy
• logical conditions: if… else…
• looping: for loops, while loops
• string formatting (new in Python 3.6: f-strings - highly recommended you use!)
• writing data to a file
• functions
• classes (using them)

12
Python: Numpy
• Numpy is an advanced Numerical Python library that is very powerful (and can
take a long time to learn in its entirety!).

• Numpy is coded in C++ and has extensive support for a wide variety of numerical
tasks, including efficient multi-dimensional arrays.

• Numpy can operate on arrays (vectorised), making it much faster than standard
Python loops and also easier-to-read less-error-prone code.

• Use Numpy’s containers and vector operations where appropriate.


• try to avoid mixing Numpy arrays with basic Python looping methods.
• Warning: don’t try to use vector operations on Python lists!

13
Observations of 3Y Student Code

− kT
• Operator precedence catches people out - example: z = e

− hνT
z = np.exp(-h*nu/k*T) Incorrect: this is z = e k

z = np.exp(-h*nu/(k*T)) correct

z = np.exp(-(h*nu)/(k*T)) correct

z = np.exp(-((h)*(nu))/((k)*(T))) correct but hard to read!

• Please make yourself aware of operator precedence (order in which


calculation are done) and strive to write correct and readable code!

• https://docs.python.org/3/reference/expressions.html#operator-precedence

14
Observations of 3Y Student Code
• Code not commented and poorly structured
• Long lines - split across multiple - check what you submit that it is not truncated!
• Variables poorly named

• Code overly complicated!


• Don’t try to automate a complicated analysis so it automatically analysed multiple
data files
• when something goes wrong it is difficult to track down!

15
Recommendations
• Keep it simple!
• straightforward easy-to-read and follow code is much more understandable and
maintainable than complicated code that is difficult to follow.
• strive to improve - experience and practice is critical!
• don’t be overwhelmed
• ask for help and advice
• be critical of results returned by google (e.g. stackoverflow)

“Debugging is twice as hard as writing the code in the first


place. Therefore, if you write the code as cleverly as possible,
you are, by definition, not smart enough to debug
it.” (Kernighan’s Law)

16
Recommendations

• Guide to how to structure your code, name variables etc:


• https://realpython.com/python-pep8/

• Guide to writing code comments:


• https://stackoverflow.blog/2021/07/05/best-practices-for-writing-code-comments/

17
Code Plagiarism
• Copying code directly and including it is Plagiarism
• The UCD Plagiarism checker will find matches between your code and each
other's, and online sites such as Stackoverflow.

• If you use a piece of code from the internet (beyond the trivial example of seeing
how to call a function), justify why and reference it!

• Googling for solutions (not reference documentation) should be a last resort not
a first port of call.

• be critical of the results returned by Google - is the source trustworthy?


• the aim of the Advanced Lab is to get you THINK for yourselves and to be able
to tackle new problems you have not met before, not to test your Googling
skills!

18

You might also like