How to Create a Multiplication Table for a Number in Python on Codewars?

Estimated read time 1 min read

Here’s an example of how to create a multiplication table for a number in Python on Codewars:

def multiplication_table(number):
    return [[i*j for j in range(1, number+1)] for i in range(1, number+1)]

In this solution, we define a function multiplication_table that takes an integer number as input. The function uses a list comprehension to create a 2D list that contains the products of each pair of numbers from 1 to number. The outer list comprehension iterates over the numbers from 1 to number and creates a list of products for each number. The inner list comprehension iterates over the numbers from 1 to number and creates a list of products for the current number.

To test this function on Codewars, you can submit the code and run the provided test cases. If the code passes all the test cases, you will receive a message indicating that your code has been accepted.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply