1. If Else
Syntax: if expression1:
Statement1 (s)
Elif expression2:
Statement2 (s)
Else
Statement3 (s)
2. For
Syntax: for Iterating_var in sequence:
Statements (s)
Where: sequence is a sequence (string, tuple, any one in the list) or dictionary
Example 1:
#!/usr/bin/pythonFruits=['Banana','Apple','Mango'] forIndexinchrange (len (fruits)):Print 'Current fruit:', Furits[index]Print "Good bye!"
Example 2:1 to 100 of cumulative
# !/usr/bin/python num=0 for in range (1,101): num+ =xprint num
extension : Range to quickly generate a sequence
Format: Range (i,j[, step value]) where I default value is 0, stepping value defaults to 1
Example 3: Traversing a dictionary
d={1:111,2:222,5:555,3:333} for in D: print d[x] for in D.items (): #d.items () returns the Key,value in the dictionary as tuples [(1,111), (2,222), (3,333), (5,555)] print k print V
(1) The contents of Else:else in the For loop are executed only when the For loop is terminated normally, not when the for loop is terminated abnormally
Cases:
# !/usr/bin/python Import Time for in range: print x time.sleep (1) else:
print
"
ending
"
(2) Break,continue,pass (code pile, play a role in a placeholder)
3. While
Syntax: while expression:
Statement (s)
Cases:
#!/usr/bin/pythonx="" whilex!="Q": Print "Hello"x= Raw_input ("Please input something,q for quit:") if notX:#if the input is empty, not X is true, jumping out of the loop BreakElse: Print "ending ..." #input q, Loop end, will print this sentence; input blank, not print
Process Control of Python learning