Python installation
In this section, we will discuss how to install Python. More specifically, we will discuss two methods: installing Python via Anaconda and installing Python directly.
There are several reasons why the first method is preferred:
- First, we can use a Python editor called Spyder, which is quite convenient for writing and editing our Python programs. For example, it has several windows (panels): one for the console, where we can type our commands directly; one for the program editor, where we can write and edit our programs; one for Variable Explorer,where we can view our variables and their values; and one for help, where we can seek help.
- Second, different colors for codes or comment lines will help us avoid some obvious typos and mistakes.
- Third, when installing Anaconda, many modules are installed simultaneously. A module is a set of programs written by experts, professionals, or any person around a specific topic. It could be viewed as a toolbox for a specific task. To speed up the process of developing new tools, a new module usually depends on the functions embedded in other, already developed modules. This is called module dependency. One disadvantage of such a module dependency is how to install them at the same time. For more information about this, see Chapter 2, Introduction to Python Modules.
Installation of Python via Anaconda
We could install Python in several ways. The consequence is that we will have different environments for writing a Python program and running a Python program.
The following is a simple two-step approach. First, we go to http://continuum.io/downloads and find an appropriate package; see the following screenshot:
For Python, different versions coexist. From the preceding screenshot, we see that there exist two versions, 3.5 and 2.7.
For this book, the version is not that critical. The old version had fewer problems while the new one usually has new improvements. Again, module dependency could be a big headache; see Chapter 2, Introduction to Python Modules for more detail. The version of Anaconda is 4.2.0. Since we will launch Python through Spyder, it might have different versions as well.
Launching Python via Spyder
After Python is installed via Anaconda, we can navigate to Start (for a Windows version) |All Programs |Anaconda3(32-bit), as shown in the following screenshot:
After we click Spyder, the last entry in the preceding screenshot, we will see the following four panels:
The top-left panel (window) is our program editor, where we write our programs. The bottom-right panel is the IPython console, where we cantype our simple commands. IPython is the default one. To know more about IPython, just type a question mark; see the following screenshot:
Alternatively, we could launch Python console by clicking Consoles on the menu bar and then Open a Python console. After that, the following window will appear:
From the image with four panels, the top-right panel is our help window, where we can seek help. The middle one is called Variable Explorer, where the names of variables and their values are shown. Depending on personal preference, users will scale those panels or reorganize them.
Direct installation of Python
For most users, knowing how to install Python via Anaconda is more than enough. Just for completeness, here the second way to install Python is presented.
The following steps are involved:
- First, go to www.python.org/download:
- Depending on your computer, choose the appropriate package, for example, Python version 3.5.2. For this book, the version of Python is not important. At this stage, a new user could just install Python with the latest version. After installation, we will see the following entries for a Windows version:
- To launch Python, we could click
IDLE (Python 3.5. 32 bit)
and get to see the following screen: - From the IPython shown in the screenshot with four panels, or from the Python console panel or from the previous screenshot showing Python Shell, we could type various commands, as shown here:
>>>pv=100 >>>pv*(1+0.1)**20 672.7499949325611 >>> import math >>>math.sqrt(3) 1.7320508075688772 >>>
- To write a Python program, we click File, then New File:
- Type this program and then save it:
- Click Run, then Run module. If no error occurs, we can use the function just like other embedded functions, as shown here: