Skip to content

Commit 5542043

Browse files
committed
added python console usage
1 parent 64cd27b commit 5542043

22 files changed

+208
-8
lines changed

styles/icons/pycharmPythonConsole.pdf

2.84 KB
Binary file not shown.

styles/icons/pycharmPythonConsole.svg

Lines changed: 1 addition & 0 deletions
Loading

styles/keys.sty

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
\protected\gdef\ubuntuTerminal{\keys{\ctrl+\Alt+T}}%
1616
\protected\gdef\windowsTerminal{press \keys{\OSwin + R}, type in \bashil{cmd}, and hit \keys{\return}}%
1717
%
18-
%% the main menu key
18+
%% the PyCharm main menu key
1919
\protected\gdef\pycharmMainMenu{\ensuremath{\mathrel{\rlap{\raisebox{\fontdimen22\textfont2}{\ensuremath{=}}}\raisebox{-0.5\fontdimen22\textfont2}{\ensuremath{=}}}}}
2020
%
21+
%% the PyCharm python console menu key
22+
\protected\gdef\pycharmConsole{\raisebox{-1.75pt}{\includegraphics[width=1em,keepaspectratio]{styles/icons/pycharmPythonConsole}}}%
23+
%

text/main/basics/gettingStarted/firstProgram/firstProgram.tex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
\hsection{Our First Program}%
2+
\label{sec:ourFirstProgram}%
23
\FloatBarrier%
34
%
45
We now want to write and execute our very first \python\ program.
@@ -66,7 +67,7 @@
6667
]{\tightbox{\includegraphics[width=0.775\linewidth]{\currentDir/firstProgram07writeHelloWorld}}}%
6768
%
6869
%
69-
\caption{The steps to create a new \python\ file in a new \pycharm\ project and to then run it (\ubuntu).}%
70+
\caption{The steps to create a new \python\ file in a new \pycharm\ project and to then run it (\ubuntu,~continued).}%
7071
\label{fig:veryFirstProgramB}%
7172
\centering%
7273
%
@@ -89,7 +90,7 @@
8990
\label{fig:firstProgram09programResult}%
9091
]{\tightbox{\includegraphics[width=0.775\linewidth]{\currentDir/firstProgram09programResult}}}%
9192
%
92-
\caption{The steps to create a new \python\ file in a new \pycharm\ project and to then run it (\ubuntu).}%
93+
\caption{The steps to create a new \python\ file in a new \pycharm\ project and to then run it (\ubuntu,~continued).}%
9394
\label{fig:veryFirstProgramC}%
9495
\centering%
9596
%

text/main/basics/gettingStarted/gettingStarted.tex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
If you are a student of computer science or any related field, then it is my personal opinion that you should get familiar with this operating system.
2424
Maybe you could start with the very easy-to-use \ubuntu\ \linux~\cite{CN2020ULB}.
2525
Either way, in the following, I will try to provide examples and instructions for both \ubuntu\ and the commercial Microsoft \windows~\cite{B2023W1IO} \pgls{OS}.
26+
27+
Once the necessary software is installed, we will here also learn how to write and execute our very first small \python\ program in \cref{sec:ourFirstProgram}%.
28+
This first program will just print \inQuotes{Hello World!} and then exit.
29+
As final step to get our environment up and running, we discuss how \python\ programs can be executed in the terminal and the \python\ console in \cref{sec:pythonInTheTerminal}%
2630
%
2731
\hinput{installingPython}{installingPython}%
2832
\hinput{installingPyCharm}{installingPyCharm}%
29-
\clearpage%
3033
\hinput{firstProgram}{firstProgram}%
34+
\hinput{pythonInTheTerminal}{pythonInTheTerminal}%
35+
\hinput{summary}{summary}%
3136
%
3237
\endhsection%
3338
%

text/main/basics/gettingStarted/installingPyCharm/installingPyCharmWindows/installingPyCharmWindows.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
\label{fig:installingPyCharmWindows14installation}%
107107
]{\tightbox{\includegraphics[height=4cm]{\currentDir/installingPyCharmWindows14installation}}}%
108108
%
109-
\caption{The installation steps of \pycharm\ under \windows.}%
109+
\caption{The installation steps of \pycharm\ under \windows~(continued).}%
110110
\label{fig:installingPyCharmWindowsB}%
111111
%
112112
\end{figure}%
@@ -145,7 +145,7 @@
145145
]{\tightbox{\includegraphics[width=0.4\linewidth]{\currentDir/installingPyCharmWindows18running}}}%
146146
\strut\hfill\strut%
147147
%
148-
\caption[]{The installation steps of \pycharm\ under \windows.}%
148+
\caption[]{The installation steps of \pycharm\ under \windows~(continued).}%
149149
\label{fig:installingPyCharmWindowsC}%
150150
\end{figure}%
151151
%

text/main/basics/gettingStarted/installingPython/pythonUnderUbuntu/pythonUnderUbuntu.tex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
\begin{figure}%
44
\centering%
55
\includegraphics[width=0.7\linewidth]{\currentDir/ubuntuTerminalPythonVersion}%
6-
\caption{Under \ubuntu\ \linux~\softwareStyle{22.04}, typing \bashil{python3 --version} in a \pgls{terminal} and hitting return yields version~\softwareStyle{3.10}.}%
6+
\caption{Under my \ubuntu\ \linux~\softwareStyle{22.04} system, typing \bashil{python3 --version} in a \pgls{terminal} and hitting return yields version~\softwareStyle{3.10.12}.}%
77
\label{fig:ubuntuTerminalPythonVersion}%
88
\end{figure}%
99
%
1010
\begin{sloppypar}%
1111
Under \ubuntu\ \linux, \python~\softwareStyle{3} is already pre-installed.
1212
You can open a \pgls{terminal}, type \bashil{python3 --version}, hit \keys{\return}, and get the result illustrated in \cref{fig:ubuntuTerminalPythonVersion}:
13-
My system has \python\ version~\softwareStyle{3.10} installed.%
1413
\end{sloppypar}%
1514
%
1615
\endhsection%
Loading
Loading
Loading
Loading
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
%
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
\hsection{Summary}%
2+
%
3+
In this introductory section, we have performed the very first steps into the domain of \python\ programming.
4+
We have now a computer where both \python\ and the \pycharm\ \pgls{IDE} are installed.
5+
We can create program files and we can execute them in different ways.
6+
We are now ready to learn how to program.%
7+
%
8+
\endhsection%
9+
%

0 commit comments

Comments
 (0)