1. Functions, methods, and libraries
In this lesson, we will discuss functions, methods, and importing external libraries.
2. Calling functions
Functions work similarly in both R and Python. One important thing to note is that R relies heavily on functions, whereas Python relies on both functions and methods.
3. Methods vs functions
Methods and functions have similar behavior, but they are different. The differences stem from the fact that Python is an object-oriented programming language. This means that all variables, lists, and other data structures that you create are objects and these objects can have attributes and methods associated with them. We'll talk about attributes in a later chapter.
Methods are functions that an object calls, whereas you pass objects into functions.
Recall you passed "L", a list, to the function len(), rather than calling the method len() on "L".
Did you notice the change in syntax? Let's discuss this.
4. Periods have a special meaning in Python
Periods have a very specific meaning in Python. You can't use a period in a variable or a function name because periods are used in several important contexts. We will talk about these throughout this course.
5. Append to list
In Python, you can add elements to a list using the append() method. Note the syntax, you call the append() method on the list "L" using the dot notation. Inside the parentheses you pass the element to be added to the list.
Note that you are not passing the list "L" to append(). Rather, you are calling append() on the list "L".
6. Update a dictionary
For dictionaries, you can call the update() method, note the dot notation to call the method.
You pass update() a dictionary of key-value pairs. If the keys already exist, they will be updated with the new value, if not, new keys will be added to the dictionary.
7. Methods are specific to the object
Methods are special functions that belong to specific objects. If you try calling append() on a dictionary object, you will get an error because the method append() is not defined for a dictionary, but it is defined for a list.
8. Libraries
The array and DataFrame objects which are widely used in Data Science are not available in Python by default. You need to import separate libraries for these objects. NumPy gives you the array and matrix objects, and pandas gives the DataFrame object.
9. Numpy ndarray
When you load a library in R, you can use any of the functions from the library directly.
Things work a little differently in Python. First, to load the numpy library, you use the `import` keyword.
Now in order to access any function from numpy, you have to use numpy dot function name.
For example, to use the loadtxt() function, you call numpy dot loadtxt.
Writing library dot function name often can become tedious, so you have the option of using an alias.
An alias is a short hand notation of representing a library. To use an alias, you use the "as" keyword.
It is common practice to use the alias np for numpy. So, to import numpy using the alias np, you write import numpy as np. Now you can access any function from numpy using np dot function name instead of numpy dot function name.
10. Pandas DataFrame
Moving on, the pandas library gives you the DataFrame object. A common alias for pandas is `pd`.
You can use the read_csv() function from pandas to import a csv file. df is now a pandas DataFrame object which has several methods and attributes associated with it. For example, you can use the head() method to view the first 5 rows.
11. Let's practice!
Now let's practice.