Defining and Importing Functions

Subscribe to my newsletter and never miss my upcoming articles

Defining a Function in Python

A function in Python is a set of related statements that are designed to perform a specific task. To develop a function our main objective is to call a function instead of writing the same code again and again for the same type of input this helps the code to be more concise and cleaner.

There are two types of functions in Python Programming Language :

  1. User Defined Functions
  2. Built-in Functions

Built-in Functions

The Python built-in functions can be defined as the function which has been already written for us to use whenever we want to use it in our Program all you have to do is import it and start using it the way you want your program to be executed. Python has many of the Built-in Functions that can be imported and can be used, some of them are listed below:-

len() function

This function is used to find the length of the String either entered by the user or provided by the Programmer.

# Syntax for len() function 

lengthOfStr = len("Hello World")

# output will be 11
list() function

This function is used to convert an Integer or tuple into the list ( explicitly ).

# Syntax for list() function 

convertedTuple = list('aeiou')

# output will be ['a', 'e', 'i', 'o', 'u']
min() function

This function is used to get the minimum from the numbers passed in this function as the return.

# Syntax for min() function 

a = (11, 15, 13, 19)
x = min(a)


# output will be 11

User Defined Functions

The functions which a user defines for the better optimization of the code and increasing the capacity and making the code cleaner is termed as User Defined Functions.

There are two steps to write a Function in Python listed below:-

  1. def keyword is used to declare user-defined functions
  2. An indented block of code is used to follow through-out the function that will tell the interpreter that it is a part of user Defined Functions.

# Syntax for declaring a User Defined function

def functionName():

Example of writing a user Defined function with its calling in Python Language

# Syntax for declaring a User Defined function

def Function2():
    print('Inside Function named Function2 ')

def Function1():
    print('Inside Function named Function1 ')


# Output will be in the following manner

Inside Function named Function1 
Inside Function named Function2

Parameterized Function

We can also have a Parameterized Function so that we can access some values and act accordingly.


def function_name(argument1, argument2, ...):

Default Parameters

This kind of parameter is used to give a default value to the variable so in case we don't pass any kind of value to the variables so that it will take up that value and the execution won't be terminated.

# Program for Default Parameter in Python
def function1(a, b = 50): 
    print("a: ", a) 
    print("b: ", b) 


# Output will be 
a:  10
b:  50

Importing a Function in Python

Importing a Python Function in Python is as easy as we do in C or C++ just write the thing we want to import from the specific fileName

# Syntax for importing a Function in Python Program

from fileName import functionName

Example for importing the Program and using it as our need

for Importing a Function we need to Specify the function name with the specified filename

Let's suppose we have a file Named with the content

def printHelloWorld():
    print('Hello World')

def returnTheSum(a, b):
    c = a+b
    return c

and Now we want to include these function in our another Program with the fileName with content

from programOne import printHelloWorld, returnTheSum

sum = returnTheSum(5, 5)

# Output will be 10

You can also import some module with another name like

import programOne as po

then for accessing the Modules you will have to write the code like this



In this post, we learned about how to create our Own Functions and Import them into other Programs and execute the Program to get the desired output.

Python Function

We learned about User Defined Functions and also about the BuiltIn Functions ( though I have covered only 3 of the Built-in Function there are many of the Built-in Functions you can refer to w3Schools for more. )

Follow me on Instagram for more Updated Content about Python and Flutter ( Dart Included ).

Currently, I am Developing Full Stack Blog Web App with React as Front End and Django as Backend. To get more Updated you can Follow me over Instagram

No Comments Yet