# Python Fundamentals

*Note: This lesson was written using Python 2.xx. If you are using Python 3.xxx any changes to the code will be annotated under headings: Python 3.xxx

Open up a new Jupyter Notebook. Remember, when working with Jupyter Notebooks, you need to hit Shift+Enter to execute your code. ## Arithmetic Operators

Python Arithmetic Operators are pretty standard. Take note of the division problem in line . 10/7 is not 1, but by default Python only shows integer answers You can work around it by defining the first number as a float() ### Python 3.xxx

Python 3 handles division differently.  To get integer numbers only in division, use //.

```10//7
1```
```10/7
1.4285714285714286```

## Arithmetic Operators

• – : Subtraction
• * : Multiplication
• / : Division
• % : Modulus (returns the remainder of a division problem: 5%2=1)
• ** : Exponent (4**2 = 16)

## Variables

Variables in Python are pretty straight forward. Unlike other programming languages, you do not need to define the variables first. Python dynamically assigns the data type.

Three main rules:

• Variables are case sensitive
• Avoid using command keywords (print, def, for) notice lowercase ‘a‘ returns an error

Remember, Jupyter notebooks only return the last command. If you want both variables, use the print command ### Python 3.xxx

In Python 3, print statements require ().

```print (A)
print (B)```

You can perform arithmetic functions on variables ### Python 3.xxx

`print (C)`

And of course, variables can hold strings as well as numbers ### Python 3.xxx

```print (E)
print (F)```

# Python: Fun with Central Tendency

Now numpy provides easy functions for finding central tendency – you can find them in my Numpy Part II lesson: Python: Numpy Part II.

But we have learned enough about Python so far, that maybe it would be more fun to build our own functions. In this lesson we are going to build our own statistics library with mean, median, mode, and quantile

## Our Data ## Mean or even easier: ## Median

Remember with median – if your data contains an odd number of elements (n%2==1), you just take the middle value. However, if your data contains and even number of elements (n%2==0) then you add the two middle numbers and divide by 2.

We handle that through the use of an if statement in our function. ## Mode

For mode, we want to find the most common element in the list. For this task, I will import Counter from collections.

d.most_common() returns each unique element and the number of times it appears

d.most_common(1) returns the or in function form: In my numpy part II lesson I use a more elegant solution, but I want you to see that there is always more than one way to do something in Python.

## Quantile

Quantiles are cut points in set of data. They can represent the bottom ten percent of the data or the top 75% or any % from 0 to 100.

In my solution, I am adding a slicing argument to the sorted() function. Below shows all elements up to (but not including) index 4 quantile function: If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.

# Python: Pandas, Working with DataFrames

Sure DataFrames look nice, but how can I work with them?

Let’s cover some basic tasks in pandas to get you started.

Let’s start by building a DataFrame I don’t like where they placed Age on my dataframe. I want to move it.

To do so, we are going to cover a couple of new terms: axis, drop() and insert()

### Axis

Using numpy and pandas, you will come across many functions that require you to enter an axis as a parameter. Axis 0 is your rows while Axis 1 is your columns. This is due to the way matrices are named with a 3×2 matrix having 3 rows and 2 columns and a 2×3 having 2 rows and 3 columns ### drop()

To move the age column, I am first going to create a copy of my dataframe minus the age column. To do this, I am going to use the drop() function. The drop() function accepts two arguments drop(name, axis). In our case name = ‘Age’ and axis = 1 since we are referring to a column. ### insert()

Now we want to insert the age column. The syntax for the insert() function is insert(insert point, name, data) Adding a new column is straight forward. Just DataFrame[new column name] = value.

Below I created at new column called ‘Age When Start’ that shows the age of employees when they started. I derived this value by subtracting Years Service column from Age column. ## boolean column

You can create a boolean column using a boolean operator. ## sort()

You can sort a dataframe by any column using sort_values() Sort is set to ascending by default. To reverse it, set ascending = False

** remember in Python, True and False need to start with a capital letter. ## slicing

### by rows

slicing by rows is just like with a list ### by columns

slicing by columns is a bit more complex. To slice by column name you have to use the dataframe.ix command. If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.

# Python: Numpy Part II

Beyond numpy’s usefulness in creating arrays and matrices, numpy also provides a great suite of math functions that – for anyone with any programming background – are fairly intuitive.

Here are some examples:

np.pi returns pi and np.sqrt() takes the square root of whatever value you feed it. ### Trig Functions

numpy handles most common trigonometry functions. ### Stats

Numpy handles many statistics functions.

Below we have mean and median. Unfortunately, just like in R, there is no mode command, but we can fake it using Set. using set to fake mode Numpy can also be used to find range, variance, and standard deviation. ### Rounding

Numpy has rounding features for dealing with decimals. It also has floor() and ceil() functions that bring the number down to the “floor” or up to the “ceiling” ## Use in creating graphs

use np.sin() use np.log() you can even put the two together ### linspace()

My final one today is a function called linspace(). It lets you create a start and finish point, and how many elements you want. It then will create a even list of number between start and finish.

linspace(start,finish, num=numbers you want) If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.

# Python: Create, Import, and Use a Module

Today we are going to cover what I consider a flaw in the iPython Notebook environment. Code you write cannot be imported into another program like you can using just a standard Python compiler. At least it cannot under its native .ipynb format.

