Skip to content

Commit 22ab05e

Browse files
committed
added escape sequences glossary entry and minor improvements
1 parent 6e387ca commit 22ab05e

File tree

7 files changed

+163
-18
lines changed

7 files changed

+163
-18
lines changed

text/main/basics/collections/sets/sets.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
This time, we begin by defining a set \pythonil{odd} containing the first eight odd integers.
103103
Sets, like lists and tuples, support both the \pythonilIdx{in}\pythonIdx{set!in} and \pythonilIdx{not in}\pythonIdx{set!not in} operators.
104104
In the context of sets, they are equivalent to the $\in$ and $\notin$ operations from, well, set theory.
105-
We illustrate this in our small example using the unicode escape sequences \pythonil{\\u2208} and \pythonil{\\u2209} for both characters~(see back in \cref{sec:unicodeChars}).
105+
We illustrate this in our small example using the unicode \pglspl{escapeSequence}~\pythonil{\\u2208} and \pythonil{\\u2209} for both characters~(see back in \cref{sec:unicodeChars}).
106106
Both \pythonil{3 in odd} and \pythonil{2 not in odd} are \pythonil{True}.%
107107
%
108108
\begin{sloppypar}%

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@
143143
Shifting \pythonil{22} by two bit positions to the \emph{right} -- which is done by entering \pythonil{22 >> 2}\pythonIdx{>\strut>} -- results in \pythonil{5}.
144144
The \pythonil{10} on the right hand side of the binary representation disappeared, as \pythonil{bin(5)} is \pythonil{'0b101'}.
145145

146-
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).
146+
\hinput{.}{numberTable.tex}
147+
148+
Besides the binary representation of integer numbers, which is to the~base~2, there also exists the hexadecimal representation~(base~16) and the octal representation~(base~7).
147149
We can obtain the hexadecimal representation of~22 by computing \pythonil{hex(22)}\pythonIdx{hex} and get \pythonil{0x16}\pythonIdx{0x}, which corresponds to~$1*16^1+6*1=22$.
148150
We can also enter hexadecimal numbers in the console like \pythonil{0x16}, which yields~\pythonil{22}.
149151
The octal representation of~22 is obtained as \pythonil{oct(22)}\pythonIdx{oct}, which produces \pythonil{0o26}\pythonIdx{0o}, which, in turn, corresponds to~$2*8^1+6$.
150152
Similarly, this octal number can be entered as \pythonil{0o26}.
153+
As a refresher on these numerical systems, we include \cref{tbl:numbersBases}, which illustrates decimal, binary, octal, and hexdecimal notations of the first couple of natural numbers.
151154

