5.1 If statement
Nothing to say, the IF statement syntax is as follows:
If expression:
Expr_true_suit
5.1.1 Multi-Conditional expressions
A single If statement can achieve multiple conditional or negative judgments through Boolean operator And,or,not.
If not warn and (system_load>=10):
print ' warning:losing RESOURCE '
Warn+=1
5.2 Else statement
If the IF condition is False the program will execute the ELSE after statement.
If expression:
Expr_true_suit
Else
Expr_false_suit
5.3 Elif is else-if
The elif is used to check if multiple expressions are true, if the position executes subsequent statements. Elif can have any one, but else can only have one.
The syntax is as follows
If expression1:
Expr1_true_suit
Elif expression2:
Expr2_true_suit
Elif Expression3:
Expr3_ture_suit
Else
None_of_the_above_suit
5.4 Three-dimensional expression
Python does not support ternary expressions (c?x:y) in python2.5, and is quite aesthetically pleasing. Syntax: X if C else Y
if Else y>>>smaller3
5.5 While loop
5.5.1 General Syntax
When the condition is true, the statement inside the statement block is executed until the condition is false.
While expression:
Suite_to_repeat
5.5.2 Count Cycle
Adding a count statement to a statement quickly ends when the loop executes a certain number of times, as in the following example.
Count=0
while (count<9):
print ' The index is: ', count
Count+=1
5.5.3 Wireless Loop
Condition is always true, and the program is always executed in the loop.
While True:
Suite_to_repeat
5.6 For Loop
Python provides another loop for loop, which can traverse the sequence members, which can be used in list parsing and generator, which automatically calls the next () method.
1. For sequence type
For can iterate over different sequence objects including, strings, lists, tuples.
>>>for eachletter in ' Names '
print ' Current letter: ', Eachletter
Current Letter:n
Current LETTER:A
Current Letter:m
Current Letter:e
Current Letter:s
For lists and tuples, the iterations are similar to the above.
2. Iterating through the sequence index
The index to set up the sequence is to use the range (Len (list)) to index the first
>>>string='python'>>>range (len (string)) [0,1,2,3,4,5] >>> for in range (len (string)):>>> print string [Index]python
3. Using Item and Index iterations
Use the built-in function enumerate () function to iterate over items and indexes at the same time.
>>>namelist=['Donn','Ben', ' David ',' Wendy ' ]>>> for in enumerate ( namelist): ... Print ' %d,%s '% (i+1, eachname)1, Donn2, Ben3, David4,wendy
5.7 Break
Break is used in the while and for loops when the execute to break statement jumps out of the yourselves loop to execute the next statement.
def factor (num): count=num/2 while count>0: if num%count==0: Print count,' is largest factorof', num break count-=1 factor (Input ('Please enter the number:'))
The program is used to calculate the greatest common divisor of a number.
5.8 Continue
As with continue in other languages, continue means to start the next loop immediately after the current loop is completed, assuming that the cyclic prerequisites are still satisfied.
Valid=Falsecount=3 whileCount>0:input=raw_input ('Enter password:\n') forEachpwdinchpasswdlist:ifeachpwd==Input:valid=TruePrint 'Welcome back' Break if notvalid:Print 'Invalid input'Count-=1Continue Else: Break
The above code is an example of the use of continue.
5.9 Pass Statement
The pass statement is provided as a placeholder in Python. If there is no statement in a block of statements the compiler will error, then you can use the PASS statement as a placeholder, it does not do anything, you can later fill in the code tag.
Other uses of 5.10 else
In other languages, such as C, else does not appear outside the conditional statement, but in Python you can use else outside of the loop. This is easy to understand, because the loop is to do the first is the condition of judgment, then there must be conditions are not satisfied when the need to execute the statement.
defmaxfactor (num): Count=num/2 whileCount>1: ifnum%count==0:Print 'largest Factor of%d is%d'%(Num,count) BreakCount-=1Else: PrintNum'is Prime' forEachnuminchRange (10,21): Maxfactor (Eachnum)
Python Learning note Five-conditions and loops