Today I completed Functions and Packages Module 3 of the self paced EdX class Microsoft: DAT208x Introduction to Python for Data Science.
This was almost as much work as module 2 (lists), which is good because it means I’m learning new things.
The short summary of this module is:
Functions – Reusable bits of Python code used a particular task. If you can think of a particular task, there is likely a function for it.
Methods – Subclass of functions tied to a specific type of Python object. Called with a “.” after a variable name. Here’s an example of the difference using the variable “room” with the value “poolhouse”
Function – print(room) – this prints “poolhouse”
Method – room.count(“o”) – This counts the number of times the letter “o” appears in the variable “room” whose value is currently set to “poolhouse”. If we used the command:
print(room.count(“o”)), we would get “3”, the number of times that the letter o appears in poolhouse.
Packages – These are directories of Python functions and methods. Because there are a large number of discipline specific packages for Python, the basic distribution of Python doesn’t have them all. There is a tool called “pip” you can use to download and install packages you need for your work. According to the instructor for this course, three common packages needed for data science are Numpy, Matplotlib and Scikit-learn. Numpy and Matplotlib have their own separate modules in this class.
Another interesting thing about packages is that installing them into your programming environment isn’t enough. There are one and usually two more things you need to do in your code itself:
- Have a line that imports the package (or subpackage, or function)
- If you’ve imported the entire package, you’ll need to preface the function with the package name.
So if I want to use the radians function from the math package to determine the number of radians in 12 degrees, I’d need these two lines in my program:
If you don’t want to put “math.” in front of radians, Python lets you import single functions. So I could execute the radians command as:
from math import radians
But doing this method can be confusing to others looking at your code, particularly with longer programs. So I’ll probably import full packages. Not sure what this does to my actual program length. I’ll look into that later.
With this module, I’ve become convinced of the value of downloading Python to my home computer and working on it further. I’ve got other things I need to do today, but will try to start setting up my coding environment this week.
Aside from the constant ads from Datacamp during the lab portion of this course, I’m really liking the course organization of short lectures followed by hands on exercises. It makes me feel like I’m getting stuff done. There should be some way I could start doing training videos in a similar way for database or tech training – though I’m not sure how I’d get the hands-on piece.