In this article, we will provide Python source code that can find the numbers divisible by another number, specifically, from a list of numbers.
Prerequisite Topics to Create the Program
- Python Input/Output.
- Python Lambda functions.
- Filter function in Python.
- Python operators.
- Python split operator.
Steps to Create the Program
- First, we ask the user to enter the values in a list separated by space.
- Next, we ask the user to enter a number n, which is supposed to be another number that divides the numbers present in the list.
- Then using the filter and lambda functions, we will find out all the numbers present in the list that are divisible by another number n.
- At last, we will display all the numbers present in the list that are divisible by the number n.
- filter(function, iterable): filter() is an inbuilt function in Python. It accepts 2 values as arguments, function, and iterable (list, tuple, dictionary, et cetera). It passes the iterable values one by one to the function and returns a filter object that contains all the values of the list that satisfy the condition of the function.
- lambda arguments: return expression: Lambda functions are also known as Anonymous functions and they are used to write user-defined functions.
Python Program to Find Numbers Divisible by Another Number
num_list = list (map(int, input("Enter the numbers separated by spaces: ").split())) n= int(input("Enter the divisible number: ")) result = list(filter(lambda x:x%n==0 , num_list)) print("The numbers divisible by",n, "are",result)
Enter the numbers separated by spaces: 12 23 32 34 36 46 55 75 78 71 Enter the divisible number: 3 The numbers divisible by 3 are [12, 36, 75, 78]
Enter the numbers separated by spaces: 12 23 32 34 36 46 55 75 78 71 Enter the divisible number: 4 The numbers divisible by 4 are [12, 32, 36]
By now, you should know how to write a Python program to find all the numbers from a list that are divisible by a number n. To thoroughly implement the Python code, one needs to have a good understanding of anonymous functions and the filter function.
People are also reading:
- Python Examples
- Python RegEx
- C Program to Design Love Calculator
- Python Program to Check Armstrong Number
- C Program to Extract a Portion of String
- Python Program to Transpose a Matrix
- C++ and Python Code to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix
- Python Program to Merge Mails
- WAP in C++ & Python for the Union of Two Arrays
- Perfect Number in C