Setting up Python on your Computer

You can start working with Python by downloading and installing it on your system.

Python itself is available in two versions, namely, 2.x and 3.x versions. Python 2.x is legacy, while Python 3.x is the present and future of the language. While many people swear by Python 2.x versions, the whole development and community effort is moving towards version 3.x. In this course, we will be using python 3.x version.

Install Python

You can download Python from python.org (https://www.python.org/downloads/). There are separate downloadables available for different operating systems such as Windows, Mac OS, and Linux. Once downloaded, start the installer by double-clicking it and follow the prompts.

If you face any problem in installation, feel free to discuss any issues in the forum.

OS X and Linux come with Python already installed.

Install a Text Editor

When you create python programs, they are saved in files with the extension .py. While you can create Python programs in Notepad or TextEdit, you will find it much easier to read and write the code using a specialized text editor. There are a variety of free editors to choose from such as Notepad++ (Windows), TextWrangler (Mac), or Atom. There are also advanced Integrated Development Environments (IDEs) such as PyCharm which makes working on python projects very easy. To start with, I would suggest to just use Notepad++, Textwrangler, or Atom. Once you've become comfortable with writing python programs and are working on complex projects, you can move to using an IDE. My personal favourite is Atom.

Python Interface

Once you've installed Python, you can start using it. To do so, open Command Prompt (Windows) or Terminal (Mac/Linux) and type python. To open Command Prompt in windows, Press Windows key+R to open the Run dialog box, type cmd in the empty box, and then click OK or hit Enter to open it

Python will load and the version number will be displayed. You will be taken to the Python interpreter command prompt, shown as >>>.

Type print("Hello, World!") and press ↵ Enter. You should see the text Hello, World! displayed beneath the Python command line.

Use Python Interpreter as a Calculator

The best way to start getting used to the python interpretor is to use it as a calculator. You can use Python to perform basic arithmetic with ease. Below are some examples on how to use the calculator functions. Note: # designates comments in Python code, and they are executed by the interpreter.

>>> 2 + 5
7
>>> 100 - 5*3
85
>>> (100 - 5*2) / 3 # Division will always return a floating point (decimal) number
30.0
>>> 25 % 6 # This calculates the remainder of the division
1
>>> 7 ** 3 # Use ** to calculate power (7 raised to the power of 3)
343

Creating Python Programs

In Python, you can write your programs and then save them as Python (.py) files which can them be open and executed by the Python interpreter. To understand the files concept, let's write our own small program.

Windows

  1. Create a folder in your system where you would like to store your programs. For example, pythondatascience.
  2. Open up your text editor and create a new file called hello.py containing just this line: print('Welcome to Data Science!')
  3. Save your hello.py program in folder you just created.
  4. In the Start menu, select "Run...", and type in cmd. This will cause the Windows terminal to open. Type cd \pythondatascience to change directory to your pythondatascience folder, and hit Enter.
  5. Type python hello.py to run your program!

Mac

  1. Create a folder on your computer to use for your Python programs. For example, name it pythondatascience and place it in your Home folder (the one that contains folders for Documents, Movies, Music, Pictures, etc).
  2. Open up your text editor and create a new file called hello.py containing just this line:print('Welcome to Data Science!')
  3. Save your hello.py program in the folder you just created.
  4. Open the Applications folder, go into the Utilities folder, and open the Terminal program.
  5. Type cd pythondatascience to change directory to your pythondatascience folder, and hit Enter.
  6. Type python ./hello.py to run your program!

Result

The program should print:

Welcome to Data Science!

Congratulations! You've written your first Python program.

Exercises

  • Modify the hello.py program to greet someone.
  • Modify the program to print two statements instead of one.
  • Modify the program to print the statement 'The sum of 3 and 7 is: ' and then write the math formula 3+7 to print the final

Note: In Python 3+, You need to put the calculation within the print bracket, as shown below: print ("The sum of 3 and 5 is: ", (3+5)) In Python 2+, there are no brackets. So, it will be like below: print "The sum of 3 and 5 is:" + str(3+5)

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book includes PDFs, explanations, instructions, data files, and R code for all examples.

Get the Bundle for $29 (Regular $57)
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.