for loop iterates
for i in xxx:
Block
i: is a variable, the value is the one which is taken out xxx stored inside the i
XXX: may be a sequence (SEQ comprising: a list of tuples, string), the object may be an iterative
>>> for i in "abc": ... print(i) ... a b c
# Debugging techniques Example: list of requirements [1,2,3,4] and print out the results of each step
Result = 0 >>>
>>> I for in [1,2,3,4]:
... Print ( "value of I", I)
... I = Result +
... Print ( "computing result ", result)
...
value i 1
results 1
i 2 value of
the calculation result 3
i value of 3
results. 6
i value of 4
results 10
>>> Print (result)
10
# Iterate through the index cycle
>>> fruits = ['banana','apple','mango']
>>> for index in range(len(fruits)):
... print(fruits[index])
...
banana
apple
mango
>>>
The above examples we use the built-len () function and Range (), len () function returns the length of the list, i.e. the number of elements. range returns a sequence number.
while loop iterates
while True:
Block
True: the conditions, as long as the condition is True code block will always be executed
Infinite loop scenarios:
1 server: the general infinite loop
2 You do not know how many times the cycle time is necessary to use an infinite loop
>>> a = 0 >>> while a<=5: ... print(a) ... a+=1 #相当于a=a+1 ... 0 1 2 3 4 5
# Exercises:
Achieved with infinite loop:
A user input number, odd or even is determined
When the input number is 100 times, the end of the cycle
The while. 1 >>>:
... user_num = int (INPUT ( "Enter a number:"))
... IF user_num% 2 == 0:
... Print ( "% S is an even number"% user_num) # template string
... the else:
... Print ( "% S is an odd number"% user_num)
... IF user_num == 100:
... BREAK
...
Please enter a number: 2
2 being even
enter a digital: 3
3 is an odd number
, enter a number: 4
. 4 is an even number
, enter a number: 5
5 is an odd number
, enter a number: 6
6 is an even number
, enter a number: 100
100 is an even number
>>>
break和continue
break: a complete end to the cycle
continue: out of this cycle, the next execution time
Comparative Example:
>>> for I in Range (10 ): ... IF I == 3 : ... BREAK # for the entire cycle is ended,> = 3 is not a digital printing ... Print (I) ... 0 . 1 2 >>> for i in the Range (10 ): ... IF i == 3 : ... the continue # i == 3 skip this cycle does not print, for circulation loop continues back ... Print (i) ... 0 . 1 2 . 4 . 5 . 6 . 7 . 8 . 9 >>>
for / while ... else ... with a strange usage:
# No BREAK
>>> for i in the Range (5 ): ... IF i == 3 : ... the Continue ... Print (i) ... the else : #else statement is executed ... Print ( " break NO " ) ... 0 1 2 4 NO break
# break there
>>> for i in range(5):
... if i == 3:
... break
... print(i)
... else: #else没有被执行
... print("no break")
...
0
1
2
>>>
Summary: As long as there is no break execution cycle internal body else will execute the code section
Example: Title: [ 1,2,3,4,5,7 ] You enter a number, a judgment whether or not this list, execute three times this logic. Algorithm: 1, 3 cycles ( for ) for I in Range (3 ) 2 , a digital input (INPUT), to a variable stored in user_num = int (INPUT ( " Enter a number: " )) 3, is determined this figure is not [1,2,3,4,5,7 ] desc_list = [1,2,3,4,5,7 ] . 4 , if the printing, what; no longer be printed out. IF user_num in desc_list: Print ( " % S in " % user_num) the else : Print ( " % S not" % User_num) achieved: >>> desc_list = [1,2,3,4,5,7 ] >>> for I in Range (. 3 ): ... user_num = int (INPUT ( " Please enter a number: " )) ... IF user_num in desc_list: ... Print ( " % S in " )% user_num # template string expression"% SA Number "%. 1 '. 1 A Number' ... the else : ... Print ( " % S is not "% User_num) ... Please enter a number:. 5 . 5 In Please enter a number: 2 2 In Please enter a number: . 1 . 1 in >>>