Why not "def"? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 3

Why not "def"?

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

13th Feb 2020, 2:58 PM
Navneet
Navneet - avatar
11 ответов
+ 6
Your for loop will run 3 times. Then print a,b,c.
13th Feb 2020, 3:12 PM
Pedro H.J
Pedro H.J - avatar
+ 6
I am testing here. See I can print "def " using test= "abcdef". See this code. https://code.sololearn.com/crfhY0Oobo65/?ref=app
13th Feb 2020, 3:08 PM
Pedro H.J
Pedro H.J - avatar
+ 4
Navneet Ghosh Try this for i in range (196,199): print (next(it), end="") It will print "abc" as well.
13th Feb 2020, 3:17 PM
Pedro H.J
Pedro H.J - avatar
+ 3
Why def?
13th Feb 2020, 3:07 PM
Oma Falk
Oma Falk - avatar
+ 2
it=iter('abcdef') for i in range (3): next(it) for i in range (3): print (next(it), end="")
13th Feb 2020, 3:17 PM
Oma Falk
Oma Falk - avatar
+ 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.
13th Feb 2020, 6:57 PM
HonFu
HonFu - avatar
+ 1
Oma Falk output is abc not def
13th Feb 2020, 3:23 PM
Navneet
Navneet - avatar
+ 1
Navneet Ghosh did u understand why not def?
13th Feb 2020, 3:28 PM
Oma Falk
Oma Falk - avatar
+ 1
I thought you meant why not a function but I get it now.
14th Feb 2020, 9:59 AM
Sonic
Sonic - avatar
0
Oma Falk little bit As commented above that loop will run 3 times and after that iteration will work
13th Feb 2020, 3:32 PM
Navneet
Navneet - avatar
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
14th Feb 2020, 7:54 PM
vijay
vijay - avatar