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 »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.