D. Using Python

There are many different ways to use Python. The “simplest” way is to use a text editor and terminal. However, projects like IPython and Jupyter have enhanced Python’s REPL (Read–Evaluate–Print–Loop) interface, making it one of the standards interfaces in the data analytics and scientific Python communities.

D.1 Command Line and Text Editor

To use Python from the command line and text editor, all you need is a plain text editor and a terminal. Although any plain text editor would work, a “good” one would have a Python feature that will do syntax highlighting and auto-completion. Popular multiple-platform text editors include Sublime Text1 and Atom.2 Textmate3 and TextWrangler4 are other popular text editors for Macs. Notepad++5 is another option for Windows users.

1. www.sublimetext.com/3

2. https://atom.io/

3. https://macromates.com/

4. www.barebones.com/products/textwrangler/

5. https://notepad-plus-plus.org/

If you are on Windows, be careful not to do too much editing using the default Notepad application, especially if you plan to collaborate with users on other operating systems. Line endings in Notepad are different from those in Windows and on *nix machines (Linux and Macs). If you ever open up a Python file and the indentations and newlines do not appear correctly, it’s probably because of how Windows is interpreting the newline endings of the file.

When you work in a text editor, all your Python code will be saved in a .py script. You can run the script by executing it from the command line. For example, if your script’s name is my_script.py, you can execute all the code in the script, line-by-line, with the following command:

$ python my_script.py

More information about running Python scripts from the command line is found in Appendices B and E.

D.2 Python and IPython

Under Windows, Anaconda will provide a “Anaconda command prompt.” This is just like the regular windows command prompt, but is configured to use the Anaconda Python distribution. Typing python or ipython here will open the python or ipython command prompt, respectively.

For OSX and Linux, you can run the python or ipython command prompt by typing the respective command in a terminal.

There are a few differences between the python and ipython command prompts. The regular python prompt takes only Python commands, whereas the ipython prompt provides some useful additional commands you can type to enhance your Python experience. My personal suggestion is to use the ipython prompt.

You can directly type Python commands into either prompt, or you can save your code in a file and then copy/paste commands into the prompt to run your code.

D.3 Jupyter

Instead of running python or ipython in the command prompt to run Python, you can run the jupyter notebook. This will open another Python interface in a web browser. Even though a web browser is opened, it does not actually need any Internet connection to run, nor is any information sent across the Internet.

The jupyter notebook will open in a location on your computer. You can create a new “notebook” by clicking the “new” button on the top right corner and selecting “python.” This will open up a “notebook” where you can type your python commands. Each cell provides a site where you can type your code, and you can run the cell by using the commands in the “Cell” menu bar. Alternatively, you can press Shift + Enter to run the cell and create a new cell below it, or press Ctrl + Enter to simply run the cell.

An especially useful aspect of the notebook is the ability to interweave your Python code, its output, and regular prose text.

To change the cell type, make sure you have the cell selected. Then, on the top right below the menu bar, click a drop-down menu that says “Code.” If you change this to “Markdown,” you can write regular prose text that is not Python code to help interpret your results, or record notes about what your code is doing.

D.4 Integrated Development Environments (IDEs)

Anaconda comes with an IDE called Spyder. Those who are familiar with Matlab or RStudio might take comfort in having access to a similar interface.

Other IDEs include the following:

1. rodeo: www.yhat.com/products/rodeo

2. nteract: https://nteract.io/

3. pycharm: www.jetbrains.com/pycharm/

I suggest exploring the various ways to use Python and seeing which works best for you. IPython/script, Jupyter notebook, and Spyder come pre-installed with Anaconda, so those would be the most accessible, but the other IDEs might work better for your particular circumstances.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.171.134