There is a work around though. Starting jupyter notebooks up with the –script command results in your notebook being saved as both a ipynb file and a .py file. And .py is what we are looking for. note ‘–script‘ is being deprecated in favor of nbconvert. But as of this moment it still works and will work on older versions you have downloaded as well.

## Create a Module

I am going to create a little module call add2. This module contains 3 simple functions: sqr2, sub2, and add2 You can see that now along with my add2.ipynb file, I have a add2.py file. In my instance, my iPython notebooks are stored under my user directory. If you are not sure where you notebooks are, you can use the pwd command (print working directory) ** note the double \\ are due to a formatting function in Python. \ is a break command that can be used for many purposes. Example \t means to insert a tab. So effectively you have to use \\ if you want a \ in Python, making my working directory C:\Users\Benjamin

## Import the Module

Now if I open up a new notebook and try using the functions I have just created, I will error out. So, what I need to do is import add2, then I can start calling on the functions I created in add2 If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.

# Python: Error handling

No matter how well crafted your code, you will inevitably come across an error every once in a while. Unfortunately, Python’s default setting when catching an error is to crash the program.

In the example below, I accidentally asked for a letter, when the code int(raw_input()) is looking for an integer.

So when I enter a string, I get an error ## Try

Using Try, we can handle such mishaps in a more elegant fashion

The syntax is a follows:

```try:
except:
What to do if your code errors out
else:
What to do if your code is successful``` And as you can see from above, it doesn’t just protect against code errors, but it protects against user errors as well.

Using a while loop, we can give the user another chance if they don’t enter a number the first time.

Code explanation

• while True:  – starts an infinite loop
• continue – returns to the beginning of the while loop
• break – exits the loop ## Finally

You can add the finally: command to the end of a try: statement if you have something you want to execute regardless of whether there was an error or not. ## Exception types:

You can also determine what your program does based on the type of error

ValueError ZeroDivisionError No Error A list of Exception types can be found here: Link

If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.

# Python: Print Variables and User Input

## Print

The basic Print statement in Python is Print “” – note Print ” also works. You can choose either single or double quotes, but you cannot mix them in the same statement.

## Print Variables

Printing variables in Python is a bit more complicated.

If the variable you want to print is a number, use the special string %d inside the quotes and % variable outside the quotes.

If you have more than one variable, you need to have a %d for each variable. Then place your variables inside () at the end of the print line. T

To print a string, you use %s. Notice below you can mix strings and numbers in the same line. You can use %r if you are not sure what value the variable will have. It will print both.

## User Input

raw_input() allows you ask the user for input.

You can assign the user input to a variable.

You can also pre-define what kind of input you want. Note the error in the second instance. x = int(raw_input()) means I am looking for an integer as input. If I enter an integer, I do not get the error. If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.

# Python: Line Graph

Let’s build on what we learned in : Python: Intro to Graphs

First, import pyplot from matplotlib

Remember %matplotlib inline let’s you see your graphs in the jupyter notebooks ## Line 1

Note I am using a list comprehension to fill the x axis. Now, let us do some formatting:

• ‘r:’    –   red dotted line
• label = ‘up’   –  adds label to the line
• py.legend(loc=9)  – adds a legend to the chart ## Line 2

For line 2, I want to create a reverse curve:

• d = u  – list u is copied to d
• d.reverse() – reverses values in d Plot the line.

• ‘g-.’ – green dashed line Combine the two lines ## Line 3

Here I use another list comprehension  and zip to make our last like. This adds each element of list u and d in order.

• ‘b-‘ – blue solid line If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.

# Python: Pandas Intro (Dataframes)

Before we continue onto Dataframes, I want to clear up something from the Series exercise. Note the line (from pandas import Series, DataFrame) Using that line, I can call upon Series or DataFrame directly in my code In this example below, I did not directly import the methods Series and DataFrame, so I when I tried x = Series() I go an error.

I had to use the full method name of pd.Series() for this to work. ## DataFrame

DataFrames provide another level of data management for Python. Those of you who come from a more data driven background with appreciate DataFrames.

Let’s start by creating dictionary. Now, pass the dictionary to the method DataFrame()

Note, now you have a table looking structure with named columns You can call up a list of indexes or columns using the methods below: DataFrame.info() will return a summary of your DataFrame Create a new DataFrame from a dictionary If you want just see a few of the first elements, you can use the head() method The tail() method does the last few. You can even choose how many rows you want. ## Describe

The describe() method gives you some quick statistics on any numeric column ## Slice

You can slice a DataFrame just as you would a list ## Choose What to Display

DataFrames allow you to filter what rows to display by value or column There is a lot more you can do with Series and DataFrame in pandas, and we will be covering them in later lessons. For now though, I think you have a general idea.

If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.

# Python: Pandas Intro (Series)

Pandas adds some great data management functionality to Python. Pandas allows you to create series and dataframes.

## Series

Think of a series as combination of a list and a dictionary.

To use pandas, first we need to import it. To create as series with pandas, use the following syntax

• variable = Series([item1, item2, … item_n])

** note Series is capitalized. This method is case sensitive

pandas Series can contain numbers or strings. You call elements in a Series like you do a list. variable[index] You can create you own index in a Series as well, making it function a lot like a dictionary.

Notice with the syntax leg[leg==4] I am able to filter by items equal to 4 If you have an existing dictionary, you can convert it into a pandas Series You can call the index and values of a Series using the following methods: If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT.