Python program to illustrate the working of lambda functions on array

Here, we will see a Python program to illustrate the working of lambda functions on an array using for in loop.
Submitted by Shivang Yadav, on March 16, 2021

Lambda functions in Python are anonymous functions that are single expression functions to work on variables.

For in loop is used to iterate over an array and return the value at the current index.

Here, we will apply the given lambda function to return the grade of the students of a class. The marks of all students are stored in an array. And we will use the for in loop to access each value of the array.

Program to illustrate the working of our solution

studentMarks = [56,77,88,45,2,27,44]

studentGrades = lambda:['First' if (per>=60 and per<=100) else
          'Second' if(per>=45 and per<=59) else 'Fail'
          for per in studentMarks]
          
print(studentGrades())

Output:

['Second', 'First', 'First', 'Second', 'Fail', 'Fail', 'Fail']

Explanation:

In the above code, we are creating an array named studentMarks that will store the marks of students and then using the lambda function, we have calculated the grades of students based on their marks and store it in an array studentGrades. At last, we have printed the array.

Python Array Programs » Python Lambda Function Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.