write a lambda function to convert measurements from meters to feet

Hi in this article you will learn a Python program that uses the Lambda function to calculate the feet given that length in meters.

So given floating point variable length in meters you have to convert it into feet.

Let's try to solve this using python.

 

write a lambda function to convert measurements from meters to feet

 

 

 write a lambda function to convert measurements from meters to feet

Take a variable name does convert and using the Lambda convert the meter to feet by dividing the meter by 3.280839895, as shown below

convert = lambda meter: meter / 3.280839895




Once we define the Lambda function, take a  length variable and ask the user to enter the number of measurements he is going to enter,  take an empty list of floating point  values named as meters and initialize an empty list.  We will be filling this list by asking a user to enter the meters.

Now using the for loop, iterate from 0 to the list length and ask the user to enter the meters and convert it into a floating point value. Once we get the floating point value appended to the list.

length = int(input("Enter a Total Number of Measurements : "))
meters = []
for _ in range(length):
   temp = float(input("Enter a Length in Meters : "))
   meters.append(temp)





Use the map function to convert all the measurements  in meters to  feet,  and then again convert back to list and store it in feet variable.  The feet variable is a floating point list containing the feet.

Use the for loop to print the converted measurements from meters to feet using the print function call.

feet = list(map(convert, meters))
print("The measurements converted from meters to feet are ...")
for item in feet:
   print(item)




Complete Python program
==============================

convert = lambda meter: meter / 3.280839895

length = int(input("Enter a Total Number of Measurements : "))
meters = []
for _ in range(length):
   temp = float(input("Enter a Length in Meters : "))
   meters.append(temp)

feet = list(map(convert, meters))
print("The measurements converted from meters to feet are ...")
for item in feet:
   print(item)





Output
===============

Enter a Total Number of Measurements : 4
Enter a Length in Meters : 1
Enter a Length in Meters : 2
Enter a Length in Meters : 3
Enter a Length in Meters : 4
The measurements converted from meters to feet are ...
0.3048000000012192
0.6096000000024384
0.9144000000036575
1.2192000000048768





Extra learning
========================

Suppose you have only one measurement then, you can just ask the user to enter meters and then use the Lambda function to convert it into feet as one below.

Python Program
======================

convert = lambda meter: meter / 3.280839895

meter = float(input("Enter a Length in Meters : "))
feet = convert(meter)
print(f"The measurements {meter} meters is : {feet} feet.")




Output
=============

Enter a Length in Meters : 24.88
The measurements 24.88 meters is : 7.583424000030333 feet.




Conclusion
=================

There are two  programs in this article: the first is for having multiple measurements and the next Python program is to handle only a single measurement.

In case of multiple measurements to convert from meters to feet we use  Python list and map function.

In the case of a single value entered by the user, they generally only use two variables and then print them on standard output.

Comment it down below if you have any queries regarding the above python program.


Post a Comment

0 Comments