+3

Why not "def"?

it=iter('abcdef') for i in range (3,6): print (next(it), end="")

2/13/2020 2:58:45 PM

Navneet Ghosh

12 Answers

New Answer

+5

Your for loop will run 3 times. Then print a,b,c.

+5

I am testing here. See I can print "def " using test= "abcdef". See this code. https://code.sololearn.com/crfhY0Oobo65/?ref=app

+5

Navneet Ghosh The range function define how many times your loop should iterate which is 3 as mentioned above, it will by default does nothing to your code except you commanded it to do so... How about slicing "it" it[3:] then iterate over that, I.e iter(it[3:])

+3

Why def?

+3

Navneet Ghosh Try this for i in range (196,199): print (next(it), end="") It will print "abc" as well.

+2

it=iter('abcdef') for i in range (3): next(it) for i in range (3): print (next(it), end="")

+2

iter creates an iterator. Next pops out the next item. That's like when you get a soda from the vending machine: You hit the button, first can comes out.

+1

Oma Falk output is abc not def

+1

Navneet Ghosh did u understand why not def?

+1

I thought you meant why not a function but I get it now.

0

Oma Falk little bit As commented above that loop will run 3 times and after that iteration will work

0

I have test ur logic and reached on a conclusion that u were printed iter value within the for loop which was iterating 3 time that is why u got abc. IF YOU iterated range(6) instead of range(3,6), then you would got entirely string.... IF u r agree to my answer so, plz thumb up which will help others to view my answer. https://code.sololearn.com/cQPgsvaQ9TOm/?ref=app