what is the usage of python lambda functions

Lambda is a keyword used to create an functions without a name or in simple lambda is used to create an anonymous functions,lambda function has n number of argument with a single expression which is processed and returned the result.we know normally a function is defined in python using def statement followed by function name and argument after evaluating it should return the result to get the result value when function is called but lambda has no name and return statement

Syntax Lambda functon

lambda arguments : expression

Example

Tenpercentage= lambda x :x * 10/100  

#"x" is an argument and "x * 10/100" is an expression which got evaluated and returned.   

print(Tenpercentage(200))

print(Tenpercentage(450))

Output

python lambda functions

See above code in Normal functions syntax

def Tenpercentage(n):
  return n * 10/100

print(Tenpercentage(450))

common usage of python lambda functions

Lambda function perform well if we use lambda functions inside another function see example

See example


def currency_convert(currencyvalue):  
    return lambda x:x*currencyvalue;

Israelicurrency=currency_convert(3.48);  #find dollar to Israeli New Shekel

print(Israelicurrency(100));

print(Israelicurrency(900));


indiancurrency=currency_convert(71.57);  #Find dollar to INR

print(indiancurrency(100));

print(indiancurrency(900));

python lambda functions inside anothor function

indentationerror unindent does not match any outer indentation level visual studio code

One of the main issue we are facing with python code run is indentationerror actully that is is the confusion between tab and space in vscode. In this article we are going to discuss about how to fix indentationerror unindent does not match any outer indentation level visual studio code


Press  CTRL +SHIFT + P 

Then Convert Indentation to Tabs

indentationerror unindent does not match any outer indentation level visual studio code

python for loop continue to next iteration

Being a programmer we may face a situation we have to continue to next iteration in python for loop here in this section we are going to explore the syntax of python for loop continue statement.

Syntac

for LOOP:

   # add something.

   if condition true:
      continue    # continue here
    
    print("Loop value without  condition true")
    

suppose we have a have for loop like below to print all the number between 90 and 100


for number in range(90,100):
    
   print('Number is ' + str(number))

OutPut

Number is 91
Number is 92
Number is 93
Number is 94
Number is 95
Number is 96
Number is 97
Number is 98
Number is 99

So next our requirement we have to print only even number between 90 and 100

for number in range(90,100):
   value = number %2

   if value != 0:
      continue    # continue here

   print('Even number is ' + str(number))

OutPut

Even number is 90
Even number is 92
Even number is 94
Even number is 96
Even number is 98

python for loop continue to next iteration

Control Statements in Python

We know that loops are used to execute a set of statements repeatedly sometimes we need to control execution of the program based on the values and the conditions, so to accomplish this task Control Statements are using in python ,python support three types of Control Statements Continue Statement,Break Statement and the Pass Statement. Lets see in details one by one

Continue Control Statement in Python

Sometimes we need to exclude particular iteration inside the loop in that case “Continue” Control Statements is used to accomplish control to the beginning of the loop.if the condition is true it will return to the beginning of the loop.

Example


# Continue Control Statement in Python

#for loop to print a number between 1 to 4 without 2

l = [1, 2, 3, 4] 
for i in l: 
    if i==2:
       continue
    print(i) 



Out put
1
3
4

Break Control Statement in Python

Sometimes we need to stop the iteration based on particular logic or condition Break Statement is used to accomplish this task .Break statement break stop the execution of the loops

Example

# Break Control Statement in Python


l = range(1, 100)
for i in l: 
    if i==10:
       break   #stop the execution here if i equal to 10
    print(i) 

Output

1
2
3
4
5
6
7
8
9

Pass Control Statement in Python

If we want to execute nothing pass statement can be used. Sometimes we need to implement functionality code after beta delivery of the project we use “pass” to keep a loop without anything


l = [1, 2, 3, 4] 
for i in l :  
    pass