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, a nice packaging of Python with various packages and configurations added keeping scientific usages in mind. 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:
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
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 :
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:
>>> 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++. Open it and create a new file. Write the following lines:
#!/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:
C:\Users\mazr>cd Downloads
C:\Users\mazr\Downloads>python solution.py
Hello World !
Congrats ! You can now try to answer your current exercise using your own Python installation and copy and then paste the content of your script in the interactive editor on the exercise page.
