Python functions

WHY ARE SOME FUNCTIONS USED IN THE FORM

E.G LEN(LIST)
AND SOME
E.G string.title()

Trying to understand this in detail can quickly cause you to go down the rabbit hole of how Python code works internally. Before I provide any broad overview, I would suggest not to focus too much on this especially if you have not learnt about Python Classes yet.

If you understand the basics of Classes, you can go ahead.

In Python, broadly speaking, everything is an object. And each object has its attributes and methods. For example, if you have a Class called Student, and it has a method called get_name() which prints out the name of the student . Then you can instantiate an object of that Class as follows -

student1 = Student("my name")

And now, you can print out the name as - student1.get_name() which will output my name. This is just a very broad overview, so I am not going into how the Class was constructed. But if you understand how Classes work it shouldn’t be difficult for you to grasp this.

That’s what happens with something like string.title().

a = "hello"
a.title()

a, in Python, will be essentially an object belonging to the String data type Class, and the String Class has a method called title() which you can access using the object a.

Python also has built-in functions which you can call directly on a specific object. For example, len(list_name), where len() is a built-in function. You can find more of them here

a = [1, 2, 3]
len(a)

More detail than this is not really required. As you work more and more in Python, you will learn about more and more built-in functions, and functions corresponding to specific data types (like string or integer etc).

In short , python uses both functions and methods.

Case 1 : when a parameter is passed.

len(dataframe) -> Function

Case 2 : When it called with the help of an object.

dataframe.count() -> method

Hello @firasmalik, Welcome to the DataQuest Community.!!

This is a Function.

function is a sequence of statements that execute in a certain order, given a name.

This is a method.

method is like a function, except it is attached to an object .


Read More:




This post was flagged by the community and is temporarily hidden.