Skip to content

Commit ff7524b

Browse files
committed
Add sections on Python installation and VS Code
1 parent 303ff09 commit ff7524b

19 files changed

+176
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Installation
2+
3+
As described in the introduction, the Python compiler can be downloaded and installed directly from python.org. However, it will then be necessary to manually install the required libraries for engineering and scientific calculations. Hence, this way is not recommended. Please follow the instructions below, depending on your operating system:
4+
5+
6+
## Linux
7+
8+
In every Linux distribution Python is installed by default. Just make sure you have a proper package with Python 3 installed (e.g. in Ubuntu this is `python3`). You will also need some additional libraries: NumPy, SciPy, Matplotlib, and Pandas (`python3-numpy`, `python3-scipy`, `python3-matplotlib`, `python3-pandas`).
9+
10+
11+
## Windows
12+
13+
In Windows, the simplest solution is to install [Anaconda](https://www.anaconda.com/distribution/) environment, which already includes all the necessary libraries and a set of additional tools. Go to <https://www.anaconda.com/products/individual#Downloads> and download **Python 3.8**.
14+
15+
After downloading the installer, run it. You can leave the default settings. However, I suggest that during installation you make sure that the **All users** and **Register Anaconda as the system Python 3.8** options are checked. I also suggest to check **Add Anaconda to the system PATH environment variable** (ignore the installer warning). It will save some problems in the future...
16+
17+
![Anaconda installer](anaconda.png)
18+
19+
After successful installation, new items will appear in the Start menu:
20+
21+
![New items in the start menu](anaconda-start-menu.png)
22+
23+
24+
<hr/>
25+
26+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Programming Environment
2+
3+
You can write your Python programs in a Notepad or any text file editor. However, I strongly discourage to do so. It is much better, to use some programming environment. There is plenty of such environments that you can use with Python, e.g. Spyder, PyCharm, Visual Studio, Visual Studio Code...
4+
5+
## VS Code
6+
7+
You you do not have any familiar editor, I recommend [Visual Studio Code](https://code.visualstudio.com/) (VS Code), that is free and works in any operating system (Windows, Linux, Mac). You can download it from <https://code.visualstudio.com/Download>.
8+
9+
Once you have installed VS Code, you need to install the [Python extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python) from the Visual Studio Marketplace. For additional details on installing extensions, see [Extension Marketplace](https://code.visualstudio.com/docs/editor/extension-marketplace). The Python extension is named Python and it's published by Microsoft.
10+
11+
### Start VS Code in a project (workspace) folder
12+
13+
Using a command prompt or terminal, create an empty folder called ‘`hello`’, navigate into it, and open VS Code (`code`) in that folder (.) by entering the following commands:
14+
15+
```
16+
mkdir hello
17+
cd hello
18+
code .
19+
```
20+
21+
By starting VS Code in a folder, that folder becomes your “workspace”. VS Code stores settings that are specific to that workspace in `.vscode/settings.json`, which are separate from user settings that are stored globally.
22+
23+
Alternately, you can run VS Code through the operating system UI, then use **`File` > `Open Folder`** to open the project folder.
24+
25+
### Select a Python interpreter
26+
27+
Python is an interpreted language, and in order to run Python code and get Python help (called IntelliSense in the VS Code), you must tell VS Code which interpreter to use.
28+
29+
From within VS Code, select a Python 3 interpreter by opening the **Command Palette** (`Ctrl+Shift+P`), start typing the **`Python: Select Interpreter`** command to search, then select the command. You can also use the **`Select Python Environment`** option on the Status Bar if available (it may already show a selected interpreter, too):
30+
31+
![No interpreter selected](no-interpreter-selected-statusbar.png)
32+
33+
The command presents a list of available interpreters that VS Code can find automatically. If you don't see the desired interpreter, see VS Code manual on [Configuring Python environments](https://code.visualstudio.com/docs/python/environments).
34+
35+
Selecting an interpreter sets which interpreter will be used by the Python extension for that workspace.
36+
37+
### Create a Python Hello World source code file
38+
39+
From the File Explorer toolbar, select the **New File** button on the `hello` folder:
40+
41+
![File Explorer New File](toolbar-new-file.png)
42+
43+
Name the file `hello.py`, and it automatically opens in the editor:
44+
45+
![File Explorer hello.py](hello-py-file-created.png)
46+
47+
By using the `.py` file extension, you tell VS Code to interpret this file as a Python program, so that it evaluates the contents with the Python extension and the selected interpreter.
48+
49+
> **Note:** The File Explorer toolbar also allows you to create folders within your workspace to better organize your code.
50+
> You can use the **New folder** button to quickly create a folder.
51+
52+
Now that you have a code file in your Workspace, enter the following source code in `hello.py`:
53+
54+
```python
55+
msg = "Hello World"
56+
print(msg)
57+
```
58+
59+
When you start typing print, notice how IntelliSense presents auto-completion options.
60+
61+
![IntelliSense appearing for Python code](intellisense01.png)
62+
63+
### Run Hello World
64+
65+
It's simple to run `hello.py` with Python. Just click the **Run Python File in Terminal** play button in the top-right side of the editor.
66+
67+
![Using the run python file in terminal button](run-python-file-in-terminal-button.png)
68+
69+
The button opens a terminal panel in which your Python interpreter is automatically activated, then runs `python3 hello.py` (macOS/Linux) or `python hello.py` (Windows):
70+
71+
![Program output in a Python terminal](output-in-terminal.png)
72+
73+
There are three other ways you can run Python code within VS Code:
74+
75+
* Right-click anywhere in the editor window and select **`Run Python File in Terminal`** (which saves the file automatically):
76+
77+
![Run Python File in Terminal command in the Python editor](run-python-file-in-terminal.png)
78+
79+
* Select one or more lines, then press `Shift+Enter` or right-click and select **`Run Selection/Line in Python Terminal`**. This command is convenient for testing just a part of a file.
80+
81+
* From the Command Palette (`Ctrl+Shift+P`), select the Python: **`Start REPL`** command to open a REPL terminal for the currently selected Python interpreter. In the REPL, you can then enter and run lines of code one at a time.
82+
83+
84+
<hr/>
85+
86+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license.
87+
Source: <https://code.visualstudio.com/docs/python/python-tutorial>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Debugging your code
2+
3+
A debugger is a computer program used to test and debug other programs (in our case Python scripts). The main use of a debugger is to run the target program under controlled conditions that permit the programmer to track its operations in progress and monitor changes in computer resources (most often memory areas used by the target program or the computer's operating system) that may indicate malfunctioning code. Typical debugging facilities include the ability to run or halt the target program at specific points and display its state.
4+
5+
The terms “bug” and “debugging” are popularly attributed to Admiral Grace Hopper in the 1940s. While she was working on a Mark II computer at Harvard University, her associates discovered a moth stuck in a relay and thereby impeding operation, whereupon she remarked that they were “debugging” the system.
6+
7+
![First computer bug](first_computer_bug_1945.jpg)
8+
<span style="font-size: 80%">Source: U.S. Naval Historical Center Online Library Photograph NH 96566-KN</span>
9+
10+
## Configure and run the debugger in VS Code
11+
12+
Let's now try debugging our simple Hello World program.
13+
14+
First, set a breakpoint on line 2 of `hello.py` by placing the cursor on the print call and pressing `F9`. Alternately, just click in the editor's left gutter, next to the line numbers. When you set a breakpoint, a red circle appears in the gutter.
15+
16+
![Setting a breakpoint in hello.py](breakpoint-set.png)
17+
18+
Next, to initialize the debugger, press `F5`. Since this is your first time debugging this file, a configuration menu will open from the Command Palette allowing you to select the type of debug configuration you would like for the opened file.
19+
20+
![Debug configurations](debug-configurations.png)
21+
22+
For now, just select *`Python File`*, which is the configuration that runs the current file shown in the editor using the currently selected Python interpreter.
23+
24+
The debugger will stop at the first line of the file breakpoint. The current line is indicated with a yellow arrow in the left margin. If you examine the **Local** variables window at this point, you will see now defined `msg` variable appears in the Local pane.
25+
26+
![Debugging step 2 - variable defined](debug-step-02.png)
27+
28+
A debug toolbar appears along the top with the following commands from left to right: continue (`F5`), step over (`F10`), step into (`F11`), step out (`Shift+F11`), restart (`Ctrl+Shift+F5`), and stop (`Shift+F5`).
29+
30+
![Debugging toolbar](debug-toolbar.png)
31+
32+
The Status Bar also changes color (orange in many themes) to indicate that you're in debug mode. The *Python Debug Console* also appears automatically in the lower right panel to show the commands being run, along with the program output.
33+
34+
To continue running the program, select the continue command on the debug toolbar (`F5`). The debugger runs the program to the end.
35+
36+
> **Tip:** Debugging information can also be seen by hovering over code, such as variables. In the case of `msg`, hovering over the variable will display the string `Hello world` in a box above the variable.
37+
38+
You can also work with variables in the Debug Console (If you don't see it, select **Debug Console** in the lower right area of VS Code, or select it from the **...** menu.) Then try entering the following lines, one by one, at the **>** prompt at the bottom of the console:
39+
40+
```python
41+
msg
42+
msg.capitalize()
43+
msg.split()
44+
```
45+
46+
![Debugging step 3 - using the debug console](debug-step-03.png)
47+
48+
Select the blue **Continue** button on the toolbar again (or press `F5`) to run the program to completion. “Hello World” appears in the Python Debug Console if you switch back to it, and VS Code exits debugging mode once the program is complete.
49+
50+
If you restart the debugger, the debugger again stops on the first breakpoint.
51+
52+
To stop running a program before it's complete, use the red square stop button on the debug toolbar (`Shift+F5`), or use the **`Run` > `Stop`** debugging menu command.
53+
54+
> You should use the debugger often to understand how your code works and what operations are executed. Later, use it for finding errors in your code. Remember that this is normal that every program has many bugs in the beginning. An important part of the programming skill is ability to find them and fix them.
55+
56+
57+
<hr/>
58+
59+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license.
60+
Source: <https://code.visualstudio.com/docs/python/python-tutorial>
25.2 KB
Loading
20.2 KB
Loading
2.45 KB
Loading
75.4 KB
Loading
70.5 KB
Loading
19.6 KB
Loading
1.82 KB
Loading

0 commit comments

Comments
 (0)