Friday, December 27, 2024
Google search engine
HomeLanguagesCreate three lists of numbers, their squares and cubes using Python

Create three lists of numbers, their squares and cubes using Python

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




# 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)


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  # 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]

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):
    """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)


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  # 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]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments