Conditions:
If condition:
Statement block
Elif
Statement block
Else
Statement block
Elif indicates else if
This is actually legal!!! 1 < x < 2!!!
[Python]View Plaincopy print?
- >>> If 1 < x < 2:
- print (' True ')
- True
and represents and
[Python]View Plaincopy print?
- >>> if x > 1 and x < 2:
- print (' True ')
- True
or represents or
[Python]View Plaincopy print?
- >>> x
- 2
- >>> if x = = 2 or x = = 3:
- print (x)
- 2
Returns a If B is true, otherwise returns C
A If B else c
[Python]View Plaincopy print?
- >>> ' True ' if 1 < x <2 else ' False '
- ' True '
While loop
While condition:
Statement block
No brackets required!
[Python]View Plaincopy print?
- >>> x
- 1.2
- >>> while x < 2:
- print (x)
- x + = 0.2
- 1.2
- 1.4
- 1.5999999999999999
- 1.7999999999999998
- 1.9999999999999998
- >>>
Often used:
[Python]View Plaincopy print?
- While True:
- ....
- If ...:
- Break
- ....
For loop
For something in XXXX:
Statement block
That is, for each element in XXXX, execute some block of statements, XXXX can be a list, a dictionary, a tuple, an iterator, and so on.
[Python]View Plain Copy print?
- >>> for x in range (0,10):
- print (x*x)
- &NBSP;&NBSP;
-
- 0&NBSP;&NBSP;
- 1&NBSP;&NBSP;
- 4&NBSP;&NBSP;
- 9&NBSP;&NBSP;
- 16&NBSP;&NBSP;
- 25&NBSP;&NBSP;
- 36&NBSP;&NBSP;
- 49&NBSP;&NBSP;
- 64&NBSP;&NBSP;
- 81&NBSP;&NBSP;
This is for: else ... Statement
Execute only without break, or, as long as you do not break, it will execute
[Python]View Plaincopy print?
- >>> for n in range (81,-1):
- root = sqrt (n)
- if root = = Int (root):
- print (n)
- Break
- Else
- print ("I didn ' t fint it")
- I didn ' t fint it
But you should use the list derivation as much as possible, as it is more convenient and clear
[Python]View Plaincopy print?
- >>> [x*x for x in range (1,5)]
- [1, 4, 9, + ]
- >>> [x**2 for x in range (1,Ten) if x 2 = =0] /c5>
- [4, +, + , + ]
- >>> [(x, y) for x in range (1,3) for y in range (4,6)]
- [(1, 4), (1, 5), (2, 4), (2, 5)]
Assert assert
The following statement is true, otherwise assertionerror appears
Used to check a condition, and if it is true, do nothing. If it is false, it throws a Asserterror and contains an error message.
For example:
py> x
=
23
py>
assert
x >
0
,
"x is not zero or negative"
py>
assert
x
%
2
=
=
0
,
"x is not an even number"
Traceback (most recent call last):
File
"", line
1
,
in
AssertionError: x
is
not
an even number
|
#常用在代码开头的注释
assert
target
in
(x, y, z)
if
target
=
=
x:
run_x_code()
elif
target
=
=
y:
run_y_code()
else
:
assert
target
=
=
z
run_z_code()
Pass
Pass means there's nothing here, no action done.
If your program has unfinished functions and classes and so on, you can add some comments first, then the Code section just write a pass, so the program can run without error, and later you can continue to refine your program
[Python]View Plaincopy print?
- >>> class Nothing:
- Pass
- >>>
Del
Del deletes only references and names, does not delete values, that is, Python automatically manages memory and is responsible for memory recycling, which is one reason why Python is running less efficiently.
[Python]View Plaincopy print?
- >>> x = [1,2,3]
- >>> y = x #x and y point to the same list
- >>> del x
- >>> x
- Traceback (most recent):
- File "<pyshell#41>", line 1, in <module>
- X
- Nameerror:name ' x ' is not defined
- >>> y
- [1, 2, 3]
Python 3 conditions, loops and assert, pass, Del