|
3 | 3 | % |
4 | 4 | Like in mathematics, a variable in \python\ is basically a name with a value assigned to it. |
5 | 5 | You can define a variable and assign its value by writing \pythonil{name = value}. |
6 | | -Here, \pythonil{name} is the name of the variable and \pythonil{value} be the value that we want to assign to that name.% |
| 6 | +Here, \pythonil{name} is the name of the variable and \pythonil{value} be the value that we want to assign to that name. |
| 7 | +If we want to access the value that was stored, we can just use the \pythonil{name} instead. |
| 8 | +You can write \pythonil{name} in an arbitrary expression and \python\ then just uses \pythonil{value} instead. |
| 9 | +Indeed, you have seen this before, when we used the variables~\pythonilIdx{pi}, \pythonilIdx{e}, \pythonilIdx{inf}, and \pythonilIdx{nan} that we imported from the module~\pythonilIdx{math}. |
| 10 | +You can also change which value is stored under a given name by assigning another value to it, e.g., by doing~\pythonil{name = new_value}. |
| 11 | + |
| 12 | +With this, we can now store intermediate results and use them in later computation steps. |
| 13 | +This allows us, for the first time, two write programs that perform computations in multiple steps and that consist of multiple lines of code. |
| 14 | +\python\ programs are stored as text files with the suffix~\textil{.py}, e.g., \textil{hello.py}. |
| 15 | + |
| 16 | +In the very moment we begin to create such files, two things happen: |
| 17 | +First, our code will immediately become much more complex. |
| 18 | +Second, our code can be reused, i.e., executed multiple times. |
| 19 | +It can be reused by us, now or in ten years, or shared with others. |
| 20 | +This changes the quality of programming entirely. |
| 21 | +Until now, the \python\ interpreter was basically a fancy calculator. |
| 22 | +Now our programs become tools to be used hundereds of times or building blocks, or bricks of giant architectures. |
| 23 | + |
| 24 | +Therefore, from the very start when we actually begin to write program files, it becomes important that we clearly document what we do and why. |
| 25 | +For this purpose, we will never just write code -- we will always write comments giving explanations of what our code does. |
| 26 | +From the very beginning we must train ourselves to proper discipline.% |
| 27 | +% |
| 28 | +\bestPractice{comments}{% |
| 29 | +Comments help to explain what the code in programs does and are a very important part of the \emph{documentation} of code. % |
| 30 | +Comments begin with a \pythonilIdx{\#} character, after which all text is ignored by the \python\ interpreter until the end of the current line. % |
| 31 | +Comments can either occupy a complete line or we insert two spaces after the last code character in the line and then start the comment~\cite{PEP8}.% |
| 32 | +}% |
| 33 | +% |
| 34 | +So we now learn two things together: |
| 35 | +Using variable assignment and commenting our code. |
| 36 | +Because variable assignment is the most fundamental step in writing programs and because programs without comments are \emph{wrong}. |
| 37 | +By the way, this is how we do it in this book: |
| 38 | +We learn new programming concepts, but try to always intersperse important best practices.% |
7 | 39 | % |
8 | 40 | \hsection{A Simple Example of Variable Assignment and Comments in the Code}% |
9 | 41 | \gitLoadAndExecPython{variables:assignment}{}{variables}{assignment.py}{}% |
|
96 | 128 | \label{fig:variables:assignment}% |
97 | 129 | \end{figure}% |
98 | 130 | % |
99 | | -With this, we can now store intermediate results. |
100 | | -This allows us, for the first time, two write programs that perform computations in multiple steps and that consist of multiple lines of code.% |
101 | | -% |
102 | | -\bestPractice{comments}{% |
103 | | -Comments help to explain what the code in programs does and are a very important part of the \emph{documentation} of code. % |
104 | | -Comments begin with a \pythonilIdx{\#} character, after which all text is ignored by the \python\ interpreter until the end of the current line. % |
105 | | -Comments can either occupy a complete line or we insert two spaces after the last code character in the line and then start the comment~\cite{PEP8}.% |
106 | | -}% |
107 | | -% |
108 | | -\cref{lst:variables:assignment} shows the source code of such a commented program. |
| 131 | +So let's get to the subject of variable assignments with some examples. |
| 132 | +\Cref{lst:variables:assignment} shows the source code of our very first commented program. |
109 | 133 | This program does not do anything useful, but it illustrates how variables can be used. |
110 | | - |
111 | 134 | It begins by assigning the \pythonilIdx{int} value~\pythonil{1} to a variable named~\pythonil{int_var}. |
112 | 135 | 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}. |
113 | 136 | But we chose \pythonil{int_var}. |
|
0 commit comments