Python Table of Powers Example and Explanation

Join Community

Summary

In How_The_Python_Works this announcement walks through printing a table of i, i^2, i^3, and i^4 using Python formatting and a while loop. It explains header formatting with %-ns and the print line print("%-4d%-6d%-7d%-8d" % (i, i**2, i**3, i**4)) plus loop progression. This helps members produce aligned numeric tables and understand exponent syntax.

Original Post

here i can hep you in any code

Reply

question no. 1
i = 1
print("%-4s%-5s%-6s%-7s"%("i","i2","i3","i4"))
while i<=5:
print("%-4d%-6d%-7d%-8d"%(i,i2,i3,i4))
i = i+1

Reply

short explanation:

First, you write i = 1 to start the loop from number 1. Then you print the heading using print("%-4s%-5s%-6s%-7s"%("i","i2","i3","i4")), where %-4s and all just help give space between the words so it looks like a table. After that, you run a while loop from 1 to 5. Inside the loop, you use print("%-4d%-6d%-7d%-8d"%(i,i2,i3,i4)) which prints the number, its square, cube and power 4. Here, i2 means i power 2 (like 2² = 4), i**3 means i power 3, and so on. The %-4d part just helps align the numbers nicely in columns. After printing each row, you do i = i + 1 to move to the next number until it reaches 5.

Reply

Long explanation (But Easy)

chatGPT explanation:

So first you write:

i = 1
This line is just starting the number from 1. You will use this i to show powers like i², i³, and i⁴.

Then comes:

print("%-4s%-5s%-6s%-7s" % ("i", "i2", "i3", "i4"))
This line prints the table heading. It just shows "i", "i2", "i3", and "i4" on the top like column names. The %-4s, %-5s, etc. are used for spacing. It makes sure everything lines up properly and looks like a clean table. The s stands for string, so it's printing words.

Now the main part is the loop:

while i <= 5:
This line says, repeat the next block of code as long as i is less than or equal to 5. So it will run 5 times for i = 1, 2, 3, 4, 5.

Inside the loop, this line runs:

print("%-4d%-6d%-7d%-8d" % (i, i2, i3, i**4))
Now this is the line that prints the actual numbers. Here’s what happens:

% means you are formatting numbers.

%-4d means print a number (d means digit) and leave 4 spaces after it (so that it looks aligned).

i is printed as it is (like 1, 2, 3...).

i**2 means i to the power 2 (square of i). So if i = 2, then i² = 4.

i**3 means i to the power 3 (cube of i). So if i = 2, then i³ = 8.

i**4 means i to the power 4.

So basically this line prints the number i, then i², then i³, then i⁴ all in one line, and spaces them neatly using the formatting.

Then finally, you write:

i = i + 1
This line just increases the value of i by 1. So after printing for i = 1, it goes to 2, then 3, and so on. Once it becomes 6, the while condition i <= 5 becomes false, and the loop stops.

So the whole program will print:

i i2 i3 i4
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625

Reply

My explanation:

first line i = 1 means we start from 1, simple

then print("%-4s%-5s%-6s%-7s"%("i","i2","i3","i4")) this is just to show table headings like i, i square, i cube, i power 4 – and those %-4s, %-5s things are just to keep space so everything looks clean and aligned

now while i <= 5: this means bro keep running until i becomes more than 5

inside loop, this line print("%-4d%-6d%-7d%-8d"%(i,i2,i3,i4)) is the main one
here, it prints i, then i square (i
2), i cube (i3), and i power 4 (i4) — all with spaces so it looks like a table

after that, i = i + 1 is just moving to the next number

so output will be like a full table of powers of numbers from 1 to 5, neat and clean

that’s it bro, simple game 😄

The latest from How_The_Python_Works