Skip to content

Commit 401eda0

Browse files
committed
move towards listings for interactive python sessions
1 parent 2d9f2c1 commit 401eda0

File tree

6 files changed

+20
-27
lines changed

6 files changed

+20
-27
lines changed

text/main/basics/simpleDataTypesAndOperations/float/float.tex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,11 @@
7373
\hsection{Floating Point Arithmetic}%
7474
\label{sec:floatarith}%
7575
%
76-
\begin{figure}%
77-
\centering%
78-
\includegraphics[width=0.8\linewidth]{\currentDir/floatMathInConsoleArith}%
79-
\caption{Basic arithmetics with floating point numbers in \python.}%
80-
\label{fig:floatMathInConsoleArith}%
81-
\end{figure}%
76+
\gitEvalPython{float_arith}{}{simple_datatypes/float_arith.py}%
77+
\listingBox{exec:float_arith}{Basic arithmetics with floating point numbers in \python.}{,style=python_console_style}%
8278
%
8379
Floating point numbers in \python\ can be distinguished from \pythonils{int}\pythonIdx{int} by having a decimal dot in their text representation, i.e., \pythonil{5.0} is a \pythonilIdx{float} and \pythonil{5} is an \pythonilIdx{int}.
84-
Let us now look at some examples for the basic arithmetic operations available for \pythonils{float}\pythonIdx{float} in \cref{fig:floatMathInConsoleArith}.
80+
Let us now look at some examples for the basic arithmetic operations available for \pythonils{float}\pythonIdx{float} in \cref{exec:float_arith}.
8581

8682
We already learned that the division operator~\pythonilIdx{/} always yields a \pythonilIdx{float} result.
8783
Therefore \pythonil{6 / 3} yields \pythonil{2.0} instead of \pythonil{2}.
Binary file not shown.

text/main/basics/simpleDataTypesAndOperations/int/int.tex

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@
2424
\begin{figure}%
2525
\centering%
2626
\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleArith}%
27-
\caption{Examples for \python\ integer math in the console, part~1 (see \cref{fig:pythonIntMathInConsolePower} for part~2).}%
27+
\caption{Examples of \python\ integer math in the console, part~1 (see \cref{exec:int_math_arith} for part~2).}%
2828
\label{fig:pythonIntMathInConsoleArith}%
2929
\end{figure}%
30+
%
31+
\gitEvalPython{int_math_arith}{}{simple_datatypes/int_math_arith.py}%
32+
\listingBox{exec:int_math_arith}{The same examples of \python\ integer math in the \python\ console as given in \cref{fig:pythonIntMathInConsoleArith}, just as listing instead of screenshot.}{,style=python_console_style}%
3033

31-
In \cref{fig:pythonIntMathInConsoleArith}, you can find some examples for this.
34+
In \cref{fig:pythonIntMathInConsoleArith}, you can find some examples of this.
35+
(The same example is given in \cref{exec:int_math_arith}, just as listing instead of screenshot.
36+
We will use such listings from now on, as they convey the exactly same information, but are easier to read and I can more conveniently include comments.)
3237
Like back in \cref{sec:terminalConsolem}, press \ubuntuTerminal\ under \ubuntu\ \linux\ or \windowsTerminal\ under \microsoftWindows\ to open a \pgls{terminal}.
3338
After entering \bashil{python3} and hitting \keys{\enter}, we can begin experimenting with integer maths.
3439
The lines with \python\ commands in the console begin with \pythonil{>>>}, whereas the result is directly output below them without prefix string.
3540

36-
In the very first line of \cref{fig:pythonIntMathInConsoleArith}, we enter \pythonil{4 + 3}\pythonIdx{+} and hit \keys{\enter}.
41+
In the very first line of \cref{fig:pythonIntMathInConsoleArith,exec:int_math_arith}, we enter \pythonil{4 + 3}\pythonIdx{+} and hit \keys{\enter}.
3742
The result is displayed on the next line and, as expected, is \pythonil{7}.
3843
We then attempt to multiply the two integers \pythonil{7} and \pythonil{5} by typing \pythonil{7 * 5}\pythonIdx{*!multiplication} and hitting \keys{\enter}.
3944
The result is \pythonil{35}.
@@ -65,14 +70,10 @@
6570
The remainder of this operation can be computed using the \pgls{modulodiv} operator \expandafter\pythonilIdx{\%}, i.e., by typing \pythonil{33 \% 4}, which yields~\pythonil{1}.
6671
We also find that \expandafter\pythonil{34 \% 4} yields~\pythonil{2}, \expandafter\pythonil{35 \% 4} gives us~\pythonil{3}, and \expandafter\pythonil{36 \% 4} is~\pythonil{0}.
6772

