The Python programming language

Python is another programming language that is used for statistical computing and graphics. We can use Python in R Markdown by clicking the insert button on the top left of the screen and selecting Python.

Before we start using Python, we need to make sure that the package reticulate is installed and loaded. To do this, run the code chunk below.

We also need to run this code chunk so R knows what version of Python we are using and what modules in Python we want to load. Modules in Python are sort of like packages in R. We have to import modules so we can use different functions.

In this code chunk, we are importing the pandas module. We will call the pandas module pd to make our lives a little easier.

import pandas as pd

When we want to use a function attached to a specific module in Python, we must make sure we use the module name when we use the function. For instance, we want to use read_csv which is a part of the pandas module. Because of this, we write pd.read_csv because we are calling pandas pd.

data = pd.read_csv('cars.csv')

In our Introduction to R module, we used the summary function on the data set cars. The describe function in Python is similar to the summary function in R. The code below is very different from R. In our, we would write describe(data). Python is a little different because we write the object name first and then add the function name after a period after the object name.

data.describe()
##        Unnamed: 0      speed        dist
## count    50.00000  50.000000   50.000000
## mean     25.50000  15.400000   42.980000
## std      14.57738   5.287644   25.769377
## min       1.00000   4.000000    2.000000
## 25%      13.25000  12.000000   26.000000
## 50%      25.50000  15.000000   36.000000
## 75%      37.75000  19.000000   56.000000
## max      50.00000  25.000000  120.000000

When we run the code chunk above, we see a few statistics on speed and dist. To find the Min, 1st Quartile, Median, 3rd Quartile, and Max like we did in R, we look at min, 25%, 50%, 75%, and max respectively. Notice that these numbers are the same that we got in our R module.

Operations

Like R, Python allows you to do simple operations. You can find the value of these operations by running the individual code chunks. Notice that all of the operations in Python are the same as they are in R except for powers. If we want to raise 2 to the fifth power in R we would write 2^5. In Python, if we wanted to raise 2 to the fifth power, we would write 2 ** 5.

Examples

Add 3 and 4.

3 + 4
## 7

Subtract 2 from 7.

7 - 2
## 5

Multiply 4 by 5.

4 * 5
## 20

Divide 15 by 3.

15 / 3
## 5.0

Find the value of 2 cubed.

2 ** 3
## 8

Practice

1.) Add 5 and 2.

2.) Multiply 6 and 4.

3.) Find the value of 4 squared.

4.) Divide 36 by 6.

5.) Subtract 4 from 17.