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 LoopIn this example, a list of numbers [1, 2, 3, 4, 5] is created. The squares and cubes of these numbers are then calculated using list comprehensions. As a result, two separate lists are generated: one for the squares and another for the cubes.
Python
# Create a list of numbers
numbers = [1, 2, 3, 4, 5]
# Calculate squares
squares = [num ** 2 for num in numbers]
# Calculate cubes
cubes = [num ** 3 for num in numbers]
# Print the lists
print("Numbers:", numbers)
print("Squares:", squares)
print("Cubes:", cubes)
OutputNumbers: [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 functionIn this approach, we utilize lambda functions and the map function to calculate the squares and cubes for every number within the specified range. The map function then applies the lambda function to each element in the list of numbers, resulting in separate lists containing their respective squares and cubes.
Python
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 # Start number
end_num = 5 # End number
numbers_list, squares_list, cubes_list = generate_lists(start_num, end_num)
# Print the lists
print("Numbers:", numbers_list)
print("Squares:", squares_list)
print("Cubes:", cubes_list)
OutputNumbers: [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 FunctionsThe code includes three functions: calculate_squares, which calculates the squares of numbers; calculate_cubes, which calculates the cubes of numbers; and generate_lists, which combines these computations. In the main block, lists of numbers, squares, and cubes are generated for the numbers 1 to 5. These lists are then printed for display.
Python
def calculate_squares(numbers):
"""Calculate the squares of a list of numbers."""
return [num ** 2 for num in numbers]
def calculate_cubes(numbers):
"""Calculate the cubes of a list of numbers."""
return [num ** 3 for num in numbers]
def generate_lists():
"""Generate a list of numbers and their squares and cubes."""
numbers = [1, 2, 3, 4, 5] # You can change this list to any numbers you want
squares = calculate_squares(numbers)
cubes = calculate_cubes(numbers)
return numbers, squares, cubes
if __name__ == "__main__":
numbers_list, squares_list, cubes_list = generate_lists()
# Print the lists
print("Numbers:", numbers_list)
print("Squares:", squares_list)
print("Cubes:", cubes_list)
OutputNumbers: [1, 2, 3, 4, 5]
Squares: [1, 4, 9, 16, 25]
Cubes: [1, 8, 27, 64, 125]
Create lists of Squares and Cubes Using NumpyIn 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.
Python
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 # Start number
end_num = 5 # End number
numbers_list, squares_list, cubes_list = generate_lists(start_num, end_num)
# Print the 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]
|