PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
  • Quizzes
  • Code Editor
Home » Python » Programs and Examples » Python Find the Numbers Divisible by Another Number

Python Find the Numbers Divisible by Another Number

Updated on: March 31, 2025 | Leave a Comment

When working with numbers in Python, you often need to filter divisible numbers by another number. In this tutorial, we’ll cover the various methods to find numbers divisible by another number.

Table of contents

  • 1. How to find the Numbers Divisible by Another Number in Python
  • 2. Using the filter() Function
  • 3. Using Recursion
  • 4. Using List Comprehension
  • Summary

1. How to find the Numbers Divisible by Another Number in Python

This is a simple approach to finding a divisible number in Python by checking each number in the list and seeing if it is divisible by the given divisor.

We are iterating over the list using a for loop and checking divisibility using the modulo operator.
The modulo operator (%) in Python returns the remainder after dividing one number by another. If the remainder is 0, then it’s divisible. Else, it is not.

Steps to find numbers divisible by another number:

  1. Get Input

    A list (or any iterable) of numbers (let’s call it numbers).
    The divisor (let’s call it divisor).

  2. Initialize an empty list

    Create an empty list called divisible_numbers to store the numbers that are divisible by the divisor.

  3. Iterate through the numbers list

    Iterating over the number list using a for loop

  4. Check for divisibility

    In each iteration use modulo operator (%) to check If the current number is divisible by divisor. (i.e., if number % divisor == 0), then Append it to the divisible_numbers list.

  5. Return the result

    Return the divisible_numbers list.

Code Example

numbers = [10, 15, 22, 33, 40, 50, 60]
divisor = 10
divisibles = []

for num in numbers:
    if num % divisor == 0:  # Check divisibility
        divisibles.append(num);     # Add divisible in list

print(divisibles)Code language: Python (python)

Output:

[10, 40, 50, 60]Code language: Python (python)

2. Using the filter() Function

Here, filter() function is used to find numbers divisible by a divisor using Python. The filter() function filters elements from an iterable based on a given condition (function). It returns only the elements that satisfy the condition.

Code Example

numbers = [10, 15, 22, 33, 40, 50, 60]
divisor = 10
divisible_numbers = filter(lambda x: x % divisor == 0, numbers)
print(f'Numbers divisible by {divisor}:', *divisible_numbers)

# Output: Numbers divisible by 10: 10 40 50 60Code language: Python (python)

Explanation

  • The filter() uses lambda to iterate over each item in the list of numbers and apply the condition x % divisor == 0 to each number.
  • If the number passes the condition, it collects it into the iterable and returns it with all the divisible numbers. (An iterable in Python is any object that can be looped over/iterated)
  • The result is an iterator containing only the divisible numbers, which are then printed using * by unpacking the elements in the iterator.

3. Using Recursion

Recursion is a programming technique where a function calls itself to solve smaller instances of a problem until a base condition is met.

Here, we recursively check the number in the list to see if it is divisible by the given number in Python.

For Example: numbers = [10, 15, 40] and divisor = 10, the recursion will work as follows:

  • First call: numbers = [10, 15, 40] and divisor = 10 -> Check 10 % 10 == 0, then it’s divisible.
  • Second call: numbers = [15, 40] and divisor = 10 -> Check 15 % 10 != 0, then it’s not divisible.
  • Third call: numbers = [40] and divisor = 10 -> Check 40 % 10 == 0, then it’s divisible.
  • Fourth call: numbers = [] -> Base case reached → Recursion stops.

Code Example

def divisible_numbers(numbers, divisor):
    if len(numbers) == 0:  # Base case: stop if list is empty
        return
    if numbers[0] % divisor == 0:
        print(f'Number {numbers[0]} is divisible by {divisor}')
    else:
        print(f'Number {numbers[0]} is NOT divisible by {divisor}')
    divisible_numbers(numbers[1:], divisor)  # Recursive call with the rest of the list


numbers = [10, 15, 22, 33, 40, 50, 60]
divisor = 10
divisible_numbers(numbers, divisor)Code language: Python (python)

Output:

Number 10 is divisible by 10
Number 15 is NOT divisible by 10
Number 22 is NOT divisible by 10
Number 33 is NOT divisible by 10
Number 40 is divisible by 10
Number 50 is divisible by 10
Number 60 is divisible by 10

Explanation

  • In each recursion, we check the condition on the first element in the list, and then the function calls itself with the list’s subset containing numbers from position 1 to the end of the list.
  • String slicing (numbers[1:]) creates a sublist starting from index 1 to the end of the numbers list, effectively excluding the first element.
  • Each time, it checks and prints the first element if it’s divisible by the divisor. It continues till the list gets empty.

4. Using List Comprehension

List comprehension is a concise way to create Python lists by applying an expression to each item in an iterable, with optional filtering using a condition.

List comprehension is used over iterables like a list of numbers, checking for the condition number % divisor == 0 on each number. If the condition is true, then it returns the number. This way, by iterating over the list, we are collecting all divisible numbers into the list based on the filter condition.

Code Example

numbers = [10, 15, 22, 33, 40, 50, 60]
divisor = 10
divisible_numbers = [num for num in numbers if num % divisor == 0]
print(f'Numbers divisible by {divisor}:', divisible_numbers)

# Output: Numbers divisible by 10: [10, 40, 50, 60]Code language: Python (python)

Summary

These Python methods can be used to find the numbers divisible by a given number based on the use case and the performance requirements.

  • For Loop: Simple and easy to understand.
  • Filter Function: Functional programming approach.
  • Recursion: Useful for specific problems, like breaking down large tasks.
  • List Comprehension: Concise and Pythonic.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2025 pynative.com

Advertisement