Python Tutorial: Functions, Methods, and Libraries

Want to learn more? Take the full course at at your own pace. More than a video, you’ll learn hands-on coding & quickly apply skills to your daily work.

In this lesson, we will discuss the functions, methods, and importing external libraries.

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.

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.

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.

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”.

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.

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.

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 give the DataFrame object.

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 shorthand 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.

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.

Now let’s practice.

Post Author: hatefull