This is the 9 days I participated in the November Gwen Challenge. For details, see: The last Gwen Challenge 2021.
Python code follows the following basic principles during execution:
- Ordinary statements, executed directly;
- When a function is encountered, it is loaded into memory and not executed directly
- When it encounters a class, it executes a normal statement inside the class, but the class’s methods are only loaded, not executed
- encounter
if
,for
And other control statements, according to the corresponding control process - encounter
@
.break
.continue
Etc., according to the specified grammar - When encountering a function, method call, etc., the internal code of the function is executed instead, and the original sequential code is continued after execution
conditional
A conditional is a block of code that is to be executed based on the execution result (True or False) of one or more statements.
In Python syntax, the if, elif, and else keywords are used for conditional judgment:
Statement_block_1 statement_block_1 elif condition2: Condition2: statement_block_2 statement_block_2 else: statement_block_3 statement_block_3Copy the code
Principle of use:
- Use a colon after each condition
:
As the end of the judgment line, it follows that the condition is met (the result isTrue
Block of statements to execute after). - In addition to
if
You have to have branches,elif
andelse
Branches can be omitted as appropriate. - Use indentation to divide statement blocks. Statements with the same indentation number are grouped together to form a statement block.
- Each branch is judged sequentially. If any branch is hit first and executed, all subsequent branches are ignored and skipped.
- in
Python
There is noThe switch case -
Statements.
Number = 20 print(" guess ") while True: guess = int(" input ") if == number: Break elif guess<number: print elif guess>number: printCopy the code
If /else statements can be nested, that is, if… elif… The else structure is placed in another if… elif… Else structure:
Var = 100 if var < 200: print() ) if var == 150: print(' this is 150') elif var == 100: print(' this is 100') elif var == 50: print(' this is 50') elif var == 50: Print (" Less than 50! ) else: print(" Can't determine the correct value!" ) print("Good bye!" )Copy the code
Cycle control
Loop control: the process of running a piece of code through a loop until a condition is met to exit the loop. Python uses the keywords for and while for loop control, but no other language does… While statement.
while
cycle
Find the sum between 1 and 100:
num_sum = 0 n = 1 end_num = 100 while n<=end_num: # num_sum = num_sum + n num_sum += n n += 1 print(" %d"%(end_num,num_sum))Copy the code
while
theelse
clause
The while loop can also add an else clause. When the while loop completes its normal execution, else statements are executed. Note the indentation of the else and while levels:
While I < number: print(I) I += 1 else: print(I += 1) )Copy the code
⚠️ Note: If the loop is prematurely terminated by a mechanism such as break, else statements are not executed:
Number = 10 I = 0 while I < number: print(I) I += 1 if I == 7: break else: print(" )Copy the code
for
cycle
Print out the multiplication table:
For I in range (1, 10) : for j in range (1, I + 1) : # print (I, j) print (" * {} {} = {} ". The format (j, I, * I) (j), end = ' ') print (" ")Copy the code
break
statements
If you want to exit the body of a while or for loop, you can use the break statement.
continue
statements
The continue statement, unlike the break statement, is used to skip the rest of the current loop body and start the next loop directly. It does not exit and terminate the loop.