Computers can do a lot of automated tasks, because it can make their own conditions to judge.
For example, to enter user age, print different content according to age, in a Python program, you can use the if statement :
- if - : print ('your is,'+str ') print (' Adult') print ('END')
Note: The indentation rules for Python code. Code with the same indentation is treated as a block of code, with 3, 4 lines of print statements constituting a block of code (but excluding print on line 5th). If the IF statement evaluates to True, the code block is executed.
Indent please strictly follow Python's customary notation: 4 spaces, do not use tab, do not Mix tab and space, otherwise it is easy to cause syntax errors due to indentation.
NOTE: The If statement is followed by an expression, and then with: Represents the code block to begin with.
Ages=input ('Please input your age :') Age=int(Ages)ifAge >= -: Print ('Adult')elifAge >=6: Print ('teenager')elifAge >=3: Print ('Kid')Else: Print ('Baby')
A list or tuple can represent an ordered set. What if we want to access each element of a list in turn?
The python for loop can then iterate over each element of the list or tuple in turn:
L = ['Adam'Lisa'Bart' ] for in L: if3. PY Adamlisabart
In Python, for ... else means that the statement in for is no different from normal, while the statement in else is executed when the loop is executed normally (that is, for not breaking out by break), while ... else is the same.
In Python programming, a while statement is used to loop the execution of a program, which, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is:
While judging condition:
EXECUTE statement ...
The execution statement can be a single statement or a block of statements. The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true. The loop ends when the condition false false is judged.
Count =0sum=0 while(Count <9): Print ('The count is:'+Str (count))sum=sum+Count Count= Count +1Print ('sum is:'+ STR (sum)) Print ('Good bye!') Results: [[email protected]if]$ Python34. Py The count is:0The count is:1The count is:2The count is:3The count is:4The count is:5The count is:6The count is:7The count is:8sumis: $Good bye!
The break statement for Python exits the loop.
sum 0 1 while True: sumsum + x 1 if: breakprint (sumif5 5050
During the loop, you can use break to exit the current loop, and you can use continue to skip the subsequent loop code and continue the next loop.
L = [ the,98, -,Bayi, the, +, the, -]sum=0.0N=0 forXinchL:sum=sum+x N= n +1Print (sum/N) Result: [[email protected]if]$ Python36. PY72.0
Now the teacher just want to count the average of passing scores, it is necessary to cut off the score of x < 60, then, using continue, can be done when x < 60, do not continue to execute the loop body follow-up code, directly into the next cycle:
L = [ the,98, -,Bayi, the, +, the, -]sum=0.0N=0 forXinchL:ifX < -: Continuesum=sum+x N= n +1Print (sum/N) Result: [[email protected]if]$ Python37. PY79.0
Python also has multiple loops
forXinch['A','B','C']: forYinch['1','2','3']: print (x+y) Results:
x Cycles once,y loops 3 times, so we can print out a full array: [[Email protected]if]$ Python38. PY a1a2a3b1b2b3c1c2c3
The judgment and circulation of Python