Computer Science, asked by tushar38011, 8 months ago

What is the output of the following x=123 for i in x: print(i)

Answers

Answered by Equestriadash
16

Source code:

>>> x = 123

>>> for i in x:

        print(i)

Output:

Traceback (most recent call last):

 File "<pyshell#2>", line 1, in <module>

   for i in x:

TypeError: 'int' object is not iterable

As understood from the output given above, the source code results in an error.

This is because the data type assigned to x, was that of an integer type.

There really is nothing to be looped in an integer type value.

Integer values cannot be iterated. It will only result in an error.

You can however alter the code, to get a different output.

>>> x = "123"    #convert it into a string

>>> for i in x:

        print(i)

Output:

1

2

3

Through this, all the characters in the string are printed in a different line.

Answered by SONAXRAMPY
0

SIMPLE ANSWER IS ERROR

Attachments:
Similar questions