68-
\begin{figure}%
69-
\centering%
70-
\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsolePower}%
71-
\caption{Examples for \python\ integer math in the console, part~2 (see \cref{fig:pythonIntMathInConsoleArith} for part~1).}%
72-
\label{fig:pythonIntMathInConsolePower}%
73-
\end{figure}%
73+
\gitEvalPython{int_math_power}{}{simple_datatypes/int_math_power.py}%
74+
\listingBox{exec:int_math_power}{Examples of \python\ integer math (powers) in the \python\ console, part~2 (see \cref{exec:int_math_arith} for part~1).}{,style=python_console_style,literate={0}{0\-}1 {1}{1\-}1 {2}{2\-}1 {3}{3\-}1 {4}{4\-}1 {5}{5\-}1 {6}{6\-}1 {7}{7\-}1 {8}{8\-}1 {9}{9\-}1,breakatwhitespace=false,breaklines=true}
7475

75-
As you will find in \cref{fig:pythonIntMathInConsolePower}, integers can also be raised to a power.
76+
As you will find in \cref{exec:int_math_arith}, integers can also be raised to a power.
7677
For example, $2^7$~is expressed as \pythonil{2 ** 7}\pythonIdx{**!power} in \python\ (and yields~\pythonil{128}).
7778
\pythonil{7 ** 11}, i.e., $7^{11}$ gives us~1\decSep977\decSep326\decSep743 and shows as \pythonil{1977326743} in the output.
7879

@@ -82,8 +83,8 @@
8283
In \python\, we can compute $2^{63}$~(\pythonil{2 ** 63}), namely 9\decSep223\decSep372\decSep036\decSep854\decSep775\decSep808, and
8384
$2^{64}$~(\pythonil{2 ** 64}), which is~18\decSep446\decSep744\decSep073\decSep709\decSep551\decSep616.
8485
These are very large numbers and the latter one would overflow the range of the standard integer types of \pgls{Java} and \pgls{C}.
85-
However, we can also keep going and compute \pythonil{2 ** 1024}, which is such a huge number that it wraps four times in the output of our \python\ console in \cref{fig:pythonIntMathInConsolePower}!
86-
\python\ integers are basically unbounded.
86+
However, we can also keep going and compute \pythonil{2 ** 1024}, which is such a huge number that it wraps several times in the output of our \python\ console in \cref{exec:int_math_arith}!
87+
\python\ integers are basically unbounded~(or bounded only by the memory size of our computer).
8788
However, the larger they get, the more memory they need and the longer it will take to compute with them.
8889
\endhsection%
8990
%
@@ -102,12 +103,8 @@
102103
\caption{Examples for how integer numbers are represented as bit strings in the computer (upper part) and for the binary (bitwise) operations \emph{and}, \emph{or}, and \emph{exclusive or} (often called~\emph{xor}).}%
103104
\label{fig:binaryMath}%
104105
\end{figure}
105-
\begin{figure}%
106-
\centering%
107-
\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleBin}%
108-
\caption{Examples for the binary representation of integers and the operations that apply to it.}%
109-
\label{fig:pythonIntMathInConsoleBin}%
110-
\end{figure}
106+
\gitEvalPython{int_math_bin}{}{simple_datatypes/int_math_bin.py}%
107+
\listingBox{exec:int_math_bin}{Examples of the binary representation of integers and the operations that apply to it.}{,style=python_console_style}
111108

112109
All integer numbers can be represented as bit strings.
113110
In other words, a number $z\in\integerNumbers$ can be expressed as $b_0 2^0 + b_1 2^1 + b_2 2^2 + b_3 2^3 + b_4 2^4\dots$, where the $b_i$-values each are either~0 or~1.
@@ -116,7 +113,7 @@
116113
In in \cref{fig:binaryMath}, we illustrate that the number~22 would be \texttt{10110} because $22=2^4+2^2+2^1$ and the number~15 would correspond to \texttt{01111}, as~$15=2^3+2^2+2^1+2^0$.
117114

118115
We can obtain the binary representation of integer numbers as text using the \pythonilIdx{bin} function in \python.
119-
As shown in \cref{fig:pythonIntMathInConsoleBin}, \pythonil{bin(22)} yields \pythonil{'0b10110'}.
116+
As shown in \cref{exec:int_math_bin}, \pythonil{bin(22)} yields \pythonil{'0b10110'}.
120117
Here, the \pythonilIdx{0b} prefix means that the following number is in binary representation.
121118
We can also enter numbers in binary representation in the console.
122119
Typing \pythonil{0b10110} corresponds to the number~\pythonil{22}.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)