Functions in Python

User defined functions ( UDF)
def my_sum(a, b):
   sum=a+b
   return sum

print("Sum = ",my_sum(5,8))
Output
Sum = 13

Passing list to function

We will pass a list to function and return the sum of all the elements
def my_sum(my_list):
   sum=0
   for i in my_list:
    sum=sum+i
   return sum

my_list=[1,5,7,12]
print("Sum = ",my_sum(my_list))
Output is here
Sum = 25

Returning a list from a function

def my_details(my_marks):
    total=sum(my_marks)
    avg=total/len(my_marks)
    a=[total,avg,len(my_marks)]
    return a
    
my_marks=[4,3,2,11]
marks_final=my_details(my_marks)
print("Details = {}".format(marks_final))
Output is here
Details = [20, 5.0, 4]

Passing string parameter to function

def my_string(str1,str2):
    str3=str1 + str2
    return str3
str_final=my_string('Welcome to',' Python')
print("Here is your final string : ",str_final)
Output is here
Here is your final string :  Welcome to Python

Default Arguments of function

We will not use any parameter so python will use its default value.
def my_details(my_language='Python'):
    print("Welcome to {}".format(my_language))
    
my_details()       # No parameter is passed 
my_details('PHP')  # One string is passed 
Output is here
Welcome to Python
Welcome to PHP
In above code we have called the function without any parameter in first line. In second line we have called the function with a parameter.

If any argument does not have a default value then we must supply that while calling the function. We can have any number of arguments with default value, but once we have default value then all arguments to the right of it must have default value.

This code will generate error. non-default argument follows default argument
def my_details(my_language='Python',my_database):
    print("Welcome to {} with {}".format(my_language,my_database))

my_details('PHP','MySQL')
We can change this code
def my_details(my_database,my_language='Python'):
    print("Welcome to {} with {}".format(my_language,my_database))

my_details('PHP','MySQL')
Output is here
Welcome to MySQL with PHP

Recursive function

Function which calls itself during its execution.
To prevent recursive function to execute infinitely we need to keep conditional statement to come out of recursive.

Factorial of a number is multiplication of all number less than the input number.
n! = n*(n-1)*(n-2)*(n-3) …. .* 1
Let us understand this by using one example.
Factorial of a number
def fact(n):
    
    print("The number is : ",n)
    if n==1:
        return 1
    else:
        result = n*fact(n-1)
        print("Part factorial = ", result)
        return result
fact(4)
Output is here
The number is :  4
The number is :  3
The number is :  2
The number is :  1
Part factorial =  2
Part factorial =  6
Part factorial =  24
Your Rating




Post your comments , suggestion , error , requirements etc here




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer