Print first m multiples of n without using any loop in Python
Last Updated :
19 Dec, 2024
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.
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)])
Output5 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)
Output5 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.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read