Skip to content

Commit 468010b

Browse files
committed
added multiple assignments and variable swapping
1 parent f6c5440 commit 468010b

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

text/back/backmatter.tex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
\checkandfixthelayout%
1010
%
1111
\printBestPractices%
12-
\printacronyms%
1312
\printglossaries%
1413
%
1514
\renewcommand{\indexname}{Index of \python\ Commands}%

text/main/basics/variables/assignment/assignment.tex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
This program does not do anything useful, but it illustrates how variables can be used.
109109

110110
It begins by assigning the \pythonilIdx{int} value~\pythonil{1} to a variable named~\pythonil{int_var}.
111-
We could have chosen any other name as well, as long as it does not contain spaces, e.g., \pythonil{myvalue}, \pythonil{cow}, \pythonil{race_car}.
111+
We could have chosen any other name as well, as long as it does not contain spaces, e.g., \pythonil{my_value}, \pythonil{cow}, \pythonil{race_car}.
112112
But we chose \pythonil{int_var}.
113113
The \pythonilIdx{=} assigns the value~\pythonil{1} to \pythonil{int_value}.
114114
As \cref{fig:variable:assignment1} illustrates, the value~\pythonil{1} will now be stored somewhere in memory and \pythonil{int_var} is a name that points to this memory location.%
@@ -290,5 +290,6 @@
290290
%
291291
\endhsection%
292292
%
293+
\FloatBarrier%
293294
\endhsection%
294295
%
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
\hsection{Multiple Assignments and Value Swapping}%
2+
%
3+
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{01_variables}{multi_and_swap.py}{--args format}{variables:multi_and_swap}{%
4+
A \python\ program assigning multiple values to multiple variables and using the same method to swap variable values.}%
5+
%
6+
In \python, we can assign values to multiple variables at once.
7+
In this case, we separate both the variable names and the values with commas.
8+
The first line (\pythonil{a, b = 5, 10}\pythonIdx{=!multiple}\pythonIdx{,}) in \cref{lst:variables:multi_and_swap} assigns the values~\pythonil{5} and~\pythonil{10}, respectively, to the variables~\pythonil{a} and~\pythonil{b}, respectively.
9+
After this assignment step, \pythonil{a == 5} and \pythonil{b == 10} holds.
10+
\pythonil{print(f"a={a}, b={b}")} therefore prints \textit{a=5, b=10}.
11+
12+
This method can also be used to \emph{swap} the values of two variables\pythonIdx{=!swap}\pythonIdx{swap}.
13+
Writing \pythonil{a, b = b, a} looks a bit strange but it basically means \inQuotes{the \emph{new} value of \pythonil{a} will be the \emph{present} value of \pythonil{b} and the \emph{new} value of \pythonil{b} will be the \emph{present} value of \pythonil{a}.}
14+
The line is therefore basically equivalent to first storing~\pythonil{a} in a temporary variable~\pythonil{t}, then overwriting \pythonil{a} with \pythonil{b}, and finally copying the value of \pythonil{t} to \pythonil{t}.
15+
But it accomplishes this in single line of code instead of three.
16+
\pythonil{print(f"a={a}, b={b}")} thus now prints \textit{a=10, b=5}.%
17+
%
18+
\bestPractice{swap}{Swapping of variable values can best be done with a multi-assignment statement, e.g., \pythonil{a, b = b, a}\pythonIdx{=!swap}\pythonIdx{swap}.}%
19+
%
20+
The same concept of multiple assignments works for arbitrarily many variables.
21+
\pythonil{z, y, x = 1, 2, 3} assigns, respectively, \pythonil{1} to \pythonil{z}, \pythonil{2} to \pythonil{y}, and \pythonil{3} to \pythonil{x}.
22+
\pythonil{print(f"x={x}, y={y}, z={z}")} thus yields \textil{x=3, y=2, z=1}.
23+
24+
We can also swap multiple values.
25+
\pythonil{x, y, z = z, y, x} assigns the present value of \pythonil{z} to become the new value of \pythonil{x}, the present value of \pythonil{y} to also be the new value of \pythonil{y}, and the present value of \pythonil{x} to become the new value of \pythonil{z}.
26+
\pythonil{print(f"x={x}, y={y}, z={z}")} now gives us \textil{x=1, y=2, z=3}.
27+
%
28+
\endhsection%
29+
%

text/main/basics/variables/variables.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
For this purpose, variables exist.%
99
%
1010
\hinput{assignment}{assignment.tex}%
11+
\hinput{multiAndSwap}{multiAndSwap.tex}%
1112
%
1213
\endhsection%
1314
%

0 commit comments

Comments
 (0)