|
| 1 | +\hsection{Our First Program}% |
| 2 | +\FloatBarrier% |
| 3 | +% |
| 4 | +We now want to write and execute our very first \python\ program. |
| 5 | +This program should just print \inQuotes{Hello World!} to the text output and then exit. |
| 6 | +It therefore will consist of the single statement \pythonil{print("Hello World!")}, as illustrated in \cref{lst:very_first_program}. |
| 7 | + |
| 8 | +\exCode{00_veryFirstProject/very_first_program.py}{--args format}{very_first_program}{% |
| 9 | +Our very first \python\ program, which just prints \inQuotes{Hello World!}}% |
| 10 | +% |
| 11 | +\begin{figure}% |
| 12 | +\centering% |
| 13 | +% |
| 14 | +\subfloat[][% |
| 15 | +Creating a new project in \pycharm, step~1: click \menu{New Project}.% |
| 16 | +\label{fig:firstProgram01createPycharmProject}% |
| 17 | +]{\tightbox{\includegraphics[width=0.47\linewidth]{\currentDir/firstProgram01createPycharmProject}}}% |
| 18 | +% |
| 19 | +\strut\hfill\strut% |
| 20 | +% |
| 21 | +\subfloat[][% |
| 22 | +Creating a new project in \pycharm, step~2: Make sure that \menu{Pure Python} is selected in the left pane, then select a name for the project (here: \directory{00\_veryFirstProject}), a directory location (some path on my \ubuntu\ machine, you will pick some other directory), and we select the current \python\ installation as \menu{Custom Environment}. % |
| 23 | +We finally click \menu{Create}.% |
| 24 | +\label{fig:firstProgram02createPycharmProject}% |
| 25 | +]{\tightbox{\includegraphics[width=0.47\linewidth]{\currentDir/firstProgram02createPycharmProject}}}% |
| 26 | +% |
| 27 | +\\[10pt]% |
| 28 | +% |
| 29 | +\subfloat[][% |
| 30 | +The new and empty project has been created.% |
| 31 | +\label{fig:firstProgram03pycharmProjectCreated}% |
| 32 | +]{\tightbox{\includegraphics[width=0.775\linewidth]{\currentDir/firstProgram03pycharmProjectCreated}}}% |
| 33 | +% |
| 34 | +\\[10pt]% |
| 35 | +% |
| 36 | +\subfloat[][% |
| 37 | +We create a new \python\ file within this project by right-clicking on the project folder \directory{00\_veryFirstProject} and selecting \menu{New>Python File}.% |
| 38 | +\label{fig:firstProgram04createPythonFile}% |
| 39 | +]{\tightbox{\includegraphics[width=0.775\linewidth,trim={0 200 0 0},clip]{\currentDir/firstProgram04createPythonFile}}}% |
| 40 | +% |
| 41 | +\caption{The steps to create a new \python\ file in a new \pycharm\ project and to then run it (\ubuntu).}% |
| 42 | +\label{fig:veryFirstProgramA}% |
| 43 | +\end{figure} |
| 44 | +% |
| 45 | +\begin{figure}% |
| 46 | +\ContinuedFloat% |
| 47 | +\centering% |
| 48 | +% |
| 49 | +\subfloat[][% |
| 50 | +We enter a name for the new \python\ file (here:~\directory{very\_first\_program}) and hit \keys{\enter}.% |
| 51 | +\label{fig:firstProgram05createPythonFile}% |
| 52 | +]{\tightbox{\includegraphics[width=0.775\linewidth]{\currentDir/firstProgram05createPythonFile}}}% |
| 53 | +% |
| 54 | +\\[10pt]% |
| 55 | +% |
| 56 | +\subfloat[][% |
| 57 | +The new and empty file \directory{very\_first\_program.py} has been created in the project \directory{00\_veryFirstProject}.% |
| 58 | +\label{fig:firstProgram06pythonFileCreated}% |
| 59 | +]{\tightbox{\includegraphics[width=0.775\linewidth]{\currentDir/firstProgram06pythonFileCreated}}}% |
| 60 | +% |
| 61 | +\\[10pt]% |
| 62 | +% |
| 63 | +\subfloat[][% |
| 64 | +We enter the text from \cref{lst:very_first_program}. \pycharm\ automatically saves it.% |
| 65 | +\label{fig:firstProgram07writeHelloWorld}% |
| 66 | +]{\tightbox{\includegraphics[width=0.775\linewidth]{\currentDir/firstProgram07writeHelloWorld}}}% |
| 67 | +% |
| 68 | +% |
| 69 | +\caption{The steps to create a new \python\ file in a new \pycharm\ project and to then run it (\ubuntu).}% |
| 70 | +\label{fig:veryFirstProgramB}% |
| 71 | +\centering% |
| 72 | +% |
| 73 | +\end{figure}% |
| 74 | +% |
| 75 | +\begin{figure}% |
| 76 | +\ContinuedFloat% |
| 77 | +\centering% |
| 78 | +% |
| 79 | +\subfloat[][% |
| 80 | +In order to run this program, we right-click on the program file in the tree view and select \menu{Run `very\_first\_program'}. % |
| 81 | +Alternatively, we could press \keys{\ctrl+\shift+F10}.% |
| 82 | +\label{fig:firstProgram08runProgram}% |
| 83 | +]{\tightbox{\includegraphics[width=0.775\linewidth,trim={0 200px 0 0},clip]{\currentDir/firstProgram08runProgram}}}% |
| 84 | +% |
| 85 | +\\[10pt]% |
| 86 | +% |
| 87 | +\subfloat[][% |
| 88 | +And indeed, in the console pane at the bottom of the \pycharm\ window, the text \inQuotes{Hello World!} appears.% |
| 89 | +\label{fig:firstProgram09programResult}% |
| 90 | +]{\tightbox{\includegraphics[width=0.775\linewidth]{\currentDir/firstProgram09programResult}}}% |
| 91 | +% |
| 92 | +\caption{The steps to create a new \python\ file in a new \pycharm\ project and to then run it (\ubuntu).}% |
| 93 | +\label{fig:veryFirstProgramC}% |
| 94 | +\centering% |
| 95 | +% |
| 96 | +\end{figure}% |
| 97 | + |
| 98 | +In \pycharm, we usually will not just create a single \python\ source code file. |
| 99 | +Instead, we will work in the context of \emph{projects}, which can contain many \python\ files, settings, build scripts, and other resources. |
| 100 | +Inside such a project, we would create the \python\ file, write our source code from \cref{lst:very_first_program} into it, and then run it. |
| 101 | +The steps for doing this are illustrated with screenshots in \cref{fig:veryFirstProgramA}, which were taken on my \ubuntu\ machine. |
| 102 | +On \windows, this will look very similar (maybe with the exception of ``\verb=\='' instead of ``\verb=/='' as directory separator characters in paths). |
| 103 | + |
| 104 | +Since we started with a completely new \pycharm\ installation in \cref{sec:installingPyCharm}, no project has yet been created. |
| 105 | +So when we open \pycharm, we will arrive at the project creation screen illustrated in \cref{fig:firstProgram01createPycharmProject}. |
| 106 | +Here, we will \menu{New Project}, which takes us to the second project creation screen shown in \cref{fig:firstProgram02createPycharmProject}. |
| 107 | +In case that you already had \pycharm\ installed and already created some projects in the past, you can get to the same screen by clicking \menu{\pycharmMainMenu > File > New Project}. |
| 108 | +Either way, when arriving at the screen, you will find several confusingly looking options. |
| 109 | +First, make sure that \menu{Pure Python} is selected in the left pane. |
| 110 | +Then you can choose a name for the project in the \menu{Name:} text box. |
| 111 | +For the sake of our example, let's call the new project \directory{00\_veryFirstProject}. |
| 112 | + |
| 113 | +Below the name text box, you can select the destination directory in which the project folder should be created in the \menu{Location:} box. |
| 114 | +You can ignore the text in the screenshot, as this is just the path that I am using on my \ubuntu\ machine. |
| 115 | +Instead, you will choose some suitable directory on your computer. |
| 116 | +(Notice that in \windows, the directory separator character is ``\verb=\='' and not ``\verb=/='' as on my \ubuntu\ \linux.) |
| 117 | +Either way, on my machine, I choose the rather elaborate path \directory{home/tweise/local/programming/python/programmingWithPythonCode}. |
| 118 | +\pycharm\ will create the folder \directory{00\_veryFirstProject} for our new project \emph{inside} this directory. |
| 119 | +But, as said, you will choose some other suitable place on your computer. |
| 120 | + |
| 121 | +The following options may not make any sense for you, which is totally OK for now. |
| 122 | +Please make sure to select \menu{Interpreter Type: > Custom Environment} as well as \menu{Environment: > Select existing} and \menu{Type: > Python}. |
| 123 | +Under \menu{Python Path:}, you would choose the \python\ interpreter you have installed on your system (see \cref{sec:installingPython}). |
| 124 | +On my system that was \inQuotes{\python~3.10.12} at the time of this writing. |
| 125 | +We finally click \menu{Create}. |
| 126 | +We now have created our first and empty \pycharm\ \python\ project -- as illustrated in \cref{fig:firstProgram03pycharmProjectCreated}. |
| 127 | + |
| 128 | +We can now create the \python\ file to write our actual program code. |
| 129 | +To do this, we right-click on the folder \directory{00\_veryFirstProject} in our \menu{Project} tree view pane on the left-hand side (see \cref{fig:firstProgram04createPythonFile}). |
| 130 | +In the menu that pops up, we select and click \menu{New > Python File}. |
| 131 | +This takes us the \menu{New Python file} creation dialog illustrated in \cref{fig:firstProgram05createPythonFile}. |
| 132 | +Here, we enter the name for our first program. |
| 133 | +What could be more fitting than \directory{very\_first\_program}? |
| 134 | +After hitting \keys{Enter}, the file is created in our project folder and opened in the editor pane, as shown in \cref{fig:firstProgram06pythonFileCreated}. |
| 135 | + |
| 136 | +We now enter the single line of code from \cref{lst:very_first_program} into the editor, as illustrated in \cref{fig:firstProgram07writeHelloWorld}. |
| 137 | +We do not need to save the file, as \pycharm\ does this automatically for us. |
| 138 | + |
| 139 | +Finally, we want to actually execute, i.e., run, our program. |
| 140 | +We can do this directly from the editor by pressing \keys{\ctrl + \shift + F10}. |
| 141 | +We can also do it by right-clicking on the file in the project tree view on the left-hand side and then clicking \menu{Run `very\_first\_program'} in the popup menu, as shown in \cref{fig:firstProgram08runProgram}. |
| 142 | + |
| 143 | +Either way, a console with the title \inQuotes{very\_first\_program} opens at the bottom of our editor. |
| 144 | +And behold: |
| 145 | +Indeed, the text \bashil{Hello World!} appears. |
| 146 | + |
| 147 | +Well, before that text, we see the command line that was actually executed, namely the \python\ interpreter with our file's path as parameter. |
| 148 | +And after our program's output, we are notified that \inQuotes{Process finished with exit code 0,} which means that the program has completed successfully and without error. |
| 149 | + |
| 150 | +Congratulations. |
| 151 | +You now have written, saved, and executed your first ever \python\ program!% |
| 152 | +% |
| 153 | +\endhsection% |
| 154 | +% |
0 commit comments