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

T00 - Python intro

Uploaded by

Lucas Zhang
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

T00 - Python intro

Uploaded by

Lucas Zhang
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Data Acquisition and

Analysis on your laptop


Guide to setup Python on your laptop
Procedure is a modified version of a workshop set up at UBC in 2019 by
Engineering Physics student Morten Kals

© Chris Waltham, UBC Physics & Astronomy 2023


Why are we making you do this?
• Physics is about observing, measuring and attempting to understand the physical universe at all
length and timescales.
• Today we have hitherto undreamed of capacity to measure and model physical systems.
• Your laptop has a capacity to acquire and analyze data at GHz rates.
• Professional programming languages are open source and easy to install.
• Sensors to measure voltage, temperature, distance, pressure etc. are extraordinarily cheap and
easy to obtain.
• Microcontrollers needed as an interface between computer and sensor are cheap and simple to
run.
• These tools have tremendous power; you should know how to use them.
• You will use Python in other Science One disciplines as well, not just physics!

© Chris Waltham, UBC Physics & Astronomy 2023


Goal of this guide
• The goal is simple. You will install Anaconda to install Python, which you will then use with JupyterLabs
to write some code!
Goal of this guide (explained)
• “Python” (not the snake) is a powerful programming language used in many
scientific disciplines. There exist thousands of packages (sometimes called
“libraries”) that extend the capability of a basic Python installation (e.g. to get
your laptop to communicate with your Arduino board).

• There are various ways to write and run Python code, but one of the most useful
for scientific purposes is to use an interface called “JupyterLab” that runs straight
out of your web browser.

• Managing, installing and constantly updating all the tools and packages
associated with your Python setup can be tricky. That’s why there is a software
called “Anaconda” that does it all for you in a very easy user-friendly way.

© Chris Waltham, UBC Physics & Astronomy 2023


Setting up Python on your
laptop
• The goal now is to install everything you need using Anaconda, and then to run a
simple Python code in a JupyterLab window on your web browser.
• Save for later the details of how to program, and how to access your Arduino.
• TAKE IT SLOW – follow instructions carefully and in order.
• If things don’t work: DON’T PANIC – stop, think, ask!
• When and where to ask for help?
• Step 1: Asking a peer or doing a simple internet search can answer common questions quickly!
• Step 2: Post the question in the “Python” folder on Piazza.
• Step 3: For big issues that might require more hands-on support, go to your Python drop-in session:
• Thursday 2-4pm (last name A to M)
• Friday 10-12pm (last name N to Z)
Setting up Python on your PC
We are going to need multiple tools for this task: the next slides will guide you through installing all of them. Do not
worry if you do not understand all the following steps and concepts. Things will become clearer as you use the tools.
If you have a previous Python installation on your laptop, you may want to uninstall it, lest there be a conflict with what follows. For advanced users, you
can also decide to create a new separate environment if you are worried about conflicts with your Python versions and/or package dependencies.

Installing Anaconda
• Anaconda is a package manager (a program that helps you install all the things you need, and that keeps it organized)
which is made for Python. To install Anaconda, do the following:
• https://www.anaconda.com/download and then scroll down the page to “Download” the installer (it should recognize
your operating system). This is a large file, so make sure you have a fast and stable internet connection.
• For MacBook users, you might be prompted to choose between the Intel or M1/M2 chip version (on top left of your screen click on > “About
this Mac” and double-check what chip you have on your laptop)
• If Anaconda starts asking for donations, stop and back up! Anaconda is available for free!
• Ignore links prompting you to “run cloud notebooks” or “code in the cloud”.

• Run the installer. This may take a while depending on your machine (last year a student reported it took several hours
to install on an old MacBook Air!). Make sure you have enough space on your hard drive (it will take 4-5 GB).
Run the “Anaconda Navigator”
application
• After installation “Anaconda Navigator” might start automatically. If not, run the application.

• When Anaconda is starting, you might see a black terminal window flash briefly (this is normal)

• Ignore prompts to sign in to an Anaconda cloud account. In Science One, you will be running Python on your own
machine, not on the cloud (i.e. someone else’s computer).

• Your Anaconda Navigator interface should look something like this:

• In the rare case that you find that the interface


is huge and doesn’t fit on your screen, make
sure that “Preferences” > “Enable high DPI
Scaling” is ticked and press “Apply”.

• Note: how you access preferences depends on


your operating system.
Make sure important libraries are
installed
• In the left bar menu, select “Environments”

• Stay in “base (root)” and use the search box on the top right to check you have “matplotlib” and “scipy” libraries
installed (they will have a small green checkbox if installed)

• If for whatever reason they are not


installed, just click on the checkboxes
for each uninstalled package (a small
arrow will appear in the checkbox).
Once all packages are selected, click
on the green “Apply” on the bottom
right.

© Chris Waltham, UBC Physics & Astronomy 2023


Using the pip package manager
We are going to use a package called Arduino-Python3. This package is not available through the default package manager in
Anaconda (called Conda). To install it, we need to use another package manager called pip.
To install Arduino-Python3 with pip, do the following:
• Go to the environments tab of Anaconda.
• Click the play button next to the base (root) environment you set up earlier. Click Open Terminal.
• On MacOS, you might need to allow “Anaconda-Navigator” to gain access to control “Terminal”. If prompted to do so, click “OK”.