152155
You may wonder:~\inQuotes{How does an \pythonil{int} know whether it was entered in hexadecimal, octal, binary, or decimal notation?}
153156
It does not.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
\begin{table}%
2+
\centering%
3+
\setlength{\tabcolsep}{0.2em}%
4+
\caption{A table with number conversions to the decimal~(dec), binary~(bin), octal~(oct), and hexadecimal~(hex) systems. The values of the digits are noted in the second row of the header. Powers of the bases are highlighted.}%
5+
\label{tbl:numbersBases}%
6+
\smallskip%
7+
\resizebox{0.99999\linewidth}{!}{
8+
\begin{tabular}{cc|ccccccc|ccc|cc||cc|ccccccc|ccc|cc}%
9+
\hline%
10+
\multicolumn{2}{c|}{\textbf{dec}}&\multicolumn{7}{c|}{\textbf{bin}}&\multicolumn{3}{c|}{\textbf{oct}}&\multicolumn{2}{c||}{\textbf{hex}}&\multicolumn{2}{c|}{\textbf{dec}}&\multicolumn{7}{c|}{\textbf{bin}}&\multicolumn{3}{c|}{\textbf{oct}}&\multicolumn{2}{c}{\textbf{hex}}\\%
11+
\parbox{2ex}{\centering\noindent10}&\parbox{2ex}{\centering\noindent1}&\parbox{2ex}{\centering\noindent64}&\parbox{2ex}{\centering\noindent32}&\parbox{2ex}{\centering\noindent16}&\parbox{2ex}{\centering\noindent8}&\parbox{2ex}{\centering\noindent4}&\parbox{2ex}{\centering\noindent2}&\parbox{2ex}{\centering\noindent1}&\parbox{2ex}{\centering\noindent64}&\parbox{2ex}{\centering\noindent8}&\parbox{2ex}{\centering\noindent1}&\parbox{2ex}{\centering\noindent16}&\parbox{2ex}{\centering\noindent1}&\parbox{2ex}{\centering\noindent10}&\parbox{2ex}{\centering\noindent1}&\parbox{2ex}{\centering\noindent64}&\parbox{2ex}{\centering\noindent32}&\parbox{2ex}{\centering\noindent16}&\parbox{2ex}{\centering\noindent8}&\parbox{2ex}{\centering\noindent4}&\parbox{2ex}{\centering\noindent2}&\parbox{2ex}{\centering\noindent1}&\parbox{2ex}{\centering\noindent64}&\parbox{2ex}{\centering\noindent8}&\parbox{2ex}{\centering\noindent1}&\parbox{2ex}{\centering\noindent16}&\parbox{2ex}{\centering\noindent1}\\%
12+
\hline%
13+
0&0&0&0&0&0&0&0&0&0&0&0&0&0&\textcolor{red}{\textbf{0}}&\textcolor{red}{\textbf{1}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{1}}&\textcolor{violet}{\textbf{0}}&\textcolor{violet}{\textbf{0}}&\textcolor{violet}{\textbf{1}}&\textcolor{green!80!black}{\textbf{0}}&\textcolor{green!80!black}{\textbf{1}}\\%
14+
\rowcolor{gray!20}0&2&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{1}}&\textcolor{blue}{\textbf{0}}&0&0&2&0&2&0&3&0&0&0&0&0&1&1&0&0&3&0&3\\%
15+
0&4&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{1}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&0&0&4&0&4&0&5&0&0&0&0&1&0&1&0&0&5&0&5\\%
16+
\rowcolor{gray!20}0&6&0&0&0&0&1&1&0&0&0&6&0&6&0&7&0&0&0&0&1&1&1&0&0&7&0&7\\%
17+
0&8&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{1}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{violet}{\textbf{0}}&\textcolor{violet}{\textbf{1}}&\textcolor{violet}{\textbf{0}}&0&8&0&9&0&0&0&1&0&0&1&0&1&1&0&9\\%
18+
\rowcolor{gray!20}\textcolor{red}{\textbf{1}}&\textcolor{red}{\textbf{0}}&0&0&0&1&0&1&0&0&1&2&0&a&1&1&0&0&0&1&0&1&1&0&1&3&0&b\\%
19+
1&2&0&0&0&1&1&0&0&0&1&4&0&c&1&3&0&0&0&1&1&0&1&0&1&5&0&d\\%
20+
\rowcolor{gray!20}1&4&0&0&0&1&1&1&0&0&1&6&0&e&1&5&0&0&0&1&1&1&1&0&1&7&0&f\\%
21+
1&6&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{1}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&0&2&0&\textcolor{green!80!black}{\textbf{1}}&\textcolor{green!80!black}{\textbf{0}}&1&7&0&0&1&0&0&0&1&0&2&1&1&1\\%
22+
\rowcolor{gray!20}1&8&0&0&1&0&0&1&0&0&2&2&1&2&1&9&0&0&1&0&0&1&1&0&2&3&1&3\\%
23+
2&0&0&0&1&0&1&0&0&0&2&4&1&4&2&1&0&0&1&0&1&0&1&0&2&5&1&5\\%
24+
\rowcolor{gray!20}2&2&0&0&1&0&1&1&0&0&2&6&1&6&2&3&0&0&1&0&1&1&1&0&2&7&1&7\\%
25+
2&4&0&0&1&1&0&0&0&0&3&0&1&8&2&5&0&0&1&1&0&0&1&0&3&1&1&9\\%
26+
\rowcolor{gray!20}2&6&0&0&1&1&0&1&0&0&3&2&1&a&2&7&0&0&1&1&0&1&1&0&3&3&1&b\\%
27+
2&8&0&0&1&1&1&0&0&0&3&4&1&c&2&9&0&0&1&1&1&0&1&0&3&5&1&d\\%
28+
\rowcolor{gray!20}3&0&0&0&1&1&1&1&0&0&3&6&1&e&3&1&0&0&1&1&1&1&1&0&3&7&1&f\\%
29+
3&2&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{1}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&0&4&0&2&0&3&3&0&1&0&0&0&0&1&0&4&1&2&1\\%
30+
\rowcolor{gray!20}3&4&0&1&0&0&0&1&0&0&4&2&2&2&3&5&0&1&0&0&0&1&1&0&4&3&2&3\\%
31+
3&6&0&1&0&0&1&0&0&0&4&4&2&4&3&7&0&1&0&0&1&0&1&0&4&5&2&5\\%
32+
\rowcolor{gray!20}3&8&0&1&0&0&1&1&0&0&4&6&2&6&3&9&0&1&0&0&1&1&1&0&4&7&2&7\\%
33+
4&0&0&1&0&1&0&0&0&0&5&0&2&8&4&1&0&1&0&1&0&0&1&0&5&1&2&9\\%
34+
\rowcolor{gray!20}4&2&0&1&0&1&0&1&0&0&5&2&2&a&4&3&0&1&0&1&0&1&1&0&5&3&2&b\\%
35+
4&4&0&1&0&1&1&0&0&0&5&4&2&c&4&5&0&1&0&1&1&0&1&0&5&5&2&d\\%
36+
\rowcolor{gray!20}4&6&0&1&0&1&1&1&0&0&5&6&2&e&4&7&0&1&0&1&1&1&1&0&5&7&2&f\\%
37+
4&8&0&1&1&0&0&0&0&0&6&0&3&0&4&9&0&1&1&0&0&0&1&0&6&1&3&1\\%
38+
\rowcolor{gray!20}5&0&0&1&1&0&0&1&0&0&6&2&3&2&5&1&0&1&1&0&0&1&1&0&6&3&3&3\\%
39+
5&2&0&1&1&0&1&0&0&0&6&4&3&4&5&3&0&1&1&0&1&0&1&0&6&5&3&5\\%
40+
\rowcolor{gray!20}5&4&0&1&1&0&1&1&0&0&6&6&3&6&5&5&0&1&1&0&1&1&1&0&6&7&3&7\\%
41+
5&6&0&1&1&1&0&0&0&0&7&0&3&8&5&7&0&1&1&1&0&0&1&0&7&1&3&9\\%
42+
\rowcolor{gray!20}5&8&0&1&1&1&0&1&0&0&7&2&3&a&5&9&0&1&1&1&0&1&1&0&7&3&3&b\\%
43+
6&0&0&1&1&1&1&0&0&0&7&4&3&c&6&1&0&1&1&1&1&0&1&0&7&5&3&d\\%
44+
\rowcolor{gray!20}6&2&0&1&1&1&1&1&0&0&7&6&3&e&6&3&0&1&1&1&1&1&1&0&7&7&3&f\\%
45+
6&4&\textcolor{blue}{\textbf{1}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{blue}{\textbf{0}}&\textcolor{violet}{\textbf{1}}&\textcolor{violet}{\textbf{0}}&\textcolor{violet}{\textbf{0}}&4&0&6&5&1&0&0&0&0&0&1&1&0&1&4&1\\%
46+
\hline%
47+
\end{tabular}%
48+
}%
49+
\end{table}%
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""Create a LaTeX table with numbers in different systems."""
2+
from math import ceil, log
3+
4+
COLUMNS = 2
5+
MAX_NUMBER = COLUMNS * ceil((64 + 1) / COLUMNS)
6+
7+
BASES = {"dec": 10, "bin": 2, "oct": 8, "hex": 16}
8+
HIGHLIGHTS = ["red", "blue", "violet", "green!80!black"]
9+
10+
BASE_NAMES_LIST = ["decimal~(dec)", "binary~(bin)", "octal~(oct)",
11+
"hexadecimal~(hex)"]
12+
DIGITS = [hex(i)[-1] for i in range(max(BASES.values()))]
13+
14+
print(r"\begin{table}%")
15+
print(r"\centering%")
16+
print(r"\setlength{\tabcolsep}{0.2em}%")
17+
18+
base_names = ", ".join(BASE_NAMES_LIST[:-1]) + ", and " + BASE_NAMES_LIST[-1]
19+
print(
20+
r"\caption{A table with number conversions to the "
21+
+ base_names.lower() +
22+
" systems. The values of the digits are noted in the second row of "
23+
"the header. Powers of the bases are highlighted.}%")
24+
print(r"\label{tbl:numbersBases}%")
25+
print(r"\smallskip%")
26+
print(r"\resizebox{0.99999\linewidth}{!}{")
27+
28+
BASE_COLS = [int(ceil(log(MAX_NUMBER + 1, base))) for base in BASES.values()]
29+
COL_WIDTHS = [f"{ceil(log(base ** (BASE_COLS[i] - 1) + 1, 10))}ex"
30+
for i, base in enumerate(BASES.values())]
31+
32+
BASE_HEADER_1_lst = [r"\textbf{" + name + "}" for name in BASES]
33+
BASE_HEADER_1 = "&".join((r"\multicolumn{" + str(bc) + r"}{c" + (
34+
"|" if i < len(BASE_COLS) - 1 else "ZZ") + "}{"
35+
+ BASE_HEADER_1_lst[i] + "}") for i, bc in enumerate(BASE_COLS))
36+
37+
BASE_HEADER_2_lst = ["Base~" + str(base) for base in BASES.values()]
38+
BASE_HEADER_2 = "&".join((r"\multicolumn{" + str(bc) + r"}{c" + (
39+
"|" if i < len(BASE_COLS) - 1 else "ZZ") + "}{"
40+
+ BASE_HEADER_2_lst[i] + "}") for i, bc in enumerate(BASE_COLS))
41+
BASE_HEADER_3 = "&".join(
42+
(r"\parbox{" + COL_WIDTHS[i] + r"}{\centering\noindent" +
43+
str(base ** (BASE_COLS[i] - j - 1)) + "}") for i, base in enumerate(
44+
BASES.values()) for j in range(BASE_COLS[i]))
45+
BASE_COLS_STRS = "|".join(( ("c" * j) for i, j in enumerate(BASE_COLS)))
46+
47+
print(r"\begin{tabular}{" + "||".join(BASE_COLS_STRS
48+
for _ in range(COLUMNS)) + "}%")
49+
print(r"\hline%")
50+
print(("&".join(BASE_HEADER_1 for _ in range(COLUMNS)) + r"\\%").replace(
51+
"ZZ", "||", COLUMNS-1).replace("ZZ", ""))
52+
#print(("&".join(BASE_HEADER_2 for _ in range(COLUMNS)) + r"\\%").replace(
53+
# "ZZ", "||", COLUMNS-1).replace("ZZ", ""))
54+
print("&".join(BASE_HEADER_3 for _ in range(COLUMNS)) + r"\\%")
55+
print(r"\hline%")
56+
57+
output = ""
58+
color_row = False
59+
for number in range(MAX_NUMBER):
60+
61+
for i, base in enumerate(BASES.values()):
62+
use_num = number
63+
if use_num > 0:
64+
pw_check = log(use_num, base)
65+
highlight = pw_check == int(pw_check)
66+
else:
67+
highlight = False
68+
if highlight:
69+
hls = r"\textcolor{" + HIGHLIGHTS[i] + r"}{\textbf{"
70+
hle = r"}}"
71+
else:
72+
hls = hle = ""
73+
for j in range(BASE_COLS[i]):
74+
value = base ** (BASE_COLS[i] - j - 1)
75+
res = use_num // value
76+
77+
output = output + hls + DIGITS[res] + hle + "&"
78+
use_num = use_num % value
79+
80+
if (number + 1) % COLUMNS == 0:
81+
output = output[:-1] + r"\\%"
82+
if color_row:
83+
output = r"\rowcolor{gray!20}" + output
84+
color_row = not color_row
85+
print(output)
86+
output = ""
87+
continue
88+
89+
print(r"\hline%")
90+
print(r"\end{tabular}%")
91+
print("}%")
92+
print(r"\end{table}%")

0 commit comments

Comments
 (0)