Methods in Python

You learned that there might be different object types in a Python program such as integers, floats, strings, lists, and even more sophisticated objects such as pyplot which you can see being used in the code below. So, just like an integer type is designed to represent whole numbers, a pyplot is designed to represent a data plot.

👉Now, every object type has its own methods. 

👉Methods are functions that are tied to an object and methods manipulate the object they are tied to. 

Functions are not tied to any particular object. For example, the print function is independent. You can pass any object to it (e.g., an integer, list, etc) and print will print it out. This is not the case with methods.

In the code below we have five different methods. These are title, xlabel, ylabel, plot, and show. All of them only work in conjunction with pyplot objects. In other words, those methods belong to pyplot.

For example, xlabel is a method designed to place a label to the x-axis of a plot. It wouldn't make sense to have an xlabel method for a list object. List objects have their own methods. 

But, how can we know what methods an object has? You will learn that in the next lecture.

Next Lecture
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.