2.1: Introduction to Python
- Page ID
- 358423
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Python
Python is an open source object-oriented programming language. It is interpreted in contrast to compiled, which means you can write some code and run it from the command line or an appropriate editor, and the program compiles the line of script you run into to machine readable language as you execute the code, without your having to convert the entire cscriot to machine readable code. In a compiled program, the computer is giving the entire script as machine readable code from the get go. The advantage of a compiled program is that it is "closer" to the operating system and so you can control things at a very granular level, like how much memory is allocated to a task.
We will run Python scripts in two different ways.
- From the command line
- From and an Integrated Development Environment (IDE)
Raspberry Pi (RPi) uses Linux as an operating system and Bash is the default interpreter, and we will also have lessons on both Bash scripts. We will develop Python code in the Thonny IDE that you will install in the next section of this lesson plan. We are choosing to use Thonny as it is also installed on your RPi, and so you can write code on your personal computer and then then transfer it to the RPi (or vice versa). A Python interpreter is also bundled into the Thonny installation and so after you instal Thonny you can instantly start writing Python code
Python is only part of the IOST ecosystem and there are many other programs that you will want to use as you advance in your studies of digital sciences. After you install Thonny we will a second version of Python using the miniconda version of the Anaconda data science platform (see below). We will then redirect Thonny to use the miniconda python interpreter in contrast to the default version. This will not only allow you to add additional dat science packages but assist you in updating and maintaining your Python installation.
Thonny
If you choose to use Thonny go to https://thonny.org and install the software apropriate for your operating system. As of January 2022 this will be version 3.3.12 and it will also instal Python 3.7. If you need help, this Youtube video quickly goes over the process (https://youtu.be/TlvQOWhlfpo).
UI Modes
Thonny has three UI (User Interface) modes, Simple, Regular and Expert. We will typically use the Simple or Regular mode. On your PC you will probably want Regular as it has access to the file menu (fig. \(\PageIndex{2}\)), but if you have limited screen space, like are on a Pi with a low resolution monitor, you may wish to use simple mode to maximize the screen's realestate.

To switch to REGULAR mode you can click the text in the circle of the upper right in figure \(\PageIndex{1}\). To switch from regular mode to simplemode go to tools/options/general/UI mode and pick the option you want (figure \(\PageIndex{2}\)). Also, note that the blue oblong circle on the shell of figure \(\PageIndex{1}\) shows that Thonny is running python 3.7.9, which was co-installed with Thonny, but in \(\PageIndex{2}\) we can see that Python 3.9.7 is being run. That is, these images were taken when we were using different versions of Python, and in the next section you will install Python

To switch from regular mode to simple mode choose Tools/General/UI mode/regular (Figure \(\PageIndex{2}\)). We will typically use the Regular mode.
Menu Items
When in Regular mode you have access to several menu items and it is prudent to go over these before starting. Many of these like open and save will be familiar to you while others will be unfamiliar. It is always a good idea to check out menu items on any software package before you start using it.

We will take a quick dive on several of the menu item features that your are probably not familiar with.
Edit
In some IDEs you need to save a file before you run it. In Thonny you do not, and the moment you hit "RUN" it saves the file and then runs it. This means it overwrites the code of the original file. So, a common error is to change some code and run it to see if it works, but you then lose the original code. A good practice is to comment out code with the # symbol. This means that text will not be operated on. In the Edit menu you have the ability to comment or uncomment large blocks.

Note, the color coding above is a result of the option chosen in the tools/options/theme-fonts tab.
View
The View option allows you to select the interfaces you wish to work with. The code editor is always present and you probably want to run the Shell, which emulates a command line interface. In figure (\(\PageIndex{3}\)) the Shell, Variables and Assistant options have been opened. We will go over the utility of these features as the course proceeds, but these are probably the most common ones you will wish to work with.

You will also note that the View menu provides an option to increase or decrease font size with keyboard shortcuts of Ctrl + + and Ctrl + - (click the crtl followed by either plus or minus). This is often very important when you are running a Pi in headless mode using the VNC viewer.
Run
The run tab allows you to execute python code in the editor and interact with it through the shell. You will often want to use the <F5> shortcut key. You can also run the program in "debugger mode" that allows you to walk through the code one step at a time. There are a lot of YouTube videos on the debugger and the following one goes over it (we are starting where the debugger part of the script is, but you may want to watch the entire video.
Tools
Tools is an important menu item, in fact we have already looked at in in figure \(\PageIndex{2}\) when we switched the UI (User Interface) from simple to regular mode. Let's quickly look at some of the other features of the Tools, starting with the manage packages and manage plugins. A Python package is a way to add new features to your program, like say math or time functions, and is essentially a directory of modules that you typically download from a service like the PyPI Python Package Index, that as of 1/22/2022 contains 351,947 projects. A package has a init_.py file sets how the packages are imported, while a directory could be a collection of python modules, but without the init_.py file, they will not be integrated into the interpreter.

We have already looked at the options submenu when we changed the UI (Figure \(\PageIndex{2}\).

PEP 8, the Index of Python Enhancement Proposal style guide suggests a maximum line length of 79 characters and you can set a verticle line in your editor to warn you when your string of code is getting to long (figure \(\PageIndex{7}\)).
Anaconda
Anaconda is a data science platform which allows you to find and install over 7,500 data science and machine learning package and progarams like Python, R, Jupyter, Scikit learn, matplotlib and R, but also includes the conda package manager that helps deal with conflicts across software. There are two installation options, Anaconda and Miniconda. If you are planning to get involved with Data Sciences or have a high quality computer you will probably want to install the full version of Anaconda, but if this class is your only endeavor than you will probably want to install the Miniconda version. Note, the full version comes the Anaconda Navigator which makes it very easy to launch and maintaint packages, where the Miniconda requires you to use to launch programs through the conda prompt. What we are going to do in this class is connect the Thonny IDE to the Python interpreter installed with either Anaconda or Miniconda and use Thonny to launch Python. We are choosing Thonny because it comes installed with the Raspberry Pi.
- Anaconda
- https://www.anaconda.com/
- Well suited for scientific programmings. Comes with ipython and Spyder, and the Conda package management system.
- Has 1500 scientific packages and uses 3GB of disk space
- https://www.anaconda.com/
- Miniconda
- https://docs.conda.io/en/latest/miniconda.html
- Anaconda without all the packages
You are welcome to use the default python interpreter with Thonny of instal it through Anaonda. Figure \(\(PageIndex{8}\) has screen shots for using miniconda.

Once you install miniconda you want to open the Anaconda prompt (miniconda 3) from your Windows start menu, which can be done by typing in the search

to update your packages you want to type the following into the command prompt. You first need to navigate to the correct folder. In the command prompt (which is a terminal shell) type ls to see your folders.
Note <cctrl> C bring back the command prompt if you get stuck
ls

Now move to that directory (Change Directory)
cd .\miniconda3\
When you list the files you will see where you installed python
You will also want to update python
conda update python

Exit the anaconda shell
exit
Now we want to link our Thonny to the new install of python. Open Thonny and go to tools/options/interpreter (figure \(\PageIndex{12}\)) and choose the "Alternative Python 3 interpreter or virtual environment". Initially the python executable will direct to where you installed Thonny and you need to direct it to the new path, which you found above. Or you can click the three dots tothe right of the current path and navigate to where the python application file is located and open it.

Figure \(\PageIndex{12}\): Copy and Paste Caption here. (Copyright; author via source)
If you now look at the shell in Thonny you can see the new updated Python program you are running
