How do you build a custom function in Python?

0 votes

How do you build a custom function in Python?

Aug 24, 2018 in Data Analytics by Anmol
• 1,620 points
30 views

2 answers to this question.

0 votes

def method-

Structure of the function:

def func(arg1,arg2 …):

statement 1

statement 2

return value

Example- To determine mean of a list of values.

def find_mean(given_list):

sum_values= sum(given_list)

num_values= len(given_list)

return sum_values/num_values

print find_mean([i for i in range(1,9)])

answered Aug 24, 2018 by Anmol
• 3,620 points
0 votes

Function arguments in Python
In Python, Custom functions can take four different types of arguments. The argument types and their meanings, however, are pre-defined and can’t be changed. But a developer can, instead,  follow these pre-defined rules to make their own custom functions. The following are the four types of arguments and their rules.

1. Default arguments:
Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during function call. The default value is assigned by using assignment (=) operator. Below is a typical syntax for default argument. Here, msg parameter has a default value Hello!.

Function definition
def defaultArg( name, msg = "Hello!"):
Function call
defaultArg( name)


2. Required arguments:
Required arguments are the mandatory arguments of a function. These argument values must be passed in correct number and order during function call. Below is a typical syntax for a required argument function.

Function definition
def
requiredArg (str,num):
Function call

requiredArg ("Hello",12)


3. Keyword arguments:
Keyword arguments are relevant for Python function calls. The keywords are mentioned during the function call along with their corresponding values. These keywords are mapped with the function arguments so the function can easily identify the corresponding values even if the order is not maintained during the function call. The following is the syntax for keyword arguments.

Function definition
def
keywordArg( name, role ):
Function call

keywordArg( name = "Tom", role = "Manager")
or

keywordArg( role = "Manager", name = "Tom")


4. Variable number of arguments:
This is very useful when we do not know the exact number of arguments that will be passed to a function. Or we can have a design where any number of arguments can be passed based on the requirement. Below is the syntax for this type of function call.

Function definition
def varlengthArgs(*varargs):
Function call
varlengthArgs(30,40,50,60)
Now that we have an idea about the different argument types in Python. Let’s check the steps to write a  Custom function.

Writing  Custom functions in Python:
These are the basic steps in writing  Custom functions in Python. For additional functionalities, we need to incorporate more steps as needed.

Step 1: Declare the function with the keyword def followed by the function name.
Step 2: Write the arguments inside the opening and closing parentheses of the function, and end the declaration with a colon.
Step 3: Add the program statements to be executed.
Step 4: End the function with/without return statement.
The example below is a typical syntax for defining functions:

def userDefFunction (arg1, arg2, arg3 ...):
    program statement1
    program statement3
    program statement3
    ....
   return;

answered Sep 3, 2018 by shams
• 3,580 points

Related Questions In Data Analytics

0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
119 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE) ...READ MORE

answered Aug 14 in Data Analytics by anonymous
64 views
0 votes
1 answer
0 votes
7 answers
0 votes
2 answers

What is difference between Distributed search head and Search head cluster?

 A distributed environment describes the separation of ...READ MORE

answered Dec 3, 2018 in Data Analytics by Ali
• 10,450 points
143 views
0 votes
2 answers

"Train" and "Test" sets in Data Science

Normally to perform supervised learning you need ...READ MORE

answered Aug 2, 2018 in Data Analytics by Anmol
• 3,620 points
39 views
0 votes
2 answers

Installing MXNet for R in Windows System

You can install it for python in ...READ MORE

answered Dec 3, 2018 in Data Analytics by Kalgi
• 39,330 points
286 views
+1 vote
3 answers

Problem with installation of Wordcloud in anaconda

Using Anaconda Python 3.6 version For Windows ...READ MORE

answered Aug 7, 2018 in Data Analytics by Priyaj
• 56,520 points
3,162 views
0 votes
1 answer

Which package is used to do data import in R and Python and How do you import SAS data?

We can do data import using multiple ...READ MORE

answered Aug 24, 2018 in Data Analytics by Anmol
• 3,620 points
24 views
0 votes
1 answer

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

Structured query language (SQL) is a programming ...READ MORE

answered Aug 24, 2018 in Data Analytics by Anmol
• 3,620 points
79 views