Q:

Python program to illustrate the working of lambda functions on array

belongs to collection: Python Array Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to apply lambda functions on array... >>
<< Python program to filter matrix based on a conditi...