Write A Python Program To Triple All Numbers Of A Given List Of Integers. Use Python Map

Given a list of integers as input to the Python program for your task is to use a map() function to triple all the numbers  present in the list.

We need to use map() function to get the task done.

 

Write A Python Program To Triple All Numbers Of A Given List Of Integers. Use Python Map

 



Write A Python Program To Triple All Numbers Of A Given List Of Integers. Use Python Map

Let's define a function named triple() and pass integer variable x as an input argument to the Python function.  In the function body just use a return statement and return the variable multiplied 3

def triple(x):
   return x * 3



In the main program take a variable n  to store the list length and as a user to enter the list line using input function call and then convert it into an integer using that type conversion

Take an empty list “numbers”  and I find an empty list using the following iterate up to the list length and ask the user to enter  all the numbers present in the list and append to an empty list as shown below.

n = int(input("Enter a List Size : "))
numbers = []
for _ in range(n):
   temp = int(input("Enter a List Item (Integer) : "))
   numbers.append(temp)



Once we get the list of numbers as a into the Python program so let's call the function name the map and pass the function name (triple) as first argument and  second argument as the list of integers.

Convert result of map into list and store it into a new_numbers a variable containing thrice of original list.

new_numbers = list(map(triple, numbers))



Using the print function call print the list containing triple of the original list.

print("Calculating thrice of given list is ...")
print(new_numbers)





Code
==============

def triple(x):
   return x * 3


n = int(input("Enter a List Size : "))
numbers = []
for _ in range(n):
   temp = int(input("Enter a List Item (Integer) : "))
   numbers.append(temp)

new_numbers = list(map(triple, numbers))

print("Calculating thrice of given list is ...")
print(new_numbers)





Output
============
Enter a List Size : 5
Enter a List Item (Integer) : 1
Enter a List Item (Integer) : 2
Enter a List Item (Integer) : 3
Enter a List Item (Integer) : 4
Enter a List Item (Integer) : 5
Calculating thrice of a given list is ...
[3, 6, 9, 12, 15]




Output
============
Enter a List Size : 7
Enter a List Item (Integer) : 11
Enter a List Item (Integer) : 22
Enter a List Item (Integer) : 33
Enter a List Item (Integer) : 44
Enter a List Item (Integer) : 55
Enter a List Item (Integer) : 66
Enter a List Item (Integer) : 77
Calculating thrice of a given list is ...
[33, 66, 99, 132, 165, 198, 231]




Conclusion
===============
Execute the above program by passing the list of integers as input to the Python program and note down the results. Comment down below if you have any suggestions to improve the program





Post a Comment

0 Comments