3. Power

** is the power operator. The expression base ** exponent raises base to exponent:

  • A positive exponent multiplies the base by itself that many times.
  • An exponent of 0 gives 1: 9 ** 0 == 1.
  • A negative exponent gives a float: 2 ** -1 is 1 / 2, which is 0.5.

** is right-associative: chained powers are grouped from right to left.

pow() is a built-in function. Its signature is:

  • base — the number to raise.
  • exponent — the power to raise it to.
  • mod — optional. When given, the function returns the remainder after raising base to exponent.

In this lesson you only need the two-argument form, pow(base, exponent).

#01

Square a number

Use the power operator (**) to raise 5 to the power of 2, and print the result.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Cube a number

Use the power operator (**) to raise 2 to the power of 3, and print the result.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

Negative exponent

Use the power operator (**) to raise 2 to the power of -1, and print the result. A negative exponent produces a float.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#04

Right-associative power

Use ** to write 2 ** 3 ** 2 without parentheses, and print the result. Python groups chained powers from right to left, so it computes 2 ** (3 ** 2).

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#05

Use the pow function

Use the built-in function pow() with the two arguments 2 and 10, and print the result. pow(base, exponent) returns base raised to exponent.

Print exactly:

Create a free account to save your progress and unlock chapter 2.