|
13 | 13 |
|
14 | 14 | \python\ only has one integer type, called \pythonil{int}.
|
15 | 15 | This type has basically an unbounded range.
|
16 |
| -The \python\ interpreter will allocate as much memory as is needed to store the number you want. |
17 |
| -(Ok, so the range is not actually \emph{unbounded}, it is bounded by the amount of memory available on your computer{\dots} {\dots}but for all intents and purposes within this book, we can assume that~$\pythonil{int}\equiv\integerNumbers$.) |
18 |
| - |
| 16 | +The \python\ interpreter will allocate as much memory as is needed to store the number you want.\footnote{% |
| 17 | +Ok, the range is not actually \emph{unbounded}, it is bounded by the amount of memory available on your computer{\dots} {\dots}but for all intents and purposes within this book, we can assume that~$\pythonil{int}\equiv\integerNumbers$.}% |
| 18 | +% |
| 19 | +\hsection{Integer Arithmetics}% |
19 | 20 | Now, what can we do with integer numbers?
|
20 | 21 | We can add, subtract, multiply, divide, modulo divide, and raise them to powers.
|
21 | 22 |
|
22 | 23 | \begin{figure}%
|
23 | 24 | \centering%
|
24 |
| -\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleA}% |
25 |
| -\caption{Examples for \python\ integer math in the console, part~1 (see \cref{fig:pythonIntMathInConsoleB} for part~2).}% |
26 |
| -\label{fig:pythonIntMathInConsoleA}% |
| 25 | +\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleArith}% |
| 26 | +\caption{Examples for \python\ integer math in the console, part~1 (see \cref{fig:pythonIntMathInConsolePower} for part~2).}% |
| 27 | +\label{fig:pythonIntMathInConsoleArith}% |
27 | 28 | \end{figure}%
|
28 | 29 |
|
29 |
| -In \cref{fig:pythonIntMathInConsoleA}, you can find some examples for this. |
| 30 | +In \cref{fig:pythonIntMathInConsoleArith}, you can find some examples for this. |
30 | 31 | Like back in \cref{sec:terminalConsolem}, press \ubuntuTerminal\ under \ubuntu\ \linux\ or \windowsTerminal\ under \windows\ to open a \pgls{terminal}.
|
31 | 32 | After entering \bashil{python3} and hitting \keys{\enter}, we can begin experimenting with integer maths.
|
32 | 33 | The lines with \python\ commands in the console begin with \pythonil{>>>}, whereas the result is directly output below them without prefix string.
|
33 | 34 |
|
34 |
| -In the very first line of \cref{fig:pythonIntMathInConsoleA}, we enter \pythonil{4 + 3} and hit \keys{\enter}. |
| 35 | +In the very first line of \cref{fig:pythonIntMathInConsoleArith}, we enter \pythonil{4 + 3} and hit \keys{\enter}. |
35 | 36 | The result is displayed on the next line and, as expected, is \pythonil{7}.
|
36 | 37 | We then attempt to multiply the two integers \pythonil{7} and \pythonil{5} by typing \pythonil{7 * 5} and hitting \keys{\enter}.
|
37 | 38 | The result is \pythonil{35}.
|
|
61 | 62 |
|
62 | 63 | \begin{figure}%
|
63 | 64 | \centering%
|
64 |
| -\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleB}% |
65 |
| -\caption{Examples for \python\ integer math in the console, part~2 (see \cref{fig:pythonIntMathInConsoleA} for part~1).}% |
66 |
| -\label{fig:pythonIntMathInConsoleB}% |
| 65 | +\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsolePower}% |
| 66 | +\caption{Examples for \python\ integer math in the console, part~2 (see \cref{fig:pythonIntMathInConsoleArith} for part~1).}% |
| 67 | +\label{fig:pythonIntMathInConsolePower}% |
67 | 68 | \end{figure}%
|
68 | 69 |
|
69 |
| -As you will find in \cref{fig:pythonIntMathInConsoleB}, integers can also be raised to a power. |
| 70 | +As you will find in \cref{fig:pythonIntMathInConsolePower}, integers can also be raised to a power. |
70 | 71 | For example, $2^7$~is expressed as \pythonil{2 ** 7} in \python\ (and yields~\pythonil{128}).
|
71 | 72 | \pythonil{7 ** 11}, i.e., $7^{11}$ gives us~1\decSep977\decSep326\decSep743 and shows as \pythonil{1977326743} in the output.
|
72 | 73 |
|
|
76 | 77 | In \python\, we can compute $2^{63}$~(\pythonil{2 ** 63}), namely 9\decSep223\decSep372\decSep036\decSep854\decSep775\decSep808, and
|
77 | 78 | $2^{64}$~(\pythonil{2 ** 64}), which is~18\decSep446\decSep744\decSep073\decSep709\decSep551\decSep616.
|
78 | 79 | These are very large numbers and the latter one would overflow the range of the standard integer types of \pgls{Java} and \pgls{C}.
|
79 |
| -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:pythonIntMathInConsoleB}! |
| 80 | +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}! |
80 | 81 | \python\ integers are basically unbounded.
|
81 | 82 | However, the larger they get, the more memory they need and the longer it will take to compute with them.
|
82 |
| - |
| 83 | +\endhsection% |
| 84 | +% |
| 85 | +\hsection{Operations on Bit Strings}% |
| 86 | +% |
| 87 | +First time readers are encouraged to skip over this section. |
| 88 | +This section is about integers numbers from the perspective of bit strings and the bit string based operations that we can apply to them. |
| 89 | +If you are learning programming, then this part is not important now. |
| 90 | +You can circle back to it later.% |
| 91 | +% |
83 | 92 | \begin{figure}%
|
84 | 93 | \centering%
|
85 | 94 | \includegraphics[width=0.8\linewidth]{\currentDir/binaryMath}%
|
86 | 95 | \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}).}%
|
87 | 96 | \label{fig:binaryMath}%
|
88 | 97 | \end{figure}
|
| 98 | +\begin{figure}% |
| 99 | +\centering% |
| 100 | +\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleBin}% |
| 101 | +\caption{Examples for the binary representation of integers and the operations that apply to it.}% |
| 102 | +\label{fig:pythonIntMathInConsoleBin}% |
| 103 | +\end{figure} |
89 | 104 |
|
90 |
| -\emph{The following paragraphs is just for the sake of completeness. % |
91 |
| -You do not necessarily need to understand it when reading this book for the first time.} |
92 |
| -As a small excursion for the binary mathematics aficionados: |
93 | 105 | All integer numbers can be represented as bit strings.
|
94 | 106 | 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.
|
95 | 107 | Then, $\dots b_4 b_3 b_2 b_1 b_0$ is a bit string.
|
96 | 108 | If we represent integers with such strings of five bits, then the number~1 would have representation~\texttt{00001}, because it is equivalent to~$2^0$.
|
97 | 109 | 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$.
|
98 |
| -In \python, we can compute the bitwise (i.e., binary) \emph{or}, \emph{and}, as well as the and \emph{exclusive~or} of this binary representation of integers using the \pythonil{|}, \pythonil{&}, and \pythonil{^} operators, respectively. |
99 | 110 |
|
| 111 | +We can obtain the binary representation of integer numbers as text using the \pythonil{bin} function in \python. |
| 112 | +As shown in \cref{fig:pythonIntMathInConsoleBin}, \pythonil{bin(22)} yields \pythonil{'0b10110'}. |
| 113 | +Here, the \pythonil{0b} prefix means that the following number is in binary representation. |
| 114 | +We can also enter numbers in binary representation in the console. |
| 115 | +Typing \pythonil{0b10110} corresponds to the number \pythonil{22}. |
| 116 | +Similarly, \pythonil{bin(15)} yields \pythonil{'0b1111'} and entering \pythonil{0b1111} into the console corresponds to entering the number~\pythonil{15}. |
| 117 | + |
| 118 | +In \python, we can compute the bitwise (i.e., binary) \emph{or}, \emph{and}, as well as the and \emph{exclusive~or} of this binary representation of integers using the \pythonil{|}, \pythonil{&}, and \pythonil{^} operators, respectively. |
100 | 119 | Binary \emph{or} returns an integer in which all bits are set to~1 which were~1 in either of its two operands.
|
101 | 120 | \pythonil{22 | 1} yields \pythonil{23}, because the bit with value~1 is not set in~22 and the binary~\emph{or} sets it (effectively adding~1 to~22).
|
102 | 121 | The slightly more comprehensive example \pythonil{22 | 15} sketched in \cref{fig:binaryMath} gives us \pythonil{31}, because $22=2^4+2^2+2^1$ and $15=2^3+2^2+2^1+2^0$, which \pythonil{|} combines to $31=2^4+2^3+2^2+2^1+2^0$, i.e., each power of~2 that occurred in either of the two operands is present in the result.
|
| 122 | +\pythonil{bin(31)} yields \pythonil{'0b11111'}. |
103 | 123 |
|
104 | 124 | Binary \emph{and} returns the integer where only the bits remain~1 that were~1 in \emph{both} operands.
|
105 | 125 | Applying binary \emph{and} instead of \emph{or}, i.e., doing \pythonil{22 & 1} results in \pythonil{0}, because, as said before, the bit $2^0$ is not set in~22.
|
106 | 126 | \pythonil{22 & 15}, yields~\pythonil{6}, because only $2^2$ and $2^1$ appear both in~22 and~15.
|
| 127 | +Thus, \pythonil{bin(6)} corresponds to \pythonil{'0b110'}. |
107 | 128 |
|
108 | 129 | The \emph{exclusive~or}, which is often called \emph{xor}, will set a bit to~1 if it is~1 in exactly one of the two operands.
|
109 | 130 | Therefore, \pythonil{22 ^ 1} gives~\pythonil{23}, since only the bit with value $2^0$ is set in~1 and the other bits that are~1 in~22 are not.
|
110 | 131 | \pythonil{22 ^ 15} yields \pythonil{25}, because $2^4$, $2^3$, and $2^0$ occur only once in the two operators (whereas $2^2$ and $2^1$ occured in both of them).
|
111 |
| -With this, our excursion into binary maths ends and we now welcome back all first-time readers. |
112 |
| - |
| 132 | +This is confirmed by typing \pythonil{bin(25)}, which results in \pythonil{'0b11001'}. |
| 133 | + |
| 134 | +Finally, we can also shift bit strings to the left or right by $i$~places. |
| 135 | +The former corresponds to multiplying with~$2^i$, the latter is the same as an integer division by~$2^i$. |
| 136 | +Shifting \pythonil{22} by one bit position to the \emph{left} -- which is done by entering \pythonil{22 << 1} -- therefore results in~\pythonil{44}. |
| 137 | +We already know that \pythonil{bin(22)} is \pythonil{'0b10110'} and so it comes at no surprise that \pythonil{bin(44)} is \pythonil{'0b101100'} (notice the additional \pythonil{0} that appeared on the right hand side). |
| 138 | +Shifting \pythonil{22} by two bit positions to the \emph{right} -- which is done by entering \pythonil{22 >> 2} -- results in \pythonil{5}. |
| 139 | +The \pythonil{10} on the right hand side of the binary representation disappeared, as \pythonil{bin(5)} is \pythonil{'0b101'}. |
| 140 | + |
| 141 | +Besides the binary representation of integer numbers, which is to the basis~2, there also exists the hexadecimal representation (base~16) and the octal representation (base~7). |
| 142 | +We can obtain the hexadecimal representation of~22 by computing \pythonil{hex(22)} and get \pythonil{0x16}, which corresponds to~$1*16^1+6*1=22$. |
| 143 | +We can also enter hexadecimal numbers in the console like \pythonil{0x16}, which yields~\pythonil{22}. |
| 144 | +The octal representation of~22 is obtained as \pythonil{oct(22)}, which produces \pythonil{0o26}, which, in turn, corresponds to~$2*8^1+6$. |
| 145 | +Similarly, this octal number can be entered as \pythonil{0o26}. |
| 146 | + |
| 147 | +With this, our excursion into binary maths ends and we now welcome back all first-time readers.% |
| 148 | +% |
| 149 | +\endhsection% |
| 150 | +% |
| 151 | +\hsection{Summary}% |
113 | 152 | In conclusion, the integer type \pythonil{int} represents whole numbers.
|
114 | 153 | Integers can be positive or negative or zero.
|
115 | 154 | All the primitive mathematical operations like addition, subtraction, multiplication, and division that you learned in school can be applied to integers.
|
116 | 155 | The normal arithmetic precedence rules (that you also have learned in school) apply.
|
117 | 156 | Parentheses can be used to group operations.
|
118 | 157 | Finally, \python\ also provides the same binary logic operators, working on the bit string representation of integers, that you may or or may not know from other programming languages as well.
|
119 |
| -% |
| 158 | +\endhsection% |
120 | 159 | \endhsection%
|
121 | 160 | %
|
0 commit comments