Basic python tutorials-conditional and cyclic statements-python cyclic statements
The following values are viewed as false by the interpreter ):
False None 0 "" () {} []
Everything else is interpreted as true.
>>> True
True
>>> False
False
>>> True = 1
True
>>> False = 0
True
>>> True + False + 42
43
Bool function -- used to convert other values, such
>>> Bool ([])
False
>>> Bool ('hello, World ')
True
If else elif
Is and is not -- judge whether two variables are the same object
>>> X = y = [1, 2, 3]
>>> Z = [1, 2, 3]
>>> X = y
True
>>> X = z
True
>>> X is y
True
>>> X is z
False
As shown in the preceding example, the is operator determines the same identity. Both the variables x and y are bound to the same list, and the variable z is bound to another list with the same values and order. They may have the same value but are not the same object.
In and not in -- Membership Operator
Assert -- program crashes when the condition is not true
>>> X = 5
>>> Assert 0 <x <10
>>> Assert 5 <x <4
Traceback (most recent call last ):
File "<pyshell #25>", line 1, in <module>
Assert 5 <x <4
AssertionError
Range -- the built-in range function, which contains the lower limit but does not include the upper limit, as shown in figure
>>> Range (0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for num in range(0, 10): print num,
The result is as follows:
>>>
0 1 2 3 4 5 6 7 8 9
Cyclically traverse the dictionary. You can use the sequence to unpack, as shown in figure
d = {'x':1, 'y':2}for key, value in d.items(): print key, 'corresponds to', value
Result
>>>
Y corresponds to 2
X corresponds to 1
Zip-you can "compress" any number of sequences together, and then return a list of tuples. At the same time, it can cope with sequences of varying lengths, it stops when the shortest sequence is used up, as shown in figure
>>> Zip (range (5), xrange (10000 ))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
>>> Names = ['A', 'B', 'C']
>>> Ages = [45, 23, 98]
>>> Zip (names, ages)
[('A', 45), ('B', 23), ('C', 98)]
Parallel iteration, such
names=['a', 'b', 'c']ages = [45, 23 ,98]for name, age in zip(names, ages): print name, 'is', age, 'old'
Result
>>>
A is 45 old
B is 23 old
C is 98 old
Number iteration-the object in the iteration sequence, and the index of the current object, as shown in figure
names=['Mr.a', 'Ms.b', 'Mr.c']for index, name in enumerate(names): if 'Mr' in name: names[index] = 'nan'for name in names: print name,
Result
>>>
Nan Ms. B nan
Flip and sort iterations (sorted and reversed) -- Scope any sequence or iteratable object. Instead of modifying the object in the same place, the returned version is flipped or sorted, however, the returned object cannot be directly indexed, sharded, or called by the list method. You can use the list type to convert the returned object, as shown in
>>> Sorted ([4, 3, 8, 6, 3,])
[3, 3, 4, 6, 8]
>>> Sorted ('hello, world! ')
['','! ', 'D', 'E', 'h', 'l', 'O', 'O ', 'R', 'w']
>>> List (reversed ('hello, world! '))
['! ', 'D', 'l', 'R', 'O', 'w', '',', ', 'O', 'l ', 'l', 'E', 'H']
>>> ''. Join (reversed ('hello, world! '))
'! Dlrow, olleh'
Break/continue -- jump out of the loop/continue the next loop
The else clause in the loop -- If break is not called in the loop, the else clause is executed, as shown in figure
from math import sqrtfor n in range(99, 81, -1): root = sqrt(n) if root == int(root): print n breakelse : print "Didn't dind it!"
Result
>>>
Didn't dind it!
- List derivation-lightweight Loop
List derivation is a method to create a new list using another list, such
>>> [(X, y) for x in range (3) for y in range (3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
>>> Girls = ['Alice ', 'bernice', 'clarice ']
>>> Boys = ['chris ', 'arnold', 'bob']
>>> [B + '+ g for B in boys for g in girls if B [0] = g [0]
['Chris + clarice ', 'arnold + alice', 'Bob + bernice ']