Python Program to Get K initial powers of N

PythonServer Side ProgrammingProgramming

When it is required to get the specific number of power of a number, the ‘**’ operator is used along with list comprehension.

Example

Below is a demonstration of the same

n = 4

print("The value n is : ")
print(n)
k = 5
print("The value of k is : ")
print(k)
result = [n ** index for index in range(0, k)]

print("The square values of N till K : " )
print(result)

Output

The value n is :
4
The value of k is :
5
The square values of N till K :
[1, 4, 16, 64, 256]

Explanation

  • The values for ‘n’ and ‘k’ are defined and are displayed on the console.

  • List comprehension is used to iterate through the numbers in the range of ‘k’.

  • The ‘**’ operator is used to get the power of the value.

  • This is assigned to a variable.

  • This is displayed as output on the console.

raja
Updated on 21-Sep-2021 07:18:06

Advertisements