Python Function
- Function is a group of statement that perform specific task.
- Function help reduce line of code and break into small code section.
- basically function use code in more organised and manageable.
In python there are three types of functions
1.User defined function
2.Built in function
3.Lambda Function
User defined function
User defined function made by user as their requirement.
Defining a function
- To define a function we use def keyword with parenthesis.
- We pass parameters/ arguments in parenthesis.
- To close the function we use return(expression).
Syntax of a function
def function_Name( parameters ):
#Statements
return (expression)
#Calling a function
Calling a function
After defining a function we need to call a function to perform function task, In function calling we need to passed arguments in function.
Lets understand it with an example
Example
#Defining a function
def ADD (a,b):
return a+b;
#taking values from the user
a = int(input("Enter a= "))
b = int(input("Enter b= "))
#Calling a function
print("Sum = ",ADD(a,b))
Output
Enter a= 5
Enter b=5
Sum = 10
Example
#defining a function
def Odd_Even( x ):
if (x % 2 == 0):
print ("even")
else:
print ("odd")
#Calling a function
Odd_Even(8)
Odd_Even(9)
Output
even
odd
Parameters:-A variable listed inside a function when function is defined
Arguments:-A variable is passed in a function during calling of function.
Different types of arguments in python function
Required arguments
Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.
Example
def add(a,b):
c=a+b
print(c)
add(7,8)
Output
15
Note:-Here "a" and "b" are required argument without that you cannot perform or call a function , if you try without passing a and b it will give a syntax error.
Note:- In this type of argument variables arguments taken by function are in correct positional order.
Keyword arguments
In this type of argument order of arguments does not matter,when we call function we define arguments with their respected values. It works like Key=value.
Example
def Name_Age (Name,Age):
print(Name)
print(Age)
return;
#Function calling
Name_Age(Name='XYZ',Age='21')
Output
XYZ
21
Default arguments
In Default argument it works ,when a argument value is not provided in function calling than it take a default value that is already defined in define function.
Note:-If a argument value is provided in function calling than default value is avoided.
Example
def Name_Age (Name,Age,salary=20000):
print(Name)
print(Age)
print(salary)
return;
#Function calling
Name_Age(Name='XYZ',Age='21')
def Name_Age (Name,Age,salary=20000):
print(Name)
print(Age)
print(salary)
return;
#Function calling
Name_Age(Name='XYZ',Age='21',salary=50000)
Output
XYZ 21
20000
XYZ 21
50000
Variable Length arguments
Sometimes we need more variables than already listed in function defining , in such cases we use variable length arguments.
An asterisk (*) is placed before the variable name that holds the values of all variable arguments.
Example
def Sum(*b):
c=0
print(b)
for i in b:
c=c+i
print(c)
Sum(1,5,6,8)
Output
(1, 5, 6, 8)
20
Built-In Functions
- A function that is built into an application and can be accessed by users is called built-in function. Python has a number of built-in functions.
- For example-print() is a built-in function in python
Some built-In python functions and their description
Function Name |
Description |
print() |
gives object on the screen or other standard output devices. |
list() |
create a list |
set() |
create a set |
len() |
shows the length of list, string etc |
tuple() |
create a tuple |
type() |
shows type of a object e,g-string, integer etc |
max() |
shows maximum value from the given values |
Example
#Sorting in ascending order
L=[462,45,546,6485,65,7,65,54,524,874,54,5]
Sort_L=sorted(L)
print(Sort_L)
#Descending order
Sort_L_Reverse =sorted(L, reverse=True)
print(Sort_L_Reverse)
Output
[5, 7, 45, 54, 54, 65, 65, 462, 524, 546, 874, 6485]
[6485, 874, 546, 524, 462, 65, 65, 54, 54, 45, 7, 5]
Note:- If we want to sort a list without using built-in sorted function,for that we have to write a long code.Built-in functions make things easy to us.
Lambda Function
- Lambda function is a anonymous function means that a function is without a name.
- To create a small anonymous function lambda keyword is used.
Syntax of a lambda function
lambda arguments:expression
Example
x = lambda a:a*n
n=int(input("A number="))
print("sum = ",x(20))
Output
A number=4
sum = 80
Machine Learning Tutorial
Free Online Tutorials
Artificial Intelligence(AI) Training in Jaipur
Machine Learning(ML) Training in Jaipur