Python Functions

In this article, you will learn about the python functions from basic to advance. A function is a name sequence of statement that performs the computation. In python there are two types of function are available. One is the in-built function, and another is a user-defined function. Let’s discuss different terminology use in python function.

What Is python Functions?

A function is a sequence of states that perform a computation. When you define a function you specify name and sequence of a statement. The function is not executed until it’s called. The function calls simply by its name. In python, the user defines function are either void or fruitful. A void function is those whose return nothing. While those function that returns something calls a fruitful function. We are discussing it in detail below.

Python Built-in Functions

Those function which already provides by a programming language is called built-in function. Python provides several built-in functions for solving a common problem. The built-in function is using by a user without a need to define function definition. Following are some example of a built-in function.

  1. Print() The mostly use a function in python is print function. Which is using for display the data.
  2. abs() This function return the absolute value of number.
  3. bin() the bin function that returns the binary version of the number.
  4. input() This function return the value from the user. Type of return value is always in string format.
  5. float() this function return floating-point of number.
  6. hex() this function is used to convert number to hexadecimal number.
  7. len() this function returns the length of an object.
  8. max() this function return maximum value in iteration.
  9. min() this function return minimum value in iteration.
  10. sum() this function returns the total sum of iteration
  11. type() This function return type of an object.

Many other Python functions are available this is the most useful sample of them. Following code shows how to use a built-in function.

print("Hello World")
input('Enter your name: ')
print(len('Hello World'))

python User Define Functions

So far we are using only the built-in function. But it’s also possible to define our own function. In python, a function is creating by definition of a function by specifying name and sequence of statement that execute while a function is a call. Once a function is defined then it’s using anywhere in the program again and again. We can create a function by using def keyword followed by the optional parameter in parentheses. Body of function is always intended. Here is a simple example of user define function.

Python Functions

The function is call by it’s name here is code to illustrate hoe to call function.

def func():

Argument, Parameter and Return Value

the term argument has come when calling the function. The argument is the value that is a pass to the function as an input. The argument is put into parentheses after naming the function. A parameter is a variable which is used while defining the function. The argument is assigning to the parameter. A function has may or may not be a parameter. The access of parameter is only within the function. Generally, A function takes argument through user and do some computation and return a result. The return value is using by the function call. The return keyword is using for this. Following diagram show argument, parameter and returns value.

Argument, Parameter and Return Value
def display(par):
    return par
display('Hello world')

Fruitful and Void Function

User define function is categories into two type. The fruitful function is which return something after computation. The return keyword is used to return something from function. Another function is a void function. A void function is those which is not return anything after computation. Here is an example of a fruitful and void function.

def display(par):
    return par
display('Fruitful Function')
def voidfunc(par):
voidfunc('Fruitful Function')

*Args and **kwargs as Argument

In case of simple user define a function. If the user passes an argument that is not including in the parameter then an error occurs. This is solving by using *args and **kwargs. The *arg takes input as tuple and computation are perform by unpacking. While **kwargs take input as in dictionary format. Similarly, it’s also using by unpacking dictionary. Following code shows the example of *args and **kwargs.

def total(*arg):
for i in arg:
return total_sum


def func(**kwargs):
    for i, j in kwargs.items():

Lambda Function

the function defines in one single line is called the lambda function. Lambda keyword is use to define this function. The lambda keyword follows by the number of a parameter after that clone is place. The operation of function has come after clone. The syntax of the lambda function is. Lambda parameter1,2,3,…: operation lambda function are enumerating with other function Like map, filter, reduce etc.

adder = lambda x, y: x + y
print (adder (1, 2))

lambda in filter

The filter function is used to select a single element by a particular condition. By one single line code help to filter data by using lambda function. Following code shows lambda function in the filter.

sequences = [10,2,8,7,5,4,3,11,0, 1]
filtered_result = filter (lambda x: x > 4, sequences) 
output: [10, 8, 7, 5, 11]

Lambda in map

The map function is used to apply particular operation to every element. By one single line code help to map data by using lambda function. Following code shows how to implement lambda in the map function.

sequences = [10,2,8,7,5,4,3,11,0, 1]
filtered_result = map (lambda x: x*x, sequences) 
output: [100, 4, 64, 49, 25, 16, 9, 121, 0, 1]

Lambda in Reduce

The reduce function is same as map function. It’s also used to apply particular operation to every element. Following code shows how lambda function use in Reduce. The parameter and operation are separate by a clone in every lambda function.

from functools import reduce
sequences = [1,2,3,4,5]
product = reduce (lambda x, y: x*y, sequences)
output: 120


By using all the concept of python function and loop doing ATM project. Overview of this project is first to define one function that takes input from the user. This function is used for the terminate program. While loop is started for 3 chance. And all the concept are implementing inside while loop. The loop is run when the user pin is correct. The program for this project is given below.

import sys
import os

def fun():
    print('Go back: Enter 1')
    print('Exit: Enter 2')
    opt1 = int(input())
    if opt1==2:
        print('\nThank You!\nPlease take your card')
    if opt1!=1 and opt1!=2:
        print('Wrong choice!Please try again.')
print('Welcome to ATM of Nepal Rastra Bank!')
while chance>0:
    pin = int(input('Enter your pin number: '))
    if pin==(4321):
        while opt1==1:
                print('Enter 1 for balance enquiry: ')
                print('Enter 2 for cash withdrawl: ')
                print('Enter 3 for balance deposit: ')
                print('Enter 4 to exit!: ')
                option = int(input())
                if option==1:
                    print('Your balance is Rs',balance)
                elif option==2:
                    cash = int(input('How much money do you want to withdrawl? '))
                    if cash<balance:
                        balance = balance-cash
                        print('Thanks for your transaction.\nYour remaining balance is',balance)
                        print('No sufficiet balance!!')
                elif option==3:
                    deposit = float(input('Enter the amount that you want to deposit: '))
                    print('Thank You!\nNow your total balance is',balance)
                elif option==4:
                    print('Thank You!\nNepal Rastra Bank')
                    print('Enter right choice!')
                    print('Thank You!\nNepal Rastra Bank')
        print('Wrong Pin number.')
    if chance==0:
        print('No more try!')
        print('Thank You!\nNepal Rastra Bank')


In this article we are learn about python function. In summery following topics are learn here.

  • What Is python Functions?
  • Python built-in functions
  • python User Define Functions
  • Argument, Parameter and Return Value
  • Fruitful and Void Function
  • *Args and **kwargs as Argument
  • Lambda Function
    • Filter
    • Map
    • Reduce
  • Project


Ok, this is the end of the articles I hope you you can get a good lesson what I deliver in this article. I ask forgiveness for any word and behave which are not to be. If you want to do a project on data science and machine learning here is a link for a free is a link for python basic course. Thank you for your kind and attention guys. Stay tuned for the next article. If you have any question regarding this article please feel free to comment below.

Leave a Reply