1, the use of elif comparison of the province CPU:
The first method, using the IF
score = Int (input (' Please enter your score: '))
if (score <=) and (Score >= 90):
Print (' A ')
if (score <=) and (Score >= 80):
Print ("B")
if (score <=) and (score >= 70):
Print ("C")
if (score <=) and (score >= 60):
Print ("D")
if (score <=) or (score >=101):
Print ("Input Error! ")
The second method, using Elif
score = Int (input (' Please enter your score: '))
if (score <=) and (Score >= 90):
Print (' A ')
Elif (Score <=) and (Score >= 80):
Print ("B")
Elif (Score <=) and (score >= 70):
Print ("C")
Elif (Score <=) and (score >= 60):
Print ("D")
else:
Print ("Input Error! ")
Use Elif to save CPU more than if
2, assert: When the condition behind this keyword is false, the program automatically crashes and throws Assertionerror exception
In order to prevent the programmer from finding the following exception, so in front of the judgment when the problem of the first stop with him
Later it will say: How to capture this anomaly and handle
3, a small trick:
1, how to let X, Y, Z This three value fast interchange
X, y, z = y,z,x
2. Membership Operator: in
Xname= "Beijing Tian ' an gate"
' Door ' in XName #返回TRUE
4, ternary operator: if use method
X, y, z = 6, 5, 4 #这个程序可以变为:
If x < Y:small = x if (x < y and x < Z) Else (y if y < Z else z)
small = X
If z < small:
small = Z
Elif y < z:
small = y
Else
small = Z
5. While
Usage: While condition:
The loop body #直到条件为假 Otherwise has been circulating like condition 1 = 1 This is the endless loop
6. For
Usage: for-target in expression:
Loop body
Give me a chestnut.
name = ' Xiaowangba '
For I in Name:
Print I
Give me another chestnut.
name = [' Xiaowangba ', ' 123 ', ' 456 ', ' We are Coming ']
For each in name:
Print (Each,len (each))
7, Range (): Built-in function BIM generates a sequence of numbers that can be used with the for
Range (5) #生成从0到4这五个数
Range (1,10) #生成从0到10这是个数
Range (1,10,2) #范围从0到10 interval is 2
8, break: Can terminate the current cycle, no longer cycle down, immediately jump out of the loop, execute the next statement outside the loop
9, continue: Terminate the cycle, and start the next round cycle
Getting started with Python--4--and loops