1. How the For loop works:
For loop:
D={' A ': 1, ' B ': 2, ' C ': 3}for i in D: print (i)
Principle: The For loop is actually the d.__iter__ of the D in the back ()
# Todo Loop first time: obj=d.__iter__ () i=obj.__next__ () print (i) # Todo loop second time i=obj.__next__ () print (i) # Todo loop third time i=obj.__next__ ( ) Print (i)
2. While loop
I={' A ': 1, ' B ': 2, ' C ': 3}obj=i.__iter__ () while True: i=obj.__next__ () print (i)
This code will report exceptions such as:
At this point you will use try and except (snaps):
I={' A ': 1, ' B ': 2, ' C ': 3}obj=i.__iter__ () while True: try: i=obj.__next__ () print (i) except Stopiteration: Break
Step: (1), first establish a while loop
(2), a try syntax is built in the Recycle
(3), indent the loop body into the try
(4), except catch exception will it reported stopiteration exception written to except
(5), Break end cycle
python--function 18, iterator (ii)