Statements and expressions
Code format: PEP8 indented four spaces no more than 79 characters in a row
A statement
1, assignment statement: example: s = 'Python'
2, tuple / list assignment
3, sequence assignment
Sequence assignment, when the variable does not match with the character of error. This time we need to use greedy matching [* b] preceded by a star.
Second, the expression
1, the function call statement:
For example, there is a square root function math module. After importing the call is called a function call
2, the method call
3, literals
4、print(‘hello’)
if conditional statement
Block of code is determined by one or more of the execution result of the statement (True or False).
= the INPUT name ( ' Please enter your user name: ' ) IF name == " ADMIN " : Print ( " super administrator " ) elif name == " the User " : Print ( " average user " ) elif name == " the Guest " : Print ( " guest " ) the else : Print ( " do not know you ." )
Behind each condition where a colon (:), indicating that the following condition is satisfied after the statement block to be executed.
Use indentation divided statement blocks, the number of statements in the same indentation together to form a block.
No switch in Python - case statement.
if the operator common operation
Operators | description |
---|---|
< |
Smaller than |
<= |
less than or equal to |
> |
more than the |
>= |
greater than or equal to |
== |
Equal, compare objects for equality |
!= |
not equal to |
if nested
In the nested if statements, can if ... elif ... else in another configuration if ... elif ... else structure.
Statement if expression 2: Statement elif expression 3: Statement else Statement elif expression 4: Statement else: statement
while loop
We meet the conditions (conditional statement returns True) then loop a section of code execution
= 0 COUNT the while COUNT <5: Print (COUNT, "less than 5") COUNT = COUNT +. 1 the else: Print (COUNT, "greater than or equal to 5")
for loop
Frequently used in a sequence of objects (such as lists, tuples etc.) to traverse
The general format for the following cycle:
for i in range(5): print(i)
break, continue and cyclic else Clauses
break statement can jump out of the loop for a while and. If you break out or for while loop, any corresponding loop else block will not be executed
for letter in 'Runoob': # first instance IF Letter == 'B': BREAK Print ( 'current letters:', Letter) var # 10 = second instance of the while var> 0: Print ( 'profit variable value: ', var) var var = -1 IF == var. 5: BREAK Print () "Good BYE!"
continue statement is used to tell Python skip the remaining statements in the current loop block, and then proceed with the next cycle.
for letter in 'Runoob': # first instance if letter == 'o': # o is the output alphabet skipped Continue Print ( 'the current letter:', Letter) var # 10 = second instance while var> 0: var var = -1 IF == var. 5: 5 # skip variable output Continue Print ( 'the current variable values:', var) ( "! Good BYE") Print
Loop else clause can have it in an exhaustive list (with a for loop) or the condition becomes false (with while loop) cause to be executed when the loop terminates, but does not perform the loop is terminated break.
for n- in Range (2, 10 ): for X in Range (2 , n-): IF n-% X == 0: Print (n-, ' equal ' , X, ' * ' , // n- X) BREAK the else : # loop elements not found Print (n-, ' is a prime ' )
Note:
break
Statement to exit the loop cycle, and continue
statements can be ended prematurely round cycle, and directly start the next cycle. These two statements are usually must cooperate if
statements.
Sometimes there are problems if the code is written, make the program into a "death cycle", that is, loop forever. Available Ctrl+C
exit the program, or to force the end of the Python process.