Function and Method.
As I wrapped up the datatype thread today, let’s look into the python function and method.
Let’s start with the basics. Say we have a variable:
a = ‘Hello!’
Here’s a simple example of a Python function:
Input: len(a)
Output: Result: 6
And below is an example for a Python method:
Input a.upper()
Output: Result: ‘HELLO!’
So what are Python functions and methods?
In essence, they transform something into something else. In this case, the input was ‘Hello!’ and the output was the length of this string (6), and then the capitalized version: ‘HELLO!’. Of course, these are not the only 2 functions you can use: plenty of them. Combining them will help you in every part of your data project — from data cleaning to machine learning.
♟️ Function in a program can take arguments (the data to operate on) and optionally return some data after performing the intended task. Creating a function is giving your computer well-defined instructions to perform a task. The function may be inbuilt or user-defined.
a. User-Defined Functions
Like any other programming language, Python lets us define our own functions. Let’s take an example.
>>> def add(a,b):
return a+b
>>> add(3,-3)
0
We call this function ‘add’. As expected, it adds two values, which it takes as arguments. When we call it with values -3 and 3, it returns 0. And as you can see, the ‘return’ statement returns a value from a Python function.
b. Built-in functions
These are functions that are automatic with python programming language. e. g.
abs()
Get Habeeb Abdulrauf’s stories in your inbox
Join Medium for free to get updates from this writer.
The abs() is one of the most popular Python built-in functions, which returns the absolute value of a number.
A negative value’s absolute is that value is positive.
Input : abs(-7)
Output
Output: 7
See how built-in function works here https://data-flair.training/blogs/python-built-in-functions/
♟️ Methods
Python method is like a function, except it is attached to an object. We call a method on an object, and it possibly makes changes to that object. A method, then, belongs to a class. Let’s take an example.
Method
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
from keys() Returns a dictionary with the specified keys and value
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key-value pair
keys() Returns a list containing the dictionary’s keys
pop() Removes the element with the specified key
pop item() Removes the last inserted key-value pair
set default() Returns the value of the specified key. If the key does not exist: insert the key with the specified value
update() Updates the dictionary with the specified key-value pairs
values() Returns a list of all the values in the dictionary
Function and Method Compared
• Python method is called on an object, unlike a function.
Conversely, we call Python function quite generically- we don’t call it on any object. Since we call a method on an object, it can access its data.
• A method may alter an object’s state, but the Python function usually only operates on it and then prints something or returns a value.
#pythonlearning #pythonchallenge #pythonfunctions #pythonmethods #habeebabdulrauf







