You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
13 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. \documentclass[letterpaper, 10pt, twoside]{article}
  2. % Packages without options %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. \usepackage{lipsum}
  4. \usepackage{verbatim} % Better code blocks
  5. \usepackage{fancyvrb} % Even better code blocks
  6. \usepackage{fvextra} % Even more better code blocks
  7. \usepackage{enumitem} % Control spacing inside list environments
  8. \usepackage{microtype} % Better typography
  9. \usepackage{wrapfig} % Allows us to use \wrapfigure command
  10. \usepackage{adjustbox} % Adjust padding inside \minipage environment
  11. \usepackage{tikz} % Draw pictures
  12. % Packages with options %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  13. \usepackage{multicol} % Most of our text will have two columns
  14. \setlength\columnsep{25pt}
  15. \usepackage{fancyhdr} % Granular control over headers and footers
  16. \pagestyle{fancy} % Define pagesyle «fancy»
  17. \fancyhf{} % Clear existing header/footer entries
  18. \fancyhead[LE, RO]{\thepage} % Page number is always on the outside
  19. \fancyhead[CE, CO]{Notes on Python} % Title is always in the middle
  20. \fancyhfoffset[LE,RO]{0in} % Adjust header and footer width to match text width
  21. \renewcommand{\headrulewidth}{0.5pt}
  22. \usepackage{geometry} % Adjust margins
  23. \geometry{ % Package options
  24. bmargin=0.75in, % bottom margin
  25. tmargin=1in, % top margin
  26. hmargin=0.75in, % horizontal margin (both sides)
  27. bindingoffset=0.25in % Just in case this is printed and bound
  28. }
  29. \usepackage[nottoc,numbib]{tocbibind} % Add references to TOC
  30. \usepackage[hidelinks]{hyperref} % Include URLs
  31. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  32. % My macros %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  33. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  34. % Make a nice border and box for the tops of our examples
  35. \newcommand\klab[3]{\vspace{#1}\noindent{}\hrulefill~~\fbox{\texttt{~#2~}}~~\hrulefill\vspace{#3}}
  36. % Add an \hrule with space above and below
  37. \newcommand\krule[2]{\vspace{#1}\hrule\vspace{#2}}
  38. % Make hrefs easier (must load package hyperref}
  39. \newcommand\kref[2]{\href{#1}{{\texttt{#2}}}}
  40. % Make some call outs
  41. \newcommand\kpull[2]{
  42. \setlength{\intextsep}{3.0pt plus 2.0pt minus 2.0pt}
  43. \setlength{\columnsep}{10pt}
  44. \begin{wrapfigure}[]{r}[8pt]{0pt}
  45. \fbox{
  46. \adjustbox{padding*=0pt 2pt 0pt 1pt}{
  47. \begin{minipage}{#2} % This is the width of the call out
  48. \begin{small}
  49. \begin{flushleft}
  50. {#1} % This is the contents of the call out
  51. \end{flushleft}
  52. \end{small}
  53. \end{minipage}
  54. } % End adjustbox
  55. } % End fbox
  56. \end{wrapfigure}
  57. } % The width variable comes after the contents variable because the contents should give you some idea of how wide to make this.
  58. % And make them work nicely with out multicol environment
  59. % Not quite sure if commented lines are needed. More testing is required.
  60. %\def\ksep{25pt}
  61. \newcommand\kcolb{
  62. % \setlength{\columnsep}{\ksep}
  63. \begin{multicols}{2}
  64. }
  65. \newcommand\kcole{
  66. \end{multicols}
  67. }
  68. % Add an indent to bibliography items
  69. % This is a bit of a kludge, but it works. Look for a best practice to replace this.
  70. \newcommand\kbib{
  71. \begin{tikzpicture} \draw [white, line width=6] (0,0) -- (0.1,0); \end{tikzpicture}
  72. }
  73. % Figure out how to add section symbol
  74. \newcommand\ksec{$\S$}
  75. % Additional options %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  76. \raggedbottom
  77. % Title info %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  78. \title{\textbf{Notes on Python}}
  79. \author{Kenneth John Odle\\ {\small \texttt{ken at kjodle dot net}}}
  80. \date{2023\\ \medskip v. 0.0.1}
  81. % Notes to self %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  82. % Make sure that \section{} is OUTside the multicols environment
  83. % Make sure that \subsection{} is INside the multilcols environment
  84. % If needed, add \newpage before a \section if the placement of the heading is weird
  85. \begin{document}
  86. \maketitle
  87. \begin{abstract}
  88. 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.
  89. This document was typeset in \LaTeX{}.
  90. 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}.
  91. \end{abstract}
  92. \tableofcontents
  93. \newpage
  94. \section{Preliminaries}
  95. \kcolb
  96. \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}''.
  97. \subsection{What is Python?}\label{what-is-python}
  98. To quote from the Python Software Foundation:\cite{pythonorg:blurb}
  99. \begin{quote}
  100. \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.}
  101. \end{quote}
  102. \subsection{Why use Python?}\label{why-use-python}
  103. 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.
  104. 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.
  105. \paragraph{Functional} Every statement is a kind of math equation. Functional programming is good for use in parallel processing activities.
  106. \paragraph{Imperative} Computations occur as changes to program state. This is most often used for manipulating data structures.
  107. \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.
  108. \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.
  109. \bigskip
  110. Many fields depend on Python to process data.
  111. 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.
  112. \subsection{IDEs and Python}\label{IDEs}
  113. 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}
  114. 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.)
  115. \subsection{Version 2 versus Version 3}\label{2vs3}
  116. 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.
  117. Python 3 was released in December 2008 and is a drastically different language than Python 2. Among other things:
  118. \begin{enumerate}[noitemsep]
  119. \item Python 3 stores strings as Unicode, whereas Python 2 stores them as ASCII.
  120. \item Python 3 considers ``print'' to be a function, whereas Python 2 considers it to be a statement.
  121. \item Python 3's syntax is simpler.
  122. \item Python 3 has improved performance.
  123. \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$).
  124. \item Most of the new libraries for Python 3 cannot be used in Python 2.
  125. \end{enumerate}
  126. 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.
  127. \subsection{Comments in Python}\label{comments-in-python}
  128. Python supports single line comments by adding a hashtag (\texttt{\#}) to the beginning of each line:
  129. \begin{Verbatim}
  130. # This line is a comment.
  131. \end{Verbatim}
  132. 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:
  133. \begin{Verbatim}[frame=lines, framesep=3mm, breaklines=true, label=Multiline Comments]
  134. """
  135. These lines
  136. will be ignored by Python
  137. """
  138. '''
  139. These lines will also
  140. be ignored by Python
  141. '''
  142. \end{Verbatim}
  143. 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.
  144. \subsubsection{Uses of Comments}\label{uses-of-comments}
  145. Comments are highly useful when writing code and often underutilized or poorly utilized. It has been suggested\cite{denker-j} that
  146. \begin{quote}
  147. \rule{\linewidth}{0.5pt}
  148. software = code + documentation
  149. \vspace{-12pt}
  150. \rule{\linewidth}{0.5pt}
  151. \end{quote}
  152. \bigskip
  153. That documentation is both external (i.e., user manuals) and internal (i.e., comments). You can (and should) use comments to:
  154. \begin{itemize}[noitemsep]
  155. \item Remind yourself what the code does and why you wrote it.
  156. \item Record who wrote the code in a multi-person project.
  157. \item Tell others how to maintain the code.
  158. \item Make your code accessible to other developers.
  159. \item List ideas for future updates.
  160. \item Document the sources you used to build the code.
  161. \item Maintain a list of improvements you've made.
  162. \end{itemize}
  163. 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.
  164. \subsection{Executing Python Code}\label{exec-python}
  165. \kcole
  166. \newpage
  167. \section{Python Basics}
  168. \kcolb
  169. \subsection{Python Data Types}
  170. \kcole
  171. % References page begins below %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  172. \newpage
  173. \begin{thebibliography}{XX}
  174. \kcolb
  175. \bibitem{denker-j}
  176. Denker, John S.
  177. ``Suggestions for Writing Good Software''.\\
  178. \kbib\kref{https://www.av8n.com/computer/htm/good-software.htm}{https://www.av8n.com/computer/htm/good-\\software.htm}.
  179. \bibitem{mueller-jp:23}
  180. Mueller, John Paul.
  181. \textit{Beginning Programming with Python for Dummies}.
  182. John Wiley \& Sons Inc. (United States: New Jersey: Hoboken).
  183. 3rd Edition,
  184. 2023.\\
  185. \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.
  186. \bibitem{programiz:ides}
  187. programiz.com.
  188. ``Python IDEs and Code Editors''.\\
  189. \kbib\kref{https://www.programiz.com/python-programming/ide}{https://www.programiz.com/python-\\programming/ide}
  190. \bibitem{pythonorg:beginnersguide}
  191. python.org
  192. ``Beginner's Guide to Python'',
  193. \\
  194. \kbib\kref{https://wiki.python.org/moin/BeginnersGuide}{https://wiki.python.org/moin/\\BeginnersGuide}\\
  195. \bibitem{pythonorg:main}
  196. python.org.
  197. ``Python for Beginners'',\\
  198. \kbib\kref{https://www.python.org/about/gettingstarted/}{https://www.python.org/about/\\gettingstarted/}\\
  199. \kbib The official site of the Python Software Foundation.
  200. \bibitem{pythonorg:periodicals}
  201. pythong.org. ``PythonPeriodicals'', \\
  202. \kbib\kref{https://wiki.python.org/moin/PythonPeriodicals}{https://wiki.python.org/moin/\\PythonPeriodicals}
  203. \bibitem{pythonorg:blurb}
  204. python.org.
  205. ``What is Python? Executive Summary'',\\
  206. \kbib\kref{https://www.python.org/doc/essays/blurb/}{https://www.python.org/doc/essays/blurb/}\\
  207. \kbib The official description of what Python is.
  208. \bibitem{realpython:ide}
  209. realpython.com.
  210. ``Python IDEs and Code Editors (Guide), \\
  211. \kbib\kref{https://realpython.com/python-ides-code-editors-guide/}{https://realpython.com/python-ides-code-\\editors-guide/}
  212. \kcole
  213. \end{thebibliography}
  214. \end{document}