Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Print Single and Multiple variable in Python
Next article icon

Print first m multiples of n without using any loop in Python

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

While loops are the traditional way to print first m multiples of n, Python provides other efficient methods to perform this task without explicitly using loops. This article will explore different approaches to Printing first m multiples of n without using any loop in Python.

Using List Comprehension

List comprehension provides a compact way to generate lists. Here, it can be used to calculate the multiples of n by iterating over a range of numbers.

Python
n = 5   m = 10    # Loop through numbers from 1 to 'm' #(inclusive) and multiply each by 'n' multiples = [n * i for i in range(1, m + 1)] print(multiples) 

Output
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50] 

Explanation:

  • range(1, m + 1) generates numbers from 1 to mmm, inclusive.
  • For each number in the range, it computes n×i.
  • The result is stored in a list multiples.

Let’s explore some more methods to print first m multiples of n without using any loop in Python.

Table of Content

  • Using map()
  • Using NumPy
  • Using * (Unpacking Operator)
  • Using Recursion

Using map()

map() function applies a given function to each item of an iterable. It can be used to compute multiples of n for the required range.

Python
n = 5 m = 10  # Use the map with lambda calculate the multiples of 'n' # range function generates # numbers from 1 to 'm' (inclusive) multiples = list(map(lambda x: n * x, range(1, m + 1)))  print(multiples) 

Output
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50] 
Explanation:
  • range(1, m + 1) generates numbers from 1 to m.
  • The lambda function multiplies each number in the range by n.
  • map() applies the lambda function to each element and the results are converted to a list using list().

Using NumPy

NumPy library is a powerful tool for numerical operations in Python. Its array manipulation capabilities make it an efficient choice for generating multiples.

Python
import numpy as np  n = 5 m = 10  # Use NumPy's arange function to create an array of multiples # Start at 'n', end at 'n * m + 1' (exclusive) multiples = list(np.arange(n, n * m + 1, n))  print(multiples) 

Output
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50] 
Explanation:
  • np.arange(start, stop, step) generates values from n to n×m with a step of n.
  • The result is converted into a list using list().

Using * (Unpacking Operator)

unpacking operator can be used creatively to print the multiples without explicitly storing them in a list.

Python
n = 5 m = 10  #Use a list comprehension to  #generate the multiples of 'n' #'*' unpacks the list, printing  #its elements separated by spaces print(*[n * i for i in range(1, m + 1)]) 

Output
5 10 15 20 25 30 35 40 45 50 
Explanation:
  • [n * i for i in range(1, m + 1)] generates a list of multiples.
  • * operator unpacks the list and prints its elements separated by spaces.

Using Recursion

Recursion is another way to compute and print multiples without using loops. It involves repeatedly calling a function until a base condition is met.

Python
def print_multiples(n, m, current=1):     if current > m:         return     print(n * current, end=' ')     print_multiples(n, m, current + 1)  n = 5 m = 10 print_multiples(n, m) 

Output
5 10 15 20 25 30 35 40 45 50 

Explanation:

  • The function print_multiples takes three arguments: the number n, the total multiples m and the current multiplier (defaulted to 1).
  • Base Condition: Stops when current > m.
  • Recursive Call: Prints n×current and calls itself with current + 1.


Next Article
Print Single and Multiple variable in Python

S

Striver
Improve
Article Tags :
  • Python
  • Python list-programs
  • python-list
  • python-puzzle
Practice Tags :
  • python
  • python-list

Similar Reads

  • Print Single and Multiple variable in Python
    In Python, printing single and multiple variables refers to displaying the values stored in one or more variables using the print() function. Let's look at ways how we can print variables in Python: Printing a Single Variable in PythonThe simplest form of output is displaying the value of a single v
    2 min read
  • Create multiple copies of a string in Python by using multiplication operator
    In Python, creating multiple copies of a string can be done in a simple way using multiplication operator. In this article, we will focus on how to achieve this efficiently. Using multiplication operator (*)This method is the easiest way to repeat a string multiple times in Python. It directly gives
    1 min read
  • Loops in Python - For, While and Nested Loops
    Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions). Additionally, Nested Loops allow looping within loops for more complex tasks. While all the ways provide similar basic functionality, they differ in th
    10 min read
  • Multiplication of two Matrices in Single line using Numpy in Python
    Matrix multiplication is an operation that takes two matrices as input and produces single matrix by multiplying rows of the first matrix to the column of the second matrix.In matrix multiplication make sure that the number of columns of the first matrix should be equal to the number of rows of the
    3 min read
  • Multiply All Numbers in the List in Python
    Our task is Multiplying all numbers in a list Using Python. This can be useful in calculations, data analysis, and whenever we need a cumulative product. In this article we are going to explore various method to do this. Using a loopWe can simply use a loop (for loop) to iterate over the list elemen
    2 min read
  • Print full Numpy array without truncation
    The goal here is to print the full NumPy array without truncation, meaning all elements should be displayed regardless of the array's size. By default, NumPy truncates large arrays to avoid overwhelming the display. However, there are different methods to configure the printing options so that the e
    2 min read
  • How to Create a List of N-Lists in Python
    In Python, we can have a list of many different kinds, including strings, numbers, and more. Python also allows us to create a nested list, often known as a two-dimensional list, which is a list within a list. Here we will cover different approaches to creating a list of n-lists in Python. The diffe
    3 min read
  • Python - Numbers in a list within a given range
    We are given a list and we are given a range we need to count how many number lies in the given range. For example, we are having a list n = [5, 15, 25, 35, 45, 55, 65, 75] and we are having range lower=20, upper=60 so we need to count how many element lies between this range so that the output shou
    4 min read
  • Program to print N minimum elements from list of integers
    Our task is to extract the smallest N elements from a list of integers. For example, if we have a list [5, 3, 8, 1, 2] and want the smallest 3 elements, the output should be [1, 2, 3]. We’ll explore different methods to achieve this. Using heapq.nsmallestThis method uses the nsmallest() function fro
    2 min read
  • Python List Comprehension with Slicing
    Python's list comprehension and slicing are powerful tools for handling and manipulating lists. When combined, they offer a concise and efficient way to create sublists and filter elements. This article explores how to use list comprehension with slicing including practical examples. The syntax for
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences