Python program to find out numbers in a list divisible by two numbers
In this python programming tutorial, we will learn how to find all numbers that are divisible by two specific numbers. For example, let’s take a look at the list [1,2,3,4,5]. In this list, numbers that are divisible by 2 and 1 are [2,4]. Our program will do the same thing. The user will enter the values of the list and also the two numbers (let’s say m and n).
This is a beginner friendly program and it will give you the basic understanding of loops, conditions, list and how to take user input in python. Let’s take a look at the program first :
Python program :
You can download the code from here.
The commented numbers in the above program denote the step number below :
1. Ask the user how many numbers the list will hold. Read the input and store it in list_size variable. We are using ‘input()’ method to take the list size as an input from the user. We are wrapping this value with ‘int()’ to read the value as an integer from the user.
2. Create two empty lists. number_list will hold all the numbers and result_list will hold the final list of numbers. result_list will hold the final list items and number_list will hold all the numbers in the beginning.
3. Run one for loop. Ask the user to enter the list items one by one. Read all values and append to the number_list. We are using one for loop but you can also use one while loop if you want.
4. Ask the user to enter the value for m and n. Read both values and store them in variable m and n. Our goal is to find out all values of number_list that are divisible by ‘m’ and ’n’.
5. For filtering out the required values from the list, we need to iterate through them all. Run one more for loop. Check for each number if it is divisible by m and n or not. If yes, add that value to the final_list. So, final_list will store all the result numbers after the for loop will exit.
For checking if a number is divisible by m and n or not, we are using ‘and’ operation. This operation will return True if both conditions before and after ‘and’ is True. Here, it will be True if the number is divisible by both ‘m’ and ’n’.
6. Finally, print out all the numbers that are in the final_list.
Sample Output :
We have learned how to find all numbers in a list divisible by m or n in python. This program taught you how to read user inputs, print outputs to the user, how to create an empty list and fill that list using user provided values, and how to filter out list items with a specific condition. Try to run the program by downloading the code using the list provided above.Go through the above example and drop a comment below if you have any queries. Happy Coding 🙂
You might also like :
- zip function in python and how to use it
- Python index method to get the index of an item in a list
- Python time.sleep method explanation with Example
- Python print current date,time,hour,minute,increment each
- Python program to take user input and check validity of a password
- Check palindrome Using one if-else