|
5 | 5 | A \python\ program assigning multiple values to multiple variables and using the same method to swap variable values.}{}% |
6 | 6 | % |
7 | 7 | Let us return to the topic of variables and assignments. |
8 | | -In \python, we can assign values to multiple variables at once. |
| 8 | +In \python, we can assign values to multiple variables at once~(ofcourse, always exactly one value to one variable). |
9 | 9 | In this case, we separate both the variable names and the values with commas. |
10 | | -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. |
| 10 | +The first line~\pythonil{a, b = 5, 10}\pythonIdx{=!multiple}\pythonIdx{,} in \cref{lst:variables:multi_and_swap} is equivalent to the two lines~\pythonil{a = 5} and~\pythonil{b = 10}. |
| 11 | +It assigns the value~\pythonil{5} to the variable~\pythonil{a} and the value~\pythonil{10} to the variable~\pythonil{b}. |
11 | 12 | After this assignment step, \pythonil{a == 5} and \pythonil{b == 10} holds. |
12 | | -\pythonil{print(f"a={a}, b={b}")} therefore prints \textit{a=5, b=10}. |
| 13 | +\pythonil{print(f\" \{a = \}, \{b = \}\")} therefore prints \textit{a = 5, b = 10}. |
13 | 14 |
|
14 | 15 | This method can also be used to \emph{swap} the values of two variables\pythonIdx{=!swap}\pythonIdx{swap}. |
15 | | -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}.} |
16 | | -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}. |
17 | | -But it accomplishes this in single line of code instead of three. |
18 | | -\pythonil{print(f"a={a}, b={b}")} thus now prints \textit{a=10, b=5}.% |
| 16 | +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}.} |
| 17 | +The line is therefore basically equivalent zu \pythonil{t = a}, \pythonil{a = b}, and \pythonil{b = t}. |
| 18 | +Without the double-assignment, we would first have to store the value of~\pythonil{a} in some temporary variable~\pythonil{t}, then overwrite \pythonil{a} with \pythonil{b}, and finally we would copy the value of \pythonil{t} to \pythonil{b}. |
| 19 | +But with the double-assignment, we can accomplishes this in single line of code instead of three. |
| 20 | +And we do not need a temporary variable either. |
| 21 | +\pythonil{print(f\" \{a = \}, \{b = \}\")} thus now prints \textit{a=10, b=5}.% |
19 | 22 | % |
20 | 23 | \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}.}% |
21 | 24 | % |
22 | 25 | The same concept of multiple assignments works for arbitrarily many variables. |
23 | 26 | \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}. |
24 | | -\pythonil{print(f"x={x}, y={y}, z={z}")} thus yields \textil{x=3, y=2, z=1}. |
| 27 | +\pythonil{print(f\"\{x = \}, \{y = \}, \{z = \}\")} thus yields \textil{x = 3, y = 2, z = 1}. |
25 | 28 |
|
26 | 29 | We can also swap multiple values. |
27 | 30 | \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}. |
28 | | -\pythonil{print(f"x={x}, y={y}, z={z}")} now gives us \textil{x=1, y=2, z=3}. |
| 31 | +\pythonil{print(f\"\{x = \}, \{y = \}, \{z = \}\")} now gives us \textil{x = 1, y = 2, z = 3}. |
29 | 32 | % |
30 | 33 | \FloatBarrier% |
31 | 34 | \endhsection% |
|
0 commit comments