# How to Create Multiplication Table in Python? (loop, list, lambda)

Python is an easy-to-learn language. As a beginner, you must be well-equipped with loops within Python. In this article, we will learn how to create a multiplication table in python, to understand the basics of loops. You'll also learn to use lambda functions, as a replacement for loop here.

So, let's get started!

## How to Make a Multiplication Table in Python?

Before jumping into different ways, let's take a look at our problem statement.

Problem Statement: Create a Multiplication Table for any number in Python

Example:

Let's say you need to generate and display a multiplication table for a number (say 4) in Python. The desired output must be like this:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

or

[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]

Let's move on to obtaining these solutions.

### 01) Using For loop

The for loop is used to repeat a block of code a specified number of times. Hence, the for loop works within a range.

The basic syntax of for loop is:

for reference_variable_name in (iterable or range):
{ code }

Let's create a multiplication table for a number using 'for loop' in Python:

```# Program: Multiplication Table in Python

# number
num = 5

# let's take a syntax for our table - num x (1 - 10) = num*(1-10)
# Since we're taking the table to 10, hence we'll iterate it 10 times

print("The multiplication table of ", num)
for i in range(1, 11):
print(f" {num} x {i} = {num*i}")
```

Output:

```The multiplication table of  5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
```

The range() function, as the name suggests, returns a sequence of numbers ranging from one number to another. The basic syntax of the range() function is:

range(initial, last, step)

with the default values of the initial and step being 0 and 1, respectively.

Also, note that I've used "11" in the range() function above, since range() in Python does not include the last number, i.e. it provides a sequence of one less than the last digit.

### 02) Using a while loop

The while loop helps us execute a statement as long as a condition is true. You can also use a while loop to keep count of a sequence (basically a for loop with more lines of code). Let's try it out:

```# Program: Multiplication Table in Python

# number
num = 5

# let's take a syntax for our table - num x (1 - 10) = num*(1-10)
# Since we're taking the table to 10, hence we'll iterate it 10 times

print("The multiplication table of ", num)
# initialize i for range
count = 1
while count <= 10:
print(f" {num} x {count} = {num*count}")
# increment the counter variable
count += 1
```

Output:

```The multiplication table of  5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
```

Here, you have to initialize a variable to keep the count (as did by the range() function in for loop).

Before moving forward, there's one problem we might've overlooked in the process. That is "to create the multiplication table for any number". So far, we've been initializing the number on our own. But in order to take the user's input for this, we need to use the input() function in Python.

Using the input() function in Python: Python offers an input() function to provide the facility to take input from the user. This function doesn't require any data type to be specified (and it generally stores data in string format).

In order to create a multiplication table for 'any' number in Python, you can use the input function to take the number as input from the user. Also, note that you need to type-case the input. As mentioned, it stores the input as a string, hence, it is required to convert the number into our desired data type (here, int).

Example:

```# Program: Using input() function in Python

# take input from the user
# used int() to typecast the input from string to integer
num = int(input("Enter a number: "))

print(num, " data type: ", type(num))
```

Output:

```Enter a number: 4
4  data type:  <class 'int'>
```

Now that you can obtain any number from the user, let's get back to the multiplication table for any number in Python.

### 03) Using List Comprehension

List comprehensions help to shrink your code to just one line. Let's take a look at creating a multiplication table for a number in Python using just one line of code.

```# Program: Multiplication Table in Python

# take input from the user
# used int() to typecast the input from string to integer
num = int(input("Enter a number: "))

# using list comprehension
table = [num * i for i in range(1, 11)]

print(table)
```

Output:

```Enter a number: 5
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
```

The list contains all the multiples of the given number.

Now, that you've seen the basic methods to create a multiplication table, let's see this problem without using any loop.

That's right! Without the loops. While this problem seems like an easy task with the loops, you'll often find this in interviews. But with a twist!

So, now we will learn how to create a multiplication table in python without using any loop. You must be wondering if is that even possible. What's the need for this?

So, although it is an easy task, it is indeed used to check your understanding of the 'Lambda function'. You're often required to think out of the box, and this problem checks your understanding of the language. So, let's take a look at it!

### 04) Using map and Lamda function

Functions are constituents of a block of code, which can be called whenever required within the program. These are needed to reduce code repetition. Functions are declared using the 'def' keyword in Python, followed by their name and arguments (if any). Such as:

def functionName(arguments): {code}

whereas Lambda functions are frequently used in accordance with higher-order functions that take or return one or more functions. Higher-order functions refer to functions like map() or filter(). Also, they accept any count of inputs.

Note that lambda functions do not require any label (like a function name). Also, according to syntax, lambda functions are limited to a single statement. You can declare a lambda function as:

variable = lambda {input} : {code}

Let's take an example for better understanding:

```# Program: Using lambda

# let's multiply two numbers using lambda
multiple = lambda x, y: x * y
# x and y are input parameters and the ':' is a separator
# x*y is the operation to be performed on inputs

print(multiple(4, 5))
```

Output:

```20
```

Note again that lambda functions are one-liners. The colon - ':' is a separator between the inputs (represented by x, y ) and operation (x*y).

Let's move on to creating a multiplication table for any number in Python using the lambda function:

```# Program: Create a multiplication table for any number in Python

# user-input
num = [int(input("Enter a number: "))]

# let's create our lambda function for the table
table = list(map(lambda x, y: x * y, list(range(1, 11)), num * 10))

print(table)
```

Output:

```[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
```

Hard to understand?

Let's break in the code for better understanding:

1. I've converted the number to a list (an iterable).
2. Use of map() function: The map() function allows you to process and transform all the items in an iterable without using a loop explicitly.

Now, let's unravel the lambda function:

1. lambda -> keyword
2. x, y -> input parameters
3. : -> separator
4. x*y -> operation to be performed
5. list(range(1,11)) -> to obtain a range from 1 to 10
6. num*10 -> This is done for recurrence in number. Since we need to create the table till 10, we'll need the number 10 times (hence appends the num in a list 10 times).

Easy, right?

The lambda function may seem overwhelming at the beginning but it is easy once you get well-equipped with it.

## Conclusion

Even a basic problem can have complex solutions. Interviewers might often turn up such questions to check your knowledge and understanding of the programming language.

You'll find many methods to get a multiplication table for any number in Python, with different formats or outputs. You can also find your own method to tackle the problem. In the end, your approach to a problem depends on your practice and ability to explore the language. Happy Coding!

### FavTutor - 24x7 Live Coding Help from Expert Tutors! 