Working with Python – Function & Lambda Function

Introduction

In this blog post we are trying to discuss about Python Function and Lambda Function. Here we are not going to describe what function is and how to use it, as every developer knows the uses of function.

Hope it will be interesting.

 

Syntax of Function

 


 

In Python Function, there is no Begin and End like syntax.

Here we have to identify that using the Line alignment only. So, you must be careful about it when writing a function in Python.

 

Example of a Function

 

def myfunc(name):

    print(name)

 

myfunc('Joydeep Das')

 

Joydeep Das

 

Parameters and Default Values

We can specify the default value of Parameters in the Function.

 

def myfunc(firstname,lastname="Das"):

    print(firstname+' '+lastname)

 

myfunc('Deepasree')

 

Deepasree Das

 

Here the lastname arguments have default value Das.

 

Need another example to understand it properly

def myfunc(firstname,lastname="Das",class=12,stream="Science"):

    print(firstname+' '+lastname)

    print(class)

    print(straeam)

 

myfunc('Deepasree')

 

We know, you already understand the output of this. But can you understand that we can assign Parameter value only in right side not in left.

 

For example:

def myfunc(name="Deepasree Das",class=12,stream):

 

We are not allowed to write the above type of function argument default value assignment.

 

Arbitrary Arguments, *args

If we do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.

Example:

def myfunc(*arg):

    print(arg[1])

 

myfunc('Joydeep','Mayuree','Saurrya')

 

Mayuree

 

Take another example with multiple parameters.

 

def my_function(**kid):

  print("His last name is " + kid["lname"])

 

my_function(fname = "Tobias", lname = "Refsnes")

 

His last name is Refsnes

 

 

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.

We can understand the usages of Lambda Function, when we are trying to use Map, Filter and Reduce.


Syntax:

lambda arguments expression

 

Example:

s=lambda x:x*x

print(s(4))

 à16

 

In our next post we are going to discuss Map, Filter and Reduce.

 

Hope you like it.


Comments

  1. I forgot to pass List as a parameters.
    def myfunc(l=[]): # empty list
    print(l)

    lst=[1, 2,3]
    myfunc(lst)

    --> Output:
    [1, 2,3]

    ReplyDelete
  2. Tuples is not mutable like List. That means you can not chnge the element of Tuple.
    t=(1,2, 3)
    print(t)
    Output:
    (1, 2,3)

    U can't do operation like
    t[0] = 5
    # Gives error

    ReplyDelete
  3. Dictaniary can take key and valu payer. You can change the value not key. We are trying to provide seperate post on Dictionary.
    d = {1:"Joy", 2:"Toy", 3:"Boy"}

    This is for your reference.

    ReplyDelete
  4. You can concert tuples to list, list to tuple, dictionary etc by using convertion function with some logic.
    We have nested structure tooo. .
    l=[(1,2, 3) , (4,5, 6) ]
    Here tuples is nested over list.

    ReplyDelete

Post a Comment

Popular Posts

Working with Python – Map, Filter and Reduce Function

Working with Python – Module

Copying Multiple File from Blob Storage to Single SQL Table – Part-2