PPOL564 - Data Science I: Foundations

Data Types in Python

Being Pythonic

Whitespace is significant
Everything is an object
Aim for readablility

Follow PEPs

  • Updates to python are recorded in Python Enhancement Proposals (or PEPs)
    • When there is a change to python, it is recorded here
    • also the python "philosophy" lives here in its suggestions (e.g. PEP8 re: spacing)
In [1]:
# Python has a sort of philosophy to it. 
import this 
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Modules: Importing Functionality

Standard Library

Python comes with an extensive standard library and built-in functions.

Some examples of these modules (to name a few...)

math for mathematical computations

In [110]:
import math
math.log(100)
Out[110]:
4.605170185988092

re for string computations

In [111]:
import re
my_string = "this is a dog"
re.sub("this","That",my_string)
Out[111]:
'That is a dog'

random for random number generation

In [112]:
import random
random.randint(1, 10)
Out[112]:
5

datetime for dealing with dates

In [113]:
import datetime
date1 = datetime.date(year=2009,month=1,day=13)
date2 = datetime.date(year=2010,month=1,day=13)
date2 - date1
Out[113]:
datetime.timedelta(days=365)

Importing Modules

Excerpt from Real Python post

Modular programming refers to the process of breaking a large, unwieldy programming task into separate, smaller, more manageable subtasks or modules. Individual modules can then be cobbled together like building blocks to create a larger application.

There are several advantages to modularizing code in a large application:

  • Simplicity: Rather than focusing on the entire problem at hand, a module typically focuses on one relatively small portion of the problem. If you’re working on a single module, you’ll have a smaller problem domain to wrap your head around. This makes development easier and less error-prone.

  • Maintainability: Modules are typically designed so that they enforce logical boundaries between different problem domains. If modules are written in a way that minimizes interdependency, there is decreased likelihood that modifications to a single module will have an impact on other parts of the program. (You may even be able to make changes to a module without having any knowledge of the application outside that module.) This makes it more viable for a team of many programmers to work collaboratively on a large application.

  • Reusability: Functionality defined in a single module can be easily reused (through an appropriately defined interface) by other parts of the application. This eliminates the need to recreate duplicate code.

  • Scoping: Modules typically define a separate namespace, which helps avoid collisions between identifiers in different areas of a program. (One of the tenets in the Zen of Python is Namespaces are one honking great idea—let’s do more of those!)

Functions, modules and packages are all constructs in Python that promote code modularization.

In [114]:
import sys
In [115]:
import numpy as np
In [116]:
from sklearn import metrics

Installing Modules

Using PiPy

In [117]:
!pip install numpy
Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (1.15.1)
You are using pip version 18.0, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Using Anaconda

In [118]:
!conda install numpy
/bin/sh: conda: command not found