Python Confusion
Tags: Map, Method, Function, Filter, Python

function vs method , argument vs parameter, map vs filter, iterator vs iterable

01. function VS method:

function and method both do same job. All method are function but function is not method

function :

A function is a block of code which perform specific task and can be used as many times as need without writing the same code again. So, the main purpose of function is “code reusability”.

function should have a unique name. According to PEP-8 Function names should be lowercase, with words separated by underscores as necessary to improve readability. function parameter receive and assign them with arguments value.

function body contain specific task.

function structure

function example

Functions are two types :

pre-define functions user-define Function

Functions can be used inside the class as well as outside of the class.

method :

A method in python is similar to a function, except it is associated with object/classes.

method structure

method example

Use the function naming rules for method: lowercase with words separated by underscores as necessary to improve readability.

method is user defined only.

Methods are always used the Object Oriented Programming Language.

And always defined within a class.

02. arguments VS parameters

They are almost same but the main difference between them is that the parameter is declare when function is created, while arguments is passed when calling the function.

Here, a and b are parameters and and the arguments being passed through are 3 and 4.

In python parameters don’t need to specify its data type. Thus, we should control what exact type is passed through as an argument to the function.

03. iterable VS iterator :

An iterable is a object which has a __iter__() method. It can possibly iterated over several times, such as list() s and tuple() s.

An iterator is the object which iterates. It is returned by an __iter__() method, returns itself via its own __iter__() method and has a next() method ( __next__() in 3.x).

04. map VS filter :

map :

The map() function executes a specified function for each item in a iterable. The basic function of map() is to manipulate iterables. Each item is sent to the function as a parameter.

map syntax

iterable : You can send as many iterables as you like, just make sure the function has one parameter for each iterable.

map iterate over the iterables. Only takes one argument from each iterable. So, Number of function parameter must be equal to number of iterables.

two iterable used in map

map takes all items one by one from iterable and apply function over each item. Number of output in case of map is same as number of item in each iterable.

map

filter :

As the name suggests, it is used to filter the iterables as per the conditions.

The filter() function returns an iterator were the items are filtered through a function to test if the item is accepted or not.

Generally,

filter syntax

filter takes all items one by one from iterable and apply function over each item to check whether it is True or not. If True then that particualr item is seen. Number of output in case of filter is equal to the number of item output as True.

filter

In the above code, filter_1 output is same as input iterable ‘nums’. ‘num * num’ multiplication has not apply on iterable.

So, map takes all items in a iterable and permits you to apply a function to it though Filter takes all items in a iterable and runs that through a function to make another iterable with all items that output as True without any change to item.

References:

www.w3schools.com

www.greeksforgreeks.com

www.stackoverdlow.com