conditions, loops, and other statementsPrint and import Introduction
1. Use comma input: When using PRINT statement output, use "," between the text and the variable to divide the label.
>>> print ' Age: ', 24age:24
2. module Function Import
Import Math Sqrt_num = MATH.SQRT (4) from math import sqrt sqrt (4) #如引入多模块中都存在sqrt函数则如此引用会出现异常from Math import Ceil , asin,sqrt from Math import * sqrt (4) #如引入多模块中都存在sqrt函数则如此引用会出现异常 from math import sqrt as x_sqrt #设置别名 x_sq RT (4)
Assign value
1, Sequence unpacking: or called recursive unpacking, the number of worthy sequences are solved, and then put into the sequence of variables
>>> x, y, z = 1,2,4>>> print x,y,z1 2 4>>> values =
2, chain-assigned value
>>> x = y = {' person ': ' Good program '}>>> x{' person ': ' Good program '}>>> y{' person ': ' Good progr Am '}
3. Incremental assignment: For all types of integers, characters, etc.
>>> x = 3>>> x + = 2>>> x *= 3>>> x15
Statement block
A statement block is a set of statements that executes or executes multiple times (a loop statement) when the condition is true (a conditional statement). You can create a statement block by placing a space in front of the code to indent the statement.
In Python, a colon (:) is used to indicate the beginning of a statement block, and each statement in the block is indented (the same indent). When you fall back to the same indent as an already closed block, it means that the current block is closed.
Conditional and conditional statementsBoolean value
True, False. Objects are compared, so when different Boolean values are compared, although they are false, they are not equal
False when the following value is used as a Boolean expression, the interpreter is considered false. None 0 "" () [] {}
If statement
If condition statement:
EXECUTE statement
Elif Conditional Statements:
EXECUTE statement
else:
EXECUTE statement
Comparison operators for Python
An expression |
Describe |
X==y |
X equals y |
X<y |
x less than Y |
X>y |
|
X>=y |
|
X<=y |
|
X!=y |
|
X is Y |
X and Y are the same object |
X is not Y |
X and Y are different objects |
x [not] in Y |
X[NOT] is a member of the Y container |
Assertion
You can require certain conditions to be true, such as when checking the properties of a function parameter, or as a secondary condition during initial testing and debugging. Keyword is assert
>>> age = 10>>> Assert 0 < Age < 100>>> age = -1>>> Assert 0 < Age < 100Tr Aceback (most recent): File "<pyshell#82>", line 1, in <module> assert 0 < Age < 100Assert Ionerror
Cycle
While loop
x = 1while x < 10:print x x + = 1
For loop
words = [' hello ', ' python ', ' Java ']for word in Words:print Word
Break keyword jumps out of the loop
Continue keyword jumps out of the current loop
List-derived
A list derivation is a way to create a new list with other lists. It works like a for loop.
>>> [x*x for x in range] if x%3==0][0, 9, 3, 81]>>> [(x, Y) for x in range (3) for Y in range ()] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1);
Other key words
1, pass does not do anything, you can use as a placeholder
2, Del: Delete the unused object, used to delete the variable or part of the data structure, not to delete the value
3. Exec: Executes a string execution statement
4. Eval: Evaluates the expression of a string and returns the result
conditions, loops, and other statements--python the third of the Learning series