I. Conditional statements
A python conditional statement determines the execution of a code block based on whether the result of execution of one or more statements is true or false.
Instead, the execution can be multiple lines, in which case the same range is expressed in the indentation.
1.Python judging the true and false form of the condition:
True: true, nonzero, non-null (that is, the list is not empty), not false, etc.
False: False, 0, empty list, empty dictionary, empty collection, non-true, etc.
2. Common Criteria for judging:
The judgment condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal).
When judging a condition as multiple values, you can use the following form:
The basic form of a conditional statement:
1 ifJudgment statement:2 Statement Block 13 elifJudgment statement:4 Statement Block 25 elifJudgment statement:6 Statement Block 37 Else:8 Statement Block 49 Ten #Note You can have multiple elif statements, and you cannot have more than one else statement
The order of execution of the conditional statement: The statement is executed by the top and bottom, the corresponding statement block executes, and the conditional statement is exited after execution is completed. As follows:
1_username ='Wallace Wang' #Define a variable2_password ='1234'3Username = input ('Username:')#Let the user enter the user name4 5 ImportGetpass#Python's built-in module, which is mentioned later in this article6Password = Getpass.getpass ('Passward:')#in the shell, it's typed as ciphertext.7 #password = input (' Password: ') # There's no way to use ciphertext8 if_username = = Username and_password = = Password:#if the condition after the if is set9 Print('Welcome User {name} login ...'. Format (Name=username))#executes the code after the If indentTen Else:#if not, execute the code behind the Else One Print('Invald username or password!')
two. While loop statement:
In Python programming, the while statement is used to loop the execution of the program, that is, under certain conditions, the loop executes the block program, after execution, again judge whether the condition conforms to, if conforming to continue to execute the statement block program, until encountering the break condition, or the condition does not conform to jump out of the loop statement. As a common order of execution:
Next, a dynamic instance is found on the web to demonstrate the while loop:
The operation is then interpreted in the form of code:
1>>numbers = [12,37,5,42,8,3] 2>>even = []3>>odd = []#Create a 6-length list and two empty lists4>> whileLen (numbers) >0:#when the length of the numbers list is greater than 0 o'clock, the condition is true and the following statement is executed5 6Number = Numbers.pop ()#The last element of the numbers is popped and assigned to number7 if(number% 2 = = 0):#determine if you can divide by 28Even.append (number)#can perform9 Else:TenOdd.append (number)#cannot be executed One A #executes the IF statement returns the length of the judgment numbers until the condition is not set to exit - ->>numbers#the values for the last 3 lists are the [] ->>even -[8,42,12] ->>Odd +[3,5,37]
As previously mentioned, there are two ways to exit the loop except that the statement does not meet the break and continue, respectively:
Break: Exits the loop directly.
Continue: Skips the current loop and enters the next loop.
#continue and break usageI= 1 whileI < 10: I+ = 1ifi%2 > 0:#Skip output when non-even Continue PrintI#output even 2, 4, 6, 8, tenJ= 1 while1:#the cycle condition of 1 must be set PrintJ#Output 1~10J + = 1ifJ > 10:#when I is greater than 10 o'clock jump out of the loop Break
Permanent Loop:
1 whileTrue:2a = Int (input ("Please enter your age:")3 ifA > 18:4 Print("already adult")5 Else:6 Print("Minors")7 8 #In general, permanent loops are combined with user input
Use of the Else statement in the loop:
In a loop, when the conditional statement of the while is not satisfied, the statement that follows else is executed, but a loop that exits by break does not execute the ELSE statement because break exits the entire loop, as follows:
1 #continue and break usage2 3i = 14 whileI < 10: 5i + = 16 ifi%2 > 0:#Skip output when non-even7 Continue8 Print(i)#output even 2, 4, 6, 8, ten9 Else:Ten Print("Exit Loop")#Output "Exit loop" after an even-numbered Onej = 1 A while1:#the cycle condition of 1 must be set - Print(j)#Output 1~10 -J + = 1 the ifJ > 10:#when I is greater than 10 o'clock jump out of the loop - Break - Else: - Print("Exit Loop")#does not output this statement because it is a break exit
The cycle of death, the inability to exit the loop, the damage to the computer is large:
1 while Ture: 2 Print (" dead Loop ") # execution of the statement will print infinitely
If you encounter a dead loop, you can exit the loop with CTRL + C.
three. For loop:
A For loop for python, typically used to traverse any sequence of items, such as a list or a string.
1 for<variable>inch<sequence>:#variable: Variable sequence: An iterative object such as LIST,TUPLE,STR, etc.2<statements>#Statements: statement block3 Else:4<statements>#This is the common for loop statement5 6 7 #The else here generally means that the for above execution condition is not immediate, execution after exiting, break exit does not execute
Here is a common example for analyzing a For loop:
1 forNinchRange (2, 10):#range generates a 2-10 list, but does not include2 forXinchRange (2, N):3 ifn% x = =0:4 Print(N,'equals'X'*', n//x)5 Break6 Else:7 #no element found in loop8 Print(N,'is prime') 9 Ten #This is a nested loop that is used to find 2-10 prime numbers One>> A2is prime -3is prime -4 Equals 2 * 2 the5is prime -6 equals 2 * 3 -7is prime -8 equals 2 * 4 +9 equals 3 * 3 -
Python full stack development Three Python basic conditions and loops