In my python tutorial series last series, we have learned and practiced the some of basics concept of python. Before continuing the next series I would be like to recommended to check my last tutorials about the basic of python which is posted the link description below that helps you to understand.
1. Introduction of python and get started to learn basic’s of python
2. Variables declaration and their uses in python development
3. Exploring Data Types of python
4. Understanding the conditional Statement and Loop of python
Let’s get started to learn the function of python in this series. In Python, a function is a group of related statements that perform a specific task. Functions help break our program into smaller and modular. As our program grows larger and larger, functions make it more organized and manageable to understand the code structure. The function enables us to debug program easily. Let’s check that how can we struct the function of python:
def function_name(parameters): """do something""" statement(s)
Here def
is the keyword of python to marks as a function header. Now paramaters
is the argument of the python. We need to add :
in the end of the function definition. We can use the return
also but it is optional. As I said in my last tutorial is that python is the most versatile programming language. It is super easy to understand for any student kids. Here in python, function support the multiple returns. For example.
def function_name(parameters): """do something""" statement(s) return a,b
Let’s see the few examples of function python to execute. First, we need to provide the definition of function and after we need to call this function to execute. Always remember that function dentition will be above on function call because python exception is line by line by the interpreter.
# how can we write the function in python def function_name(): print('this is a function')
Now call this function to execute.
# call function_name()
Output: this is a function
Let’s see some other example of adding two numbers in python by using the function. Here we can check the return example too.
def sum_of_two_numbers(first, second): sum = first + second print("a sum is: "+ str(sum)) def sum_of_two_numbers_return(first, second): sum = first + second print("a sum is: "+ str(sum)) return sum # call sum = sum_of_two_numbers(10, 20) print("Return sum: "+ str(sum)) sum = sum_of_two_numbers_return(10, 20) print("Return sum: "+ str(sum))
Output:
a sum is: 30
Return sum: None
a sum is: 30
Return sum: 30
A function can be argument pass is the default and defined also. We can pass the argument any number of them based on our requirement. In the default case, it takes the arguments in order which we defined in the function but in define case, it can by any order with argument name and value. Do not be confused here, let us check by example to make it simple.
# function can be argument pass is default and defined also def sum_of_numbers_custom(first, second, third = 30, fourth = 40): sum = first + second + third + fourth print(sum) # call sum_of_numbers_custom(10, 20) sum_of_numbers_custom(10, 20, 10, 20) sum_of_numbers_custom(10, 20, fourth= 20) # here in third argument, if not passing argument then it takes from defined vules
Output:
100
60
80
Now let’s talk about the arbitrary
argument function. It means we can pass any data types of argument with any number of argument. This is a great feature of function of python. Let’s see for an example.
# arbitrary argument function def get_name(*names): """This function greets all the person in the names tuple.""" # names is a tuple with arguments for name in names: print("Hello",name) get_name("Monica","Luke","Steve","John")
Output:
Hello Monica
Hello Luke
Hello Steve
Hello John
Ok great, Now let’s see the recursive
feature of function of python. As we know from another programming language recursive functions are those function which called itself. Yes in python it is also same, to clearly understand let’s see an example of factorial.
# Recursive function def factorial(number): if(number <=1): return 1 else: return number * factorial(number - 1) print("Factorial value", factorial(3))
Output: Factorial value 6
Now let’s see the lambda
function of python. While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword. It makes really awesome python.
# syntax : # lambda arguments: expression
Let’s see this as an example to clearly understand this lambda function.
f = lambda x: x + 1 print(f(4))
Output: 5
Lambda function, we can use the multiple places like in the data structure, for example, to filter the even numbers from the given list or map the list item convert into a square. To clear this let’s see an example.
# filter with Lambda my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(filter(lambda x: (x%2 == 0) , my_list)) print(new_list) # map with lambda my_list = [1, 5, 4, 6] new_list = list(map(lambda x: x*x , my_list)) print(new_list)
Output:
[4, 6, 8, 12]
[1, 25, 16, 36]
Now we have good and enough understanding of the functions of python. In my next tutorial of python series, we will learn the basic of module or package of python.
If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you.
Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on below section.
You can learn all articles of MobologicPlus from the android app, which is available to download below.
[appbox googleplay mobi.androapp.mobologicplus.c7929]
Happy coding 🙂
I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com