|
| 1 | +\hsection{Python in the Terminal}% |
| 2 | +\label{sec:pythonInTheTerminal}% |
| 3 | +\FloatBarrier% |
| 4 | +% |
| 5 | +\begin{figure}% |
| 6 | +\centering% |
| 7 | +% |
| 8 | +\subfloat[][% |
| 9 | +Open a \pgls{terminal} (by pressing \ubuntuTerminal\ under \ubuntu\ \linux; under \windows\ \windowsTerminal). % |
| 10 | +Change into the directory \inQuotes{directory} where your \python\ file is located, by typing \bashil{cd directory} and hit \keys{\enter}.% |
| 11 | +\label{fig:terminalPython1cd}% |
| 12 | +]{\includegraphics[width=0.9\linewidth]{\currentDir/terminalPython1cd}}% |
| 13 | +% |
| 14 | +\\[10pt]% |
| 15 | +% |
| 16 | +\subfloat[][% |
| 17 | +Execute a \python\ program \inQuotes{program.py} by typing \bashil{python3 program.py} and hit \keys{\enter}. % |
| 18 | +In our case, the program is \inQuotes{very\_first\_program.py}.% |
| 19 | +\label{fig:terminalPython2python}% |
| 20 | +]{\includegraphics[width=0.9\linewidth]{\currentDir/terminalPython2python}}% |
| 21 | +% |
| 22 | +\\[10pt]% |
| 23 | +% |
| 24 | +\subfloat[][% |
| 25 | +As expected, the text \inQuotes{Hello World!} appears in the \pgls{terminal}.% |
| 26 | +\label{fig:terminalPython3result}% |
| 27 | +]{\includegraphics[width=0.9\linewidth]{\currentDir/terminalPython3result}}% |
| 28 | +% |
| 29 | +\caption{Example for executing a \python\ program in a \pgls{terminal} (on \ubuntu).}% |
| 30 | +\label{fig:terminalPython}% |
| 31 | +\end{figure}% |
| 32 | +% |
| 33 | +\begin{figure}% |
| 34 | +\centering% |
| 35 | +% |
| 36 | +\subfloat[][% |
| 37 | +Pressing the \menu{\pycharmConsole} on the vertical icon bar on the left side of the \pycharm\ window.% |
| 38 | +\label{fig:pycharmConsole1consoleButton}% |
| 39 | +]{\tightbox{\includegraphics[width=0.76\linewidth]{\currentDir/pycharmConsole1consoleButton}}}% |
| 40 | +% |
| 41 | +\\[10pt]% |
| 42 | +% |
| 43 | +\subfloat[][% |
| 44 | +The \pycharm\ \python\ console is open.% |
| 45 | +\label{fig:pycharmConsole2consoleOpen}% |
| 46 | +]{\tightbox{\includegraphics[width=0.76\linewidth]{\currentDir/pycharmConsole2consoleOpen}}}% |
| 47 | +% |
| 48 | +\caption{Entering the \inQuotes{Hello World!} program from \cref{lst:very_first_program} directly into the \python\ console offered by \pycharm.}% |
| 49 | +\label{fig:pycharmConsoleA}% |
| 50 | +\end{figure}% |
| 51 | +\begin{figure}% |
| 52 | +\ContinuedFloat% |
| 53 | +\centering% |
| 54 | +% |
| 55 | +\subfloat[][% |
| 56 | +We enter the \inQuotes{Hello World!} program from \cref{lst:very_first_program} and press \keys{\enter}.% |
| 57 | +\label{fig:pycharmConsole3writingCode}% |
| 58 | +]{\tightbox{\includegraphics[width=0.76\linewidth]{\currentDir/pycharmConsole3writingCode}}}% |
| 59 | +% |
| 60 | +\\[10pt]% |
| 61 | +% |
| 62 | +\subfloat[][% |
| 63 | +And indeed, the output is \inQuotes{Hello World!}.% |
| 64 | +\label{fig:pycharmConsole4codeOutput}% |
| 65 | +]{\tightbox{\includegraphics[width=0.76\linewidth]{\currentDir/pycharmConsole4codeOutput}}}% |
| 66 | +% |
| 67 | +\caption{Entering the \inQuotes{Hello World!} program from \cref{lst:very_first_program} directly into the \python\ console offered by \pycharm~(continued).}% |
| 68 | +\label{fig:pycharmConsoleB}% |
| 69 | +\end{figure}% |
| 70 | +% |
| 71 | +\begin{figure}% |
| 72 | +\centering% |
| 73 | +% |
| 74 | +\subfloat[][% |
| 75 | +Open a \pgls{terminal} (by pressing \ubuntuTerminal\ under \ubuntu\ \linux; under \windows\ \windowsTerminal), enter \bashil{python3}, then hit \keys{\enter}.% |
| 76 | +\label{fig:terminalConsole1python}% |
| 77 | +]{\includegraphics[width=0.852\linewidth]{\currentDir/terminalConsole1python}}% |
| 78 | +% |
| 79 | +\\[10pt]% |
| 80 | +% |
| 81 | +\subfloat[][% |
| 82 | +The \python\ console opens in the \pgls{terminal}.% |
| 83 | +\label{fig:terminalConsole2pythonRunning}% |
| 84 | +]{\includegraphics[width=0.852\linewidth]{\currentDir/terminalConsole2pythonRunning}}% |
| 85 | +% |
| 86 | +\\[10pt]% |
| 87 | +% |
| 88 | +\subfloat[][% |
| 89 | +We enter the \inQuotes{Hello World!} program from \cref{lst:very_first_program} and press \keys{\enter}.% |
| 90 | +\label{fig:terminalConsole3writingCode}% |
| 91 | +]{\includegraphics[width=0.852\linewidth]{\currentDir/terminalConsole3writingCode}}% |
| 92 | +% |
| 93 | +\\[10pt]% |
| 94 | +% |
| 95 | +\subfloat[][% |
| 96 | +And indeed, the output is \inQuotes{Hello World!}.% |
| 97 | +\label{fig:terminalConsole4codeOutput}% |
| 98 | +]{\includegraphics[width=0.852\linewidth]{\currentDir/terminalConsole4codeOutput}}% |
| 99 | +% |
| 100 | +\caption{Writing a program in the \python\ console in the \pgls{terminal}~(\ubuntu).}% |
| 101 | +\label{fig:terminalConsoleA}% |
| 102 | +\end{figure}% |
| 103 | +\begin{figure}% |
| 104 | +\ContinuedFloat% |
| 105 | +\centering% |
| 106 | +% |
| 107 | +\subfloat[][% |
| 108 | +We exit the console by typing \pythonil{exit()} and pressing \keys{\enter}.% |
| 109 | +\label{fig:terminalConsole5exit}% |
| 110 | +]{\includegraphics[width=0.852\linewidth]{\currentDir/terminalConsole5exit}}% |
| 111 | +% |
| 112 | +\\[10pt]% |
| 113 | +% |
| 114 | +\subfloat[][% |
| 115 | +We are back in the normal \pgls{terminal}.% |
| 116 | +\label{fig:terminalConsole6left}% |
| 117 | +]{\includegraphics[width=0.852\linewidth]{\currentDir/terminalConsole6left}}% |
| 118 | +% |
| 119 | +\caption{Writing a program in the \python\ console in the \pgls{terminal}~(\ubuntu,~continued).}% |
| 120 | +\label{fig:terminalConsoleB}% |
| 121 | +\end{figure}% |
| 122 | +% |
| 123 | +In total, are four more ways in which we can execute a \python\ program:% |
| 124 | +% |
| 125 | +\begin{enumerate}% |
| 126 | +% |
| 127 | +\item We can enter the program into a \python\ file in the \pycharm\ \pgls{IDE} and then run it from there. % |
| 128 | +We just did this in the previous section and illustrated it in \cref{fig:veryFirstProgramC}.% |
| 129 | +% |
| 130 | +\item Actually, we can also write a \python\ program with a normal text editor. % |
| 131 | +A \python\ program is just a normal text file, after all. % |
| 132 | +We can execute such a text file by entering its directory and typing \bashil{python3 programName} (where \bashil{programName} is \bashil{very_first_program.py}, in our case) and hitting \keys{\enter}. % |
| 133 | +Then the program is executed directly in the \pgls{terminal}. % |
| 134 | +This process is shown in \cref{fig:terminalPython}.% |
| 135 | +% |
| 136 | +\item Alternatively, we could open the \python\ interpreter console in \pycharm\ and enter and execute our code line-by-line. % |
| 137 | +This is sketched in \cref{fig:pycharmConsoleA}.% |
| 138 | +% |
| 139 | +\item Besides using the \python\ console inside \pycharm, we can also open it inside a \pgls{terminal}. % |
| 140 | +We can then enter separate \python\ instructions and run them there. % |
| 141 | +This fourth option is outlined in \cref{fig:terminalConsoleA}.% |
| 142 | +% |
| 143 | +\end{enumerate}% |
| 144 | +% |
| 145 | +In order to directly execute a \python\ program in a \pgls{terminal}, we first need to open one. |
| 146 | +Under \ubuntu\ \linux, we simply press \ubuntuTerminal. |
| 147 | +Under \windows, we have to \windowsTerminal. |
| 148 | +Once the terminal is open, we need to change into the directory where the program is located. |
| 149 | +Under both \linux\ and \windows, this can be done by typing the command \bashil{cd}, followed by the path to the directory, and hitting \keys{\enter}.\footnote{% |
| 150 | +Under \windows, you may also need to change into the correct drive first.} |
| 151 | +We provide a screenshot for that, taken under \ubuntu\ \linux, in \cref{fig:terminalPython1cd}. |
| 152 | +Now we simply call the \python\ interpreter by writing \bashil{python3} followed by the file name of our program, which is \bashil{very_first_program.py} in our case. |
| 153 | +In \cref{fig:terminalPython2python} we do this and hit \keys{\enter}, which causes the \python\ interpreter to execute our program. |
| 154 | +The output \inQuotes{Hello World!} is then printed into the \pgls{terminal} in \cref{fig:terminalPython3result}. |
| 155 | + |
| 156 | +Besides writing programs in files and executing them, we can also directly enter them into the \python\ console and execute them step-by-step. |
| 157 | +This does not make sense if we want to reuse our programs later. |
| 158 | +But it does make a lot of sense when we just want to test some commands or functions or quickly test some idea. |
| 159 | +A \python\ console can be used directly in \pycharm~(\cref{fig:pycharmConsoleA}) or opened in a \pgls{terminal}~(\cref{fig:terminalConsoleA}). |
| 160 | + |
| 161 | +To explore entering \python\ code in the \python\ console inside \pycharm, we continue where we left of in \cref{sec:ourFirstProgram}. |
| 162 | +In \pycharm, we first click the \menu{\pycharmConsole} on the vertical icon bar on the left side of the \pycharm\ window, as shown in \cref{fig:pycharmConsole1consoleButton}. |
| 163 | +This directly brings us to the \python\ console~(\cref{fig:pycharmConsole2consoleOpen}). |
| 164 | +We can enter the one-line-program from \cref{lst:very_first_program} and press \keys{\enter}, as illustrated in \cref{fig:pycharmConsole3writingCode}, which leads to the expected output shown in \cref{fig:pycharmConsole4codeOutput}. |
| 165 | + |
| 166 | +Let us now open a \python\ console from the \pgls{terminal} instead of using the one in \pycharm. |
| 167 | +We therefore first need to open a normal terminal. |
| 168 | +Under \ubuntu\ \linux, we simply press \ubuntuTerminal. |
| 169 | +Under \windows, we have to \windowsTerminal. |
| 170 | +Either way, the terminal opens and we can enter \bashil{python3} and press \keys{\enter}, as shown in \cref{fig:terminalConsole1python}. |
| 171 | +Now the \python\ interpreter starts right inside the \pgls{terminal}~(\cref{fig:terminalConsole2pythonRunning}). |
| 172 | +As illustrated in \cref{fig:terminalConsole3writingCode}, we copy the single line of code, \pythonil{print("Hello World!")} from \cref{lst:very_first_program} and press \keys{\enter}. |
| 173 | +The output \inQuotes{Hello World!} is printed as expected in \cref{fig:terminalConsole4codeOutput}. |
| 174 | +However, we now are still in the \python\ interpreter. |
| 175 | +In order to leave it and to, maybe, enter other commands in the \pgls{terminal}, we have to use another new \python\ instruction: |
| 176 | +We type in \pythonil{exit()} and press \keys{\enter}, as shown in \cref{fig:terminalConsole5exit}, which causes the \python\ interpreter to exit. |
| 177 | +We are now back in the basic terminal, as shown in \cref{fig:terminalConsole6left}. |
| 178 | +In these figures, I was using \ubuntu\ \linux. |
| 179 | +On \windows\ or other \linux\ variants, the process would have looked quite similar.% |
| 180 | +% |
| 181 | +\endhsection% |
| 182 | +% |
0 commit comments