---
title: power()
url: https://docs.vultr.com/python/third-party/numpy/power
description: Exponentiation Calculation
publish_date: 2024-11-18T21:40:00.062466Z
last_updated: 2024-11-18T21:40:00.062466Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/961/db938610-482b-44f0-86d0-a42654e3ae41.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python Numpy power() - Exponentiation Calculation

## Introduction
The `power()` function in Python's NumPy library is a versatile tool for performing element-wise exponentiation of numbers in an array. This function raises each element of the base array to the power of the corresponding element in the exponent array, making it highly useful in mathematical and scientific computations where such operations are routine.

In this article, you will learn how to effectively leverage the `numpy.power()` function in different scenarios. Explore how to use this function with arrays of various shapes and data types, and understand how broadcasting affects its behavior when arrays of different sizes are involved.

## Using NumPy power() for Exponentiation

### Basic Usage of power()
1. Import the NumPy library.
1. Define the base array and the exponent array.
1. Apply the `power()` function.

    ```python
    import numpy as np

    base = np.array([2, 3, 4])
    exponent = np.array([2, 3, 4])
    result = np.power(base, exponent)
    print(result)
    ```

    This code snippet raises every element of the `base` array to the corresponding power specified in the `exponent` array, resulting in the output `[4, 27, 256]`.

### Handling Different Data Types
1. Understand that NumPy handles various data types gracefully when using `power()`.
1. Perform exponentiation on arrays containing float and integer types.

    ```python
    base = np.array([2.0, 3.1, 4.5])
    exponent = np.array([2, 3, 1])
    result = np.power(base, exponent)
    print(result)
    ```

    In this example, NumPy computes the power operation between floats and integers seamlessly, producing `[4.0, 29.791, 4.5]`.

### Using Broadcasting
1. Recognize the usefulness of broadcasting in NumPy when arrays have different shapes.
1. Use `power()` with a scalar as the exponent.

    ```python
    base = np.array([[1, 2], [3, 4]])
    exponent = 3
    result = np.power(base, exponent)
    print(result)
    ```

    Here, NumPy broadcasts the scalar `exponent` across the `base` array, effectively cubing every element in the array to yield `[[ 1  8] [27 64]]`.

## Conclusion
NumPy's `power()` function facilitates effective exponentiation of array elements in Python, making complex mathematical computations more accessible and efficient. By integrating the examples provided, you harness the full potential of this function—whether dealing with scalar values, different data types, or leveraging broadcasting for operations on mismatched array shapes. Equip your computational toolkit with `power()` to simplify and optimize your data-processing tasks.