Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python for Finance

You're reading from   Python for Finance Apply powerful finance models and quantitative analysis with Python

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher
ISBN-13 9781787125698
Length 586 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Yuxing Yan Yuxing Yan
Author Profile Icon Yuxing Yan
Yuxing Yan
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Python Basics FREE CHAPTER 2. Introduction to Python Modules 3. Time Value of Money 4. Sources of Data 5. Bond and Stock Valuation 6. Capital Asset Pricing Model 7. Multifactor Models and Performance Measures 8. Time-Series Analysis 9. Portfolio Theory 10. Options and Futures 11. Value at Risk 12. Monte Carlo Simulation 13. Credit Risk Analysis 14. Exotic Options 15. Volatility, Implied Volatility, ARCH, and GARCH Index

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:

Installation of Python via Anaconda

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:

Launching Python via Spyder

After we click Spyder, the last entry in the preceding screenshot, we will see the following four panels:

Launching Python via Spyder

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:

Launching Python via Spyder

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:

Launching Python via Spyder

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:

  1. First, go to www.python.org/download:
    Direct installation of Python
  2. 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:
    Direct installation of Python
  3. To launch Python, we could click IDLE (Python 3.5. 32 bit) and get to see the following screen:
    Direct installation of Python
  4. 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
    >>>
    
  5. To write a Python program, we click File, then New File:
    Direct installation of Python
  6. Type this program and then save it:
    Direct installation of Python
  7. Click Run, then Run module. If no error occurs, we can use the function just like other embedded functions, as shown here:
    Direct installation of Python
You have been reading a chapter from
Python for Finance - Second Edition
Published in: Jun 2017
Publisher:
ISBN-13: 9781787125698
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime