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.
2. Create two empty lists. number_list will hold all the numbers and result_list will hold the final list of numbers.
3. Run one for loop. Ask the user to enter list item. Read it and store it in number_list list.
4. Ask the user to enter the value for m and n. Read it and store it in variable m and n.
5. 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.
6. Finally, print out all the numbers that are in the final_list.
Sample Output :
We have learnt how to find all numbers in a list divisible by m or n in python. 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