N. Achyut Python,Uncategorized Functions in Python

Functions in Python

Function is the set or block of statements that takes input print values or do some functionalities to perform a task. In a programming language we create a function to put some commonly or repeatedly done task together so that instead of writing the same code again and again for different inputs. We can reduce the block of code by using the function.

In python we define function by using the def keyword, the syntax is given below:

def function_name():

Function Examples

Some examples to understand function in python

def user():
    print("users")


user()

Example to add and subtract in single function

def add_sub(x, y):
    sum = x + y
    sub = x - y
    return[sum,sub] #returning value in list 


result = add_sub(20,10)
print(f'the addition of two number is {result[0]} n'
      f'the subtraction of two number is {result[1]}')

Simple interest problem using python

def take_value():
    p = int(input('enter the value of principle'))
    t = int(input('enter the value of Time'))
    r = int(input('enter the value of Rate'))

    return [p, t, r]


value = take_value()


def calculate():
    i = ((value[0] * value[1] * value[2]) / 100)
    return i


def display():
    print(calculate())


display()

Lambda Function

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

Example of lambda function

users = lambda x, y: x + y
print(users(20, 20))

Related Post