Home > Mobile >  any one tell me how x s printed x_count times but we are not using any * operator refer line 5 [clos
any one tell me how x s printed x_count times but we are not using any * operator refer line 5 [clos

Time:09-30

code :

numbers = [5, 2, 5, 2, 2]

for x_count in numbers:

    output = ''

    for count in range(x_count):
        output  = 'x' #any one tell me how it is printed x_count times but we are not using any * operator
    print(output)

I am beginner in python programming it is a basic in python but i can't sort it out so anyone help me understand the above mentioned issue

CodePudding user response:

The 'x' is just a character here. So when you do output = 'x' you are actually appending the character x to the output string.

CodePudding user response:

Let's break it down:

numbers = [5, 2, 5, 2, 2]

numbers is a list (of ints, here).

for x_count in numbers:

    output = ''

make a new variable, output, which is an empty string, for every entry in numbers.

    for count in range(x_count):

Iterate x_count no of times (note that we don't actually do anything with count).

        output  = 'x' 

Add the string 'x' to the var output we made above.

Then we print the output.

I'm not really sure that's much clearer than the code originally was, but maybe it helps.

CodePudding user response:

You are concatenating 'x' to output

output ='x' is nothing but output = output 'x'


Your output will be

xxxxx
xx
xxxxx
xx
x
  • Related