# How to Use NumPy to Create a Numeric Sequence in Python?

### NumPy

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

### numpy.array()

This method is going to be widely used in our program so it will be beneficial to study this method in advance so that it will be easy for us to understand the concepts. This method helps us to create a numpy array from already defined data structures in python like lists, tuples, and nested lists.

Installation of numpy:

pip install numpy

### Numeric Sequence in Python

A numeric series is a seriesĀ of values that follow a particularĀ pattern. To store this series of values, we’ll use a numpy array.

We need to choose a generator to generate a numeric series.

## Using NumPy to Create a Numeric Sequence in Python

### Method #1: Using Built-in Functions (Static Input)

Approach:

• Import numpy module using the import keyword
• Create a function say generate_values().
• Inside a function, return the square of a value passed
• Create an empty array using the array() function of the numpy module and store it in a variable.
• Loop in the range 0 to 8(excluding 8) using the for loop
• Pass the iterator value to the generate_values function and append it to the given array using the append() function
• Print the given array i.e square values.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Create a function say generate_values().
def generate_values(value):
# Inside a function, return the square of a value passed
return value**2

# Create an empty array using the array() function of the numpy module
# and store it in a variable.
gvn_arry = np.array([])
# Loop in the range 0 to 8(excluding 8) using the for loop
for value in range(0, 8):
# Pass the iterator value to the generate_values function and append it
# to the given array using the append() function
gvn_arry = np.append(gvn_arry, [generate_values(value)])

# Print the given array i.e square values
print("The Numeric sequence(square values) is:")
print(gvn_arry)

Output:

The Numeric sequence(square values) is:
[ 0. 1. 4. 9. 16. 25. 36. 49.]

### Method #2: Using Built-in Functions (User Input)

Approach:

• Import numpy module using the import keyword
• Create a function say generate_values().
• Inside a function, return the square of a value passed
• Create an empty array using the array() function of the numpy module and store it in a variable.
• Give the lower limit range as user input using the int(input()) function and store it in a variable.
• Give the upper limit range as user input using the int(input()) function and store it in another variable.
• Loop in the given lower and upper limit range using the for loop
• Pass the iterator value to the generate_values function and append it to the given array using the append() function
• Print the given array i.e square values in the given range.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Create a function say generate_values().
def generate_values(value):
# Inside a function, return the square of a value passed
return value**2

# Create an empty array using the array() function of the numpy module
# and store it in a variable.
gvn_arry = np.array([])
# Give the lower limit range as user input using the int(input()) function
# and store it in a variable.
lower_lmt = int(input("Enter some random number = "))
# Give the upper limit range as user input using the int(input()) function
# and store it in another variable.
upper_lmt = int(input("Enter some random number = "))
# Loop in the given lower and upper limit range using the for loop
for value in range(lower_lmt, upper_lmt):
# Pass the iterator value to the generate_values function and append it
# to the given array using the append() function
gvn_arry = np.append(gvn_arry, [generate_values(value)])

# Print the given array i.e square values
print("The Numeric sequence(square values) in the given range is:")
print(gvn_arry)

Output:

Enter some random number = 5
Enter some random number = 12
The Numeric sequence(square values) in the given range is:
[ 25. 36. 49. 64. 81. 100. 121.]