Defining and Importing Functions
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 :
- User Defined 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:-
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") print(lengthOfStr) # output will be 11
This function is used to convert an Integer or tuple into the list ( explicitly ).
# Syntax for list() function convertedTuple = list('aeiou') print(convertedTuple) # output will be ['a', 'e', 'i', 'o', 'u']
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) print(x) # 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:-
- def keyword is used to declare user-defined functions
- 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(): statements statements statements statements
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 ') Function1() Function2() # Output will be in the following manner Inside Function named Function1 Inside Function named Function2
We can also have a Parameterized Function so that we can access some values and act accordingly.
Syntax: def function_name(argument1, argument2, ...): statements . .
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) function1(2) # 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 programOne.py 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 programTwo.py with content from programOne import printHelloWorld, returnTheSum printHelloWorld() sum = returnTheSum(5, 5) print(sum) # 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.
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