Python syntax two

First, the conditional statement

By determining one or more statements determines whether to perform code block

1, if the basic form of the statement:

determining if the condition:
    statement block

E.g:

score=75
if score>=60:
    print "passed"

2, if-else statements basic forms:

determining if Condition 1:
    block 1
the else:
    block 2

E.g:

= 55 Score
IF Score> = 60:
    Print "passed"
the else
    Print "failed"

. 3, the else-IF-statement elif basic form

determining if Condition 1:
    block 1
elif judgment conditions 2:
    code block 2
elif determination condition 3:
    block 3
the else:
    block 4

E.g:

score=85
if score>=90:
    print "very good"
elif score>=80:
    print "good"
elif score>=60:
    print "passed"
else:
    print "failed"

Note: Python does not support the switch statement, a number of conditions to determine, can only be achieved with elif statement

Second, the loop

Loops allow execution of a statement or group of statements repeatedly

1, for the basic form of loop

Cycling conditions for:
    block

E.g:

= L [ 'Adam', 'Lisa', 'Bart']
for L in name:
    Print name

2, the basic form of the while loop

while cycling conditions:
    code block

, for example:

sum=0
x=1
while x<100:
    sum=sum+x
    x=x+2
    print x

print sum

3, break exits the loop

break directly in the loop to exit the loop

For example: Calculated + 1 + 2 + 4 + 8 + 16 ... 10 and the front

sum=0
x=1
n=1
while True:
    sum=sum+x
    x=x*2
    n=n+1
    if n>10:
        break
    

print sum

4, continue to exit this cycle, the next cycle into the

For example: calculated odd and within 0-100

sum=0
x=0
while True:
    x=x+1
    if x>10:
        break
    if x%2==0:
        continue
    sum=sum+x
print sum

 

Guess you like

Origin www.cnblogs.com/testerlina/p/11069232.html