Write a program to Find Numbers Divisible by Another Number in Python
Code:
1 2 3 4 5 6 7 8 9 10 11 12 | # Python Program to find numbers divisible by fifteen from a list using anonymous function # Take a list of numbers my_list = [300, 45, 65, 12, 77, 145, 5,] # use anonymous function to filter result = list(filter(lambda x: (x % 15 == 0), my_list)) # display the result print("Numbers divisible by 13 are",result) |
This Python program uses an anonymous function (also known as a lambda function) to find numbers in a given list that are divisible by 15.
The program starts by defining a list of numbers, my_list = [300, 45, 65, 12, 77, 145, 5]
. Then, an anonymous function is defined using the lambda
keyword, which takes a single argument, x, and returns True
if x is divisible by 15 (x % 15 == 0), and False
otherwise.
This function is passed as the first argument to the built-in filter()
function, which applies the function to each element in my_list
. The filter()
function returns an iterator containing only the elements of my_list
that returned True
when passed to the anonymous function. This iterator is then passed to the list()
function to convert it to a list, and the result is assigned to the variable result
.
Finally, the program prints out the resulting list of numbers divisible by 15, which in this case would be [300,45]
So in summary, the program is using anonymous function with filter function to filter the numbers which are divisible by 15 from a list of numbers and returns the output in the form of a list.