
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Get K initial powers of N
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.
- Related Articles
- Python program to randomly create N Lists of K size
- PHP program to find the sum of the first n natural numbers who are not powers of a specific number ‘k’
- Python program to replace first ‘K’ elements by ‘N’
- Find k numbers which are powers of 2 and have sum N in C++
- Python Program to get K length groups with given summation
- PHP program to find the sum of the 5th powers of first n natural numbers
- C++ Program to get number at position k after positioning n natural numbers
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Python program to find N-sized substrings with K distinct characters
- Program to check whether number is a sum of powers of three in Python
- Python program to get all subsets having sum s\n
- Program to find lexicographically smallest lowercase string of length k and distance n in Python
- Program to check n can be shown as sum of k or not in Python
- Program to reset a polygon to its initial state in Python
- Austin Powers in Python

Advertisements