301 lines
13 KiB
TeX
301 lines
13 KiB
TeX
\documentclass[letterpaper, 10pt, twoside]{article}
|
|
|
|
% Packages without options %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
\usepackage{lipsum}
|
|
\usepackage{verbatim} % Better code blocks
|
|
\usepackage{fancyvrb} % Even better code blocks
|
|
\usepackage{fvextra} % Even more better code blocks
|
|
\usepackage{enumitem} % Control spacing inside list environments
|
|
\usepackage{microtype} % Better typography
|
|
\usepackage{wrapfig} % Allows us to use \wrapfigure command
|
|
\usepackage{adjustbox} % Adjust padding inside \minipage environment
|
|
\usepackage{tikz} % Draw pictures
|
|
|
|
% Packages with options %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
\usepackage{multicol} % Most of our text will have two columns
|
|
\setlength\columnsep{25pt}
|
|
|
|
\usepackage{fancyhdr} % Granular control over headers and footers
|
|
\pagestyle{fancy} % Define pagesyle «fancy»
|
|
\fancyhf{} % Clear existing header/footer entries
|
|
\fancyhead[LE, RO]{\thepage} % Page number is always on the outside
|
|
\fancyhead[CE, CO]{Notes on Python} % Title is always in the middle
|
|
\fancyhfoffset[LE,RO]{0in} % Adjust header and footer width to match text width
|
|
\renewcommand{\headrulewidth}{0.5pt}
|
|
|
|
\usepackage{geometry} % Adjust margins
|
|
\geometry{ % Package options
|
|
bmargin=0.75in, % bottom margin
|
|
tmargin=1in, % top margin
|
|
hmargin=0.75in, % horizontal margin (both sides)
|
|
bindingoffset=0.25in % Just in case this is printed and bound
|
|
}
|
|
|
|
\usepackage[nottoc,numbib]{tocbibind} % Add references to TOC
|
|
|
|
\usepackage[hidelinks]{hyperref} % Include URLs
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
% My macros %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Make a nice border and box for the tops of our examples
|
|
\newcommand\klab[3]{\vspace{#1}\noindent{}\hrulefill~~\fbox{\texttt{~#2~}}~~\hrulefill\vspace{#3}}
|
|
|
|
% Add an \hrule with space above and below
|
|
\newcommand\krule[2]{\vspace{#1}\hrule\vspace{#2}}
|
|
|
|
% Make hrefs easier (must load package hyperref}
|
|
\newcommand\kref[2]{\href{#1}{{\texttt{#2}}}}
|
|
|
|
% Make some call outs
|
|
\newcommand\kpull[2]{
|
|
\setlength{\intextsep}{3.0pt plus 2.0pt minus 2.0pt}
|
|
\setlength{\columnsep}{10pt}
|
|
\begin{wrapfigure}[]{r}[8pt]{0pt}
|
|
\fbox{
|
|
\adjustbox{padding*=0pt 2pt 0pt 1pt}{
|
|
\begin{minipage}{#2} % This is the width of the call out
|
|
\begin{small}
|
|
\begin{flushleft}
|
|
{#1} % This is the contents of the call out
|
|
\end{flushleft}
|
|
\end{small}
|
|
\end{minipage}
|
|
} % End adjustbox
|
|
} % End fbox
|
|
\end{wrapfigure}
|
|
} % The width variable comes after the contents variable because the contents should give you some idea of how wide to make this.
|
|
|
|
% And make them work nicely with out multicol environment
|
|
% Not quite sure if commented lines are needed. More testing is required.
|
|
%\def\ksep{25pt}
|
|
\newcommand\kcolb{
|
|
% \setlength{\columnsep}{\ksep}
|
|
\begin{multicols}{2}
|
|
}
|
|
\newcommand\kcole{
|
|
\end{multicols}
|
|
}
|
|
|
|
% Add an indent to bibliography items
|
|
% This is a bit of a kludge, but it works. Look for a best practice to replace this.
|
|
\newcommand\kbib{
|
|
\begin{tikzpicture} \draw [white, line width=6] (0,0) -- (0.1,0); \end{tikzpicture}
|
|
}
|
|
|
|
% Figure out how to add section symbol
|
|
\newcommand\ksec{$\S$}
|
|
|
|
|
|
% Additional options %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
\raggedbottom
|
|
|
|
% Title info %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
\title{\textbf{Notes on Python}}
|
|
\author{Kenneth John Odle\\ {\small \texttt{ken at kjodle dot net}}}
|
|
\date{2023\\ \medskip v. 0.0.1}
|
|
|
|
% Notes to self %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
% Make sure that \section{} is OUTside the multicols environment
|
|
% Make sure that \subsection{} is INside the multilcols environment
|
|
% If needed, add \newpage before a \section if the placement of the heading is weird
|
|
|
|
\begin{document}
|
|
|
|
\maketitle
|
|
|
|
\begin{abstract}
|
|
This document consists of notes on Python, gleaned from various sources. A list of references (often with commentary) follows. If you are in a hurry, read the \texttt{tl;dr:} at the beginning of each section.
|
|
|
|
This document was typeset in \LaTeX{}.
|
|
|
|
The latest version of this document can be found at \kref{https://git.kjodle.net/kjodle/Notes-on-Python}{https://git.kjodle.net/kjodle/Notes-on-Python}.
|
|
\end{abstract}
|
|
|
|
\tableofcontents
|
|
|
|
\newpage
|
|
|
|
\section{Preliminaries}
|
|
|
|
\kcolb
|
|
|
|
\noindent{}\texttt{tl;dr:} Use Python 3 (not 2), read \ksec\ref{comments-in-python}: ``\nameref{comments-in-python}'' and execute your python scripts from the command line as described in \ksec\ref{exec-python}: ``\nameref{exec-python}''.
|
|
|
|
\subsection{What is Python?}\label{what-is-python}
|
|
|
|
To quote from the Python Software Foundation:\cite{pythonorg:blurb}
|
|
|
|
\begin{quote}
|
|
\textit{Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.}
|
|
\end{quote}
|
|
|
|
|
|
|
|
\subsection{Why use Python?}\label{why-use-python}
|
|
|
|
I won't bore you with details. If you have made enough effort to find this document, you are probably aware of the importance of the Python programming language. If not, however, I'll give a brief summary.
|
|
|
|
Python is platform independent. It emphasizes a concise syntax that aids in code readability. It also supports multiple coding styles, such as functional, imperative, object-oriented, and procedural.
|
|
|
|
\paragraph{Functional} Every statement is a kind of math equation. Functional programming is good for use in parallel processing activities.
|
|
|
|
\paragraph{Imperative} Computations occur as changes to program state. This is most often used for manipulating data structures.
|
|
|
|
\paragraph{Object-Oriented} This programming style uses objects to model the real world. It isn't fully implemented in Python because Python doesn't support features like data hiding.
|
|
|
|
\paragraph{Procedural} Tasks proceed one step at a time. Most often used for iteration, sequencing, selection, and modularization. It is the simplest form of programming.
|
|
|
|
\bigskip
|
|
|
|
Many fields depend on Python to process data.
|
|
|
|
Mueller\cite{mueller-jp:23} has many reasons why a knowledge of Python may be useful in a job search. They were fairly accurate as of their publication (2023),but things change quickly, so a keyword search of your preferred job boards is always in order.
|
|
|
|
\subsection{IDEs and Python}\label{IDEs}
|
|
|
|
An integrated development environment (IDE) is an application that enables you to write code, debug code, and often compile it and execute it. On Linux systems, Python typically comes bundled with IDLE, although others are available. \cite{programiz:ides}\cite{realpython:ide}
|
|
|
|
The broader question ``should you use an IDE?'' is not as easy to answer. There are reasons IDEs exist, and if you are working on a large project, they can help to speed up the process. However, when you are first starting with Python, it is probably going to be more enlightening to you to use Python directly from the command line, as you will have a better feel for what Python is doing behind the scenes when you eventually do move over to an IDE. (See the section ``\nameref{exec-python}'' below.)
|
|
|
|
\subsection{Version 2 versus Version 3}\label{2vs3}
|
|
|
|
The initial version of Python 2 was released in 2000. Python 2.7, which was released in 2010, is the last version of Python 2. Many people continue to use Python 2, but they shouldn't, and neither should you.
|
|
|
|
Python 3 was released in December 2008 and is a drastically different language than Python 2. Among other things:
|
|
|
|
\begin{enumerate}[noitemsep]
|
|
\item Python 3 stores strings as Unicode, whereas Python 2 stores them as ASCII.
|
|
\item Python 3 considers ``print'' to be a function, whereas Python 2 considers it to be a statement.
|
|
\item Python 3's syntax is simpler.
|
|
\item Python 3 has improved performance.
|
|
\item The result of integer division in Python 3 is a float value (i.e., $5\div2=2.5$) whereas in Python 2 it was an integer value which truncated decimals (i.e., $5\div2=2$).
|
|
\item Most of the new libraries for Python 3 cannot be used in Python 2.
|
|
\end{enumerate}
|
|
|
|
Most importantly, the Python project stopped supporting Python 2 after 31 December 2019, meaning \kpull{\textit{Use Python 3!}}{21mm} that it is not getting bug fixes or security updates. Anything written in Python 2 is not as efficient, and definitely not as secure, as anything written in Python 3.
|
|
|
|
\subsection{Comments in Python}\label{comments-in-python}
|
|
|
|
Python supports single line comments by adding a hashtag (\texttt{\#}) to the beginning of each line:
|
|
|
|
\begin{Verbatim}
|
|
# This line is a comment.
|
|
\end{Verbatim}
|
|
|
|
Mulitline comments are not directly supported by Python. However, you can surround groups of lines to be commented out by using three double quotations marks or three single quotations marks on the lines before and after them:
|
|
|
|
\begin{Verbatim}[frame=lines, framesep=3mm, breaklines=true, label=Multiline Comments]
|
|
"""
|
|
These lines
|
|
will be ignored by Python
|
|
"""
|
|
|
|
'''
|
|
These lines will also
|
|
be ignored by Python
|
|
'''
|
|
\end{Verbatim}
|
|
|
|
The main difference between single line comments and multiline comments is that single line comments do not get exported to reports, whereas multiline comments do. Again, this is because Python does not fully support multiline commenting.
|
|
|
|
\subsubsection{Uses of Comments}\label{uses-of-comments}
|
|
|
|
Comments are highly useful when writing code and often underutilized or poorly utilized. It has been suggested\cite{denker-j} that
|
|
|
|
\begin{quote}
|
|
\rule{\linewidth}{0.5pt}
|
|
software = code + documentation
|
|
\vspace{-12pt}
|
|
\rule{\linewidth}{0.5pt}
|
|
\end{quote}
|
|
|
|
\bigskip
|
|
|
|
That documentation is both external (i.e., user manuals) and internal (i.e., comments). You can (and should) use comments to:
|
|
|
|
\begin{itemize}[noitemsep]
|
|
\item Remind yourself what the code does and why you wrote it.
|
|
\item Record who wrote the code in a multi-person project.
|
|
\item Tell others how to maintain the code.
|
|
\item Make your code accessible to other developers.
|
|
\item List ideas for future updates.
|
|
\item Document the sources you used to build the code.
|
|
\item Maintain a list of improvements you've made.
|
|
\end{itemize}
|
|
|
|
You can, of course, also use comments to prevent certain lines of code from executing, a practice known as ``commenting out''. This is useful when troubleshooting your code.
|
|
|
|
\subsection{Executing Python Code}\label{exec-python}
|
|
|
|
\kcole
|
|
|
|
\newpage
|
|
\section{Python Basics}
|
|
|
|
\kcolb
|
|
|
|
\subsection{Python Data Types}
|
|
|
|
\kcole
|
|
|
|
% References page begins below %%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
\newpage
|
|
|
|
\begin{thebibliography}{XX}
|
|
|
|
\kcolb
|
|
|
|
\bibitem{denker-j}
|
|
Denker, John S.
|
|
``Suggestions for Writing Good Software''.\\
|
|
\kbib\kref{https://www.av8n.com/computer/htm/good-software.htm}{https://www.av8n.com/computer/htm/good-\\software.htm}.
|
|
|
|
\bibitem{mueller-jp:23}
|
|
Mueller, John Paul.
|
|
\textit{Beginning Programming with Python for Dummies}.
|
|
John Wiley \& Sons Inc. (United States: New Jersey: Hoboken).
|
|
3rd Edition,
|
|
2023.\\
|
|
\kbib{}This book \textit{heavily} emphasizes the use of Google Colab. It's basically an advertisement for the $\Gamma$oogle and \textit{highly} disappointing. Python is meant to be fairly universal and this book's approach seems to work counter to that intention.
|
|
|
|
\bibitem{programiz:ides}
|
|
programiz.com.
|
|
``Python IDEs and Code Editors''.\\
|
|
\kbib\kref{https://www.programiz.com/python-programming/ide}{https://www.programiz.com/python-\\programming/ide}
|
|
|
|
\bibitem{pythonorg:beginnersguide}
|
|
python.org
|
|
``Beginner's Guide to Python'',
|
|
\\
|
|
\kbib\kref{https://wiki.python.org/moin/BeginnersGuide}{https://wiki.python.org/moin/\\BeginnersGuide}\\
|
|
|
|
\bibitem{pythonorg:main}
|
|
python.org.
|
|
``Python for Beginners'',\\
|
|
\kbib\kref{https://www.python.org/about/gettingstarted/}{https://www.python.org/about/\\gettingstarted/}\\
|
|
\kbib The official site of the Python Software Foundation.
|
|
|
|
\bibitem{pythonorg:periodicals}
|
|
pythong.org. ``PythonPeriodicals'', \\
|
|
\kbib\kref{https://wiki.python.org/moin/PythonPeriodicals}{https://wiki.python.org/moin/\\PythonPeriodicals}
|
|
|
|
\bibitem{pythonorg:blurb}
|
|
python.org.
|
|
``What is Python? Executive Summary'',\\
|
|
\kbib\kref{https://www.python.org/doc/essays/blurb/}{https://www.python.org/doc/essays/blurb/}\\
|
|
\kbib The official description of what Python is.
|
|
|
|
\bibitem{realpython:ide}
|
|
realpython.com.
|
|
``Python IDEs and Code Editors (Guide), \\
|
|
\kbib\kref{https://realpython.com/python-ides-code-editors-guide/}{https://realpython.com/python-ides-code-\\editors-guide/}
|
|
|
|
\kcole
|
|
|
|
\end{thebibliography}
|
|
|
|
\end{document} |