|
|
The first exercises were solved directly using the online interactive editor. This is handy but in most real life cases you need to be able to use your own installation of Python and execute scripts on your own computer or server. For this reason, some exercises are meant to be answered by creating and testing a script (*i.e.* a file, like `solution.py`) on your computer and then copy and paste its content within the interactive editor on the exercise page.
|
|
|
|
|
|
To do so you need to learn a few things:
|
|
|
- Install Python on your computer
|
|
|
- Know basic Command Prompt usage
|
|
|
- Create a script and execute it
|
|
|
|
|
|
If this seems trivial to you, just do as you're used to, what matters in what's inside your `solution.py` :)
|
|
|
|
|
|
# Install Python on your computer
|
|
|
|
|
|
We recommend [Anaconda Python](https://www.anaconda.com/download/), a nice packaging of Python with various packages and configurations added keeping scientific usages in mind. [Download](https://www.anaconda.com/download/) the **Python 3.7 version** installer and launch it on your computer. The default proposed options are ok except:
|
|
|
|
|
|
- Select the option "Add anaconda to my PATH environment variable"
|
|
|
- Refuse to install *Microsoft VS Code* when asked to.
|
|
|
|
|
|
# Command Prompt 101
|
|
|
|
|
|
To launch a Command Prompt, look for `cmd` in your Windows menu. You should see something like this:
|
|
|
|
|
|
![terminal](https://upload.wikimedia.org/wikipedia/commons/b/b3/Command_Prompt_on_Windows_10_RTM.png)
|
|
|
|
|
|
Welcome to your computer console ! You can do many things from here:
|
|
|
|
|
|
- know where you're in your computer arborescence with the command `cd`
|
|
|
- create a directory with `mkdir`
|
|
|
- list a directory with `dir`
|
|
|
- move somewhere with `cd`
|
|
|
- rename a file with `move`
|
|
|
|
|
|
```cmd
|
|
|
C:\Users\mazr>cd
|
|
|
C:\Users\mazr
|
|
|
|
|
|
C:\Users\mazr>cd Downloads
|
|
|
|
|
|
C:\Users\mazr\Downloads>dir
|
|
|
Volume in drive C is BOOTCAMP
|
|
|
Volume Serial Number is 0246-9FF7
|
|
|
|
|
|
Directory of C:\Users\mazr\Downloads
|
|
|
|
|
|
16/01/2019 12:04 <DIR> .
|
|
|
16/01/2019 12:04 <DIR> ..
|
|
|
16/01/2019 12:04 13 notes.txt
|
|
|
1 File(s) 13 bytes
|
|
|
2 Dir(s) 14.694.735.872 bytes free
|
|
|
|
|
|
C:\Users\mazr\Downloads>move notes.txt notes_with_new_name.txt
|
|
|
1 file(s) moved.
|
|
|
```
|
|
|
|
|
|
# Launch and use Python from a Command Prompt
|
|
|
|
|
|
Open a Command Prompt, type `python` and press enter. You should see something like this :
|
|
|
|
|
|
```python
|
|
|
C:\Users\mazr>python
|
|
|
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
|
|
|
Type "help", "copyright", "credits" or "license" for more information.
|
|
|
>>>
|
|
|
```
|
|
|
|
|
|
Welcome to the Python interpreter ! A nice place to test some python code ! Type `print("Hello")` and press `Return`:
|
|
|
|
|
|
```python
|
|
|
>>> print("Hello")
|
|
|
Hello
|
|
|
>>> import math
|
|
|
>>> print(math.pi)
|
|
|
3.141592653589793
|
|
|
```
|
|
|
|
|
|
The get to a new line, hit `Ctrl-c`. To leave the interpreter and return to your Command Prompt, hit `Ctrl-z` + `Return`.
|
|
|
|
|
|
# Create a Python script and execute it from the Terminal
|
|
|
|
|
|
Install a code editor like [Notepad++](https://notepad-plus-plus.org/download/v7.6.2.html). Open it and create a new file. Write the following lines:
|
|
|
|
|
|
```python
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
print("Hello World !")
|
|
|
```
|
|
|
|
|
|
(The first line is necessary for your computer to know this is a Python script.)
|
|
|
|
|
|
Save the file as `solution.py` in your Download folder.
|
|
|
|
|
|
Open a Command Prompt, go to your *Download* folder and execute your freshly composed Python script:
|
|
|
|
|
|
```cmd
|
|
|
C:\Users\mazr>cd Downloads
|
|
|
|
|
|
C:\Users\mazr\Downloads>python solution.py
|
|
|
Hello World !
|
|
|
```
|
|
|
|
|
|
Congrats ! You're now ready to answer exercises by submitting a file. |
|
|
\ No newline at end of file |