Python program to find the square and cube of every number in the given list of numbers using lambda expression

Published by user on

In this post, we will see how to calculate the square and cube of every number of the given list of numbers. We will use map() along with lambda expression for calculation.

Algorithm
Step 1: Declare a list of numbers
Step 2: Find the square of number by multiplying the number itself two times.
Step 3: Print the numbers with square value
Step 4: Find the cube of number by multiplying the number itself three times.
Step 5: Print the numbers with cube value
Step 6: End
Example

Input
[1, 2, 3, 4, 5]
Output
Square of every number of the provided list:
[1, 4, 9, 16, 25]
Cube of every number of the provided list:
[1, 8, 27, 64, 125]

Program

numbers = [1, 2, 3, 4, 5]
print("List of numbers:")
print(numbers)
print("\nSquare of every number of the provided list:")
square_numbers = list(map(lambda x: x ** 2, numbers))
print(square_numbers)
print("\nCube of every number of the provided list:")
cube_numbers = list(map(lambda x: x ** 3, numbers))
print(cube_numbers)

Output

List of numbers:
[1, 2, 3, 4, 5]

Square of every number of the provided list:
[1, 4, 9, 16, 25]

Cube of every number of the provided list:
[1, 8, 27, 64, 125]
Categories: python