• You will get a Terminal/Command Line window, which is referencing the Conda environment you launched it from.
• Type pip install arduino-python3 followed by return (note that case matters, this is all in small caps!)
• Look for a message indicating if the installation was successful.
• Close the Terminal/Command Line window, return to Anaconda.
• After having installed the package, you should get the message:
“Successfully installed Arduino-python3-0.6 pyserial-3.5”

After having installed the package, a terminal window on a Windows machine looks like the image above.
Note: you might launch Jupyter Notebook by
accident (they are right next to another). Both
JupyterLab and Jupyter Notebook do the same

Getting Started with JupyterLab thing but have slightly different user
interfaces. Just close the Notebook browser
tab that opened and launch JupyterLab
instead.
• To start working with JupyterLab, do the following:
• In Anaconda Navigator, go to “Home”
• Hit “Launch” on JupyterLab.

On MacOS, you might get a pop-up window


asking:

The “git” command requires the command line


developer tools. Would you like to install the
tools now?

You can ignore that message for now and click


“Cancel” to proceed with the tutorial.
Alternatively, if you plan on using versioning
tools for coding, you can instead install
developer tools. This is a large download and
might take some time. In this case at least, you’ll
stop getting the pop-up 
© Chris Waltham, UBC Physics & Astronomy 2023
Opening JupyterLab
Your browser* will now open a tab looking like this:

Click on Python 3 below Notebook and above


Console.

You can select in the pane on the left which of your


directories you wish to work in.

* We recommend you use Chrome or Firefox. Students have had


stability issues with Safari in the past. You can just copy the URL
link to open the same instance of JupyterLab in another tab or in
another browser. The URL link will always be of the form
“localhost:888N/lab”, where N is some integer (usually 8 or 9).

© Chris Waltham, UBC Physics & Astronomy 2023


Opening JupyterLab
Make sure your cell type is set to “Code” in the drop-
down menu above. If not, select it.
The tab should now look like this:

• Type 1+1 in a cell (i.e. the grey box) in the


right hand pane.

• Hit “Run” symbol ( ) or press the keyboard


shortcut “shift-enter”

• You should see the response 2 and bingo,


you are coding in a serious professional
computing language on your laptop. The
sky is the limit.

• Notice that this and all subsequent


commands can be save in a file with
extension .ipynb (for interactive python
notebook).

© Chris Waltham, UBC Physics & Astronomy 2023


As a final check: you should see no error
messages when you run this code in JupyterLab:
Case matters: “arduino” is not the same as “Arduino”

© Chris Waltham, UBC Physics & Astronomy 2023


Commenting code
• Commenting code is important so that others can understand what you are doing (not to mention
yourself, if you come back to some code you haven’t touched in weeks)
• When your cell has the “Code” type, you can always comment your Python code by using a
hashtag: # Comments will be ignored when running the cell. Try adding a comment above,
beneath or after your 1+1, and see for yourself!

© Chris Waltham, UBC Physics & Astronomy 2023


Annotating code
• To create more “report-like” notebooks that explain what you are doing in detail, you can also
change entire cells to formatted text. Change your cell type from “Code” to “Markdown” and
type something, then run the cell
• You can also type equations with LaTeX. Try typing $ F = \frac{G m_1 m_2}{r^2} $ and run the
cell.
• Markdown is a very simple but powerful markup language used nearly everywhere. It’s useful to
learn the basics: go to Help > Markdown Reference.

© Chris Waltham, UBC Physics & Astronomy 2023


Exiting JupyterLab
• Make sure you save your file before closing your tab
• Note: even if you close your JupyterLab tab, Python will still be
running in the background
• In fact, if you copy and paste your JupyterLab’s browser window URL
“localhost:XXXX/lab” (XXXX typically is 8888, 8889 and so on) in a different
tab, you can reload your JupyterLab interface after you’ve closed it (but only
the last version of your notebook that was saved will load)
• To fully quit, close your tab and exit out of Anaconda Navigator and
select “jupyterlab” in the “Close running applications window” when
prompted.

© Chris Waltham, UBC Physics & Astronomy 2023


Quality of life tips
• You can change the JupyterLab interface to dark-mode (Settings >
Themes)
• Advanced:
• Learn to navigate JupyterLab with shortcuts, it makes usage much faster.
• My favourites, in “command” mode:
• “a” to create new cell above The “edit” mode is where you type and enter
• “b” to create new cell below stuff in the cells (like we’ve done above) and
the “command” mode is where you can
• “dd” to delete your cell
operate on entire cells, instead of “inside”
• “x” to cut your cell them. To enter “command” mode, simply
• “c” to copy your cell press “ESC”: you will notice that your cursor
• “v” to paste your cell disappears from the cell

© Chris Waltham, UBC Physics & Astronomy 2023


An aside on physics labs…
• You will also be using JupyterLab and Python in your physics lab: so
whatever you learn in Science One Python activities will be useful
there (and vice-versa).
• Note that the physics labs are run independently from Science One
and use a cloud based version of JupyterLab (with special custom
packages that are no publicly available).
• This means you won’t be able to use your local installation of JupyterLab that
you just painstakingly set up here for your physics labs. This is a bit annoying,
but just the way it is for now.
• In Science One, you need a local installation of Python and JupyterLab to use
your Arduino boards: there is no avoiding it!

© Chris Waltham, UBC Physics & Astronomy 2023


Fun challenge
• Bored but want to keep exploring?
• I (Jonathan) learned a lot of my basic Python skills by giving myself
some fun little math puzzles to solve. I found
https://projecteuler.net/ particularly useful and fun! Try the first
problem for yourself!

© Chris Waltham, UBC Physics & Astronomy 2023

You might also like