[Solved] Can someone explain what all the codes mean? PYTHON [closed]


I’ll keep it very simple, because this is very basic stuff.

i = 10

Variable i is initialised as 10.

while i > 0:
    print i*'*' + 2*(10-i)*' ' + i*'*'
    i -= 1

While variable i is bigger than 0, it prints the string * i times, an empty space 2 * (10 - i) times and then the string * i times again. Every loop it subtracts 1 off of i, so i starts out as 10 and goes all the way down to 1. This results in the following triangle / pyramid:

********************
*********  *********
********    ********
*******      *******
******        ******
*****          *****
****            ****
***              ***
**                **
*                  *

for x in range(2,11):
    print x* '*' + 2*(10-x)*' '+ x*'*'
    x += 1

Here the variable x starts off as 2 and increments all the way to 10, which results in an upside down version of the above triangle (not exactly, but it would if you used range(1, 11) instead). Also x += 1 is redundant here, as the range function will already increment x in steps of 1 (because the step argument is omitted). You can see this for yourself by running the following code:

for x in range(1, 11):
    print x 

solved Can someone explain what all the codes mean? PYTHON [closed]