
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get K Initial Powers of N in Python
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.
Advertisements