Create three lists of numbers, their squares and cubes using Python
Last Updated :
27 Sep, 2023
In this article, we are going to create a list of the numbers in a particular range provided in the input, and the other two lists will contain the square and the cube of the list in the given range using Python.
Input: Start = 1, End = 10
Output:
Numbers_list = [1,2,3,4,5,6,7,8,9,10]
Squares_list= [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Cubes_list = [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
Create three lists of numbers, their squares, and cubes
- Using loop
- By defining own functions
- Using lambda function
- Using NumPy
Creating Square and Cube Using for Loop
In this exampleÂ, a list of numbers [1, 2, 3, 4, 5] is created. The squares and cubes of these numbers are then calculateÂd using list comprehensions. As a result, two seÂparate lists are geneÂrated: one for the squareÂs and another for the cubes.
Python3
numbers = [ 1 , 2 , 3 , 4 , 5 ]
squares = [num * * 2 for num in numbers]
cubes = [num * * 3 for num in numbers]
print ( "Numbers:" , numbers)
print ( "Squares:" , squares)
print ( "Cubes:" , cubes)
|
Output
Numbers: [1, 2, 3, 4, 5]
Squares: [1, 4, 9, 16, 25]
Cubes: [1, 8, 27, 64, 125]
Creating list of Square and Cube Using Lambda function
In this approach, we utilize lambda functions and the map function to calculate the squareÂs and cubes for every numbeÂr within the specified rangeÂ. The map function then applies the lambda function to each elemeÂnt in the list of numbers, resulting in seÂparate lists containing their respeÂctive squares and cubes.
Python3
def generate_lists(start, end):
numbers = list ( range (start, end + 1 ))
squares = list ( map ( lambda x: x * * 2 , numbers))
cubes = list ( map ( lambda x: x * * 3 , numbers))
return numbers, squares, cubes
if __name__ = = "__main__" :
start_num = 1
end_num = 5
numbers_list, squares_list, cubes_list = generate_lists(start_num, end_num)
print ( "Numbers:" , numbers_list)
print ( "Squares:" , squares_list)
print ( "Cubes:" , cubes_list)
|
Output
Numbers: [1, 2, 3, 4, 5]
Squares: [1, 4, 9, 16, 25]
Cubes: [1, 8, 27, 64, 125]
Create a List of Squares and Cubes by Defining Own Functions
The code includes three functions: calculateÂ_squares, which calculates the squareÂs of numbers; calculate_cubes, which calculateÂs the cubes of numbers; and geÂnerate_lists, which combines theÂse computations. In the main block, lists of numbers, squareÂs, and cubes are geneÂrated for the numbers 1 to 5. TheÂse lists are then printeÂd for display.
Python3
def calculate_squares(numbers):
return [num * * 2 for num in numbers]
def calculate_cubes(numbers):
return [num * * 3 for num in numbers]
def generate_lists():
numbers = [ 1 , 2 , 3 , 4 , 5 ]
squares = calculate_squares(numbers)
cubes = calculate_cubes(numbers)
return numbers, squares, cubes
if __name__ = = "__main__" :
numbers_list, squares_list, cubes_list = generate_lists()
print ( "Numbers:" , numbers_list)
print ( "Squares:" , squares_list)
print ( "Cubes:" , cubes_list)
|
Output
Numbers: [1, 2, 3, 4, 5]
Squares: [1, 4, 9, 16, 25]
Cubes: [1, 8, 27, 64, 125]
Create lists of Squares and Cubes Using Numpy
In this approach, we use NumPy to efficiently generate arrays of numbers, squares, and cubes. We then convert these arrays to lists using the tolist() method. The rest of the code structure is similar to the previous examples.
Python3
import numpy as np
def generate_lists(start, end):
numbers = np.arange(start, end + 1 )
squares = numbers * * 2
cubes = numbers * * 3
return numbers.tolist(), squares.tolist(), cubes.tolist()
if __name__ = = "__main__" :
start_num = 1
end_num = 5
numbers_list, squares_list, cubes_list = generate_lists(start_num, end_num)
print ( "Numbers:" , numbers_list)
print ( "Squares:" , squares_list)
print ( "Cubes:" , cubes_list)
|
Output:
Numbers: [1, 2, 3, 4, 5]
Squares: [1, 4, 9, 16, 25]
Cubes: [1, 8, 27, 64, 125]
Share your thoughts in the comments
Please Login to comment...