1. If statement1.1 Description
The IF statement is primarily used to react differently to changes in ambient conditions (i.e., expession) (i.e. execution code)
1.2 Syntax 1.2.1 Single branch structure
If single branch single condition:
if expression:expr_true_suite Note: expession is a true code execution Expr_true_suite
Example:
' Test ' = input ("input_name:")if name = = Input_name : Print ("right! ")
View Code
If single branch multi-condition:
if and expression2: # and OR or on both sides of the condition Expr_true_suite
Note: Expession is a true code execution Expr_true_suite
Example:
' Test ' = = = Input ("input_name:" = Int (input ("Input_ Age:"))if name = = Input_name and age = = Input_age: Print("name and is right! ")
View Code
If+else:
if expression: expr_true_suite Else: expr_false_suite
Example:
' Test ' = input ("input_name:")if name = = Input_name : Print ("name is right! " )else: print("name is error")
View Code
1.2.2 Multi-branch structure
If multi-branch structure:
if expession1: expr1_true_suiteelif expression2: expr2_true_suiteelif expession3: expr3_true_suiteElse: none_of_the_above_suite
Example:
Name ='Test' Age= 22Input_name= Input ("Input_name:") Input_age= Int (Input ("Input_age:") )ifName!=input_name andAge! =Input_age:Print("name and age all error!")elifName = = Input_name andAge! =Input_age:Print("name right and age error")elifName! = Input_name andAge = =Input_age:Print("Age right and name error")Else: Print(" All right")View Code
User login:
#Import Getpass #密码加密Name = input ("Please input names:")#pwd = getpass.getpass ("Please input password:")PWD = input ("Please input password")ifName = ="Chunwei" andPWD = ="Hello": Print("Welcome to here!")Else: Print("name or pwd error")
Guess Age:
Self_age = 22 Age= Int (Input ("Age :"))ifAge = =Self_age:Print(" You is right")elifSelf_age >Age :Print("try bigger")Else: Print("Try Small")
1.3 If Statement summary
1)ifa post-expression return value of true then executes its child code block, and then this if statement to this end, otherwise into the next branch judgment, until one of the branches is satisfied, after executing the IF2) expression can introduce operators: not, and,or, is, is not3multiple expression for enhanced readability most useful parentheses contain4a consistent representation of if and else indentation level is a pair5) Elif and else are optional6an If judge has at most one else but can have multiple elif7else represents the end of if judgment8) Expession can be an expression that returns a Boolean value (example X>1,x is notNone) Form, but also a single standard object (example X=1;ifX:Print('OK'))9) All standard objects are available for Boolean testing and can be compared between objects of the same type. Each object inherently has a Boolean True or False value. A null object, any number with a value of zero, or a Boolean value of None for the null object is False.
2. While Loop2.1 Description
The essence of the while loop is to allow the computer to repeat the same thing (that is, the while loop is a conditional loop, which includes: 1. Condition count cycle, 2 conditional infinite loop)
This condition means: conditional expression
The same thing means: The block of code that the while loop body contains
Repeat things such as: from 1 to 10000, ask for all the odd numbers within 1-10000, the service waits for a connection
2.2 Syntax
while expression: suite_to_repeat annotations: Repeat suite_to_repeat until expression is no longer true
2.2.1 Count Cycle
Count = 0 while (Count <5): print ("", Count) #print ("The loop is%s"%count) # #两种写法效果一样 # # count+=1
2.2.2 Infinite loop (dead loop)
Count = 0 while True: print ("theloopis" , Count) Count+=1
2.2.3 While with Break,continue,else
Break jumps out of this layer loop:
count=0 while (Count < 5): count+=1 if count = =3 : Print(" jump out of this layer, i.e. completely end this layer while loop") break print(' ', count)
Continue jump out of this cycle:
count=0 while (Count < 5): count+=1 if count = =3 :# print (' Jump out of this layer, i.e. completely end this one/layer while loop ') #break print(" jump out of this loop, that is, after the loop continue code is no longer executed, into the next loop ") continue Print (' , Count)
else with
Count=0 while(Count < 6): Count+=1ifCount = = 3: Print('jump out of this loop, that is, after the loop continue code is no longer executed, into the next loop') Continue Print('The loop is%s'%count)Else: Print('loop is not interrupted by break, that is, the end of the normal, the other code block will be executed')
Guess Age Optimization:
Enter three times not to exit
Count =0self_age= 22 whileCount <3: Guess_age= Int (Input ("Guess_age:")) #if Guess_age.isdigit (): #guess_age = = Int (guess_age) #Else: #Continue ifGuess_age = =Self_age:Print("You are right!!!! ") Break elifGuess_age <Self_age:Print("Try bigger!!! ") Else: Print("Try small!!!! ") Count+=1Else: Print("try too many is error! Byebye ....")
2.3 While summary
1) The condition is true to repeat the code, until the condition is no longer true, and if the condition is true, only executes once the code ends 2) While there are counting loops and infinite loops two, infinite loop can be used for a service of the main program has been waiting to be connected state 3 Break represents jumping out of this layer loop, continue represents a jump out of this loop 4) While loop ends without breaking breaks, the else code is executed
3. For loop
3.1 Description
The For loop provides the most powerful loop structure in Python (the For loop is an iterative loop mechanism, while the while loop is a conditional loop, and the iteration repeats the same logical operation, and each operation is based on the last result)
3.2 syntax
3.2.1 Basic Syntax
for inch iterable: suite_to_repeat Annotations: Each loop, the Iter_var iteration variable is set as the current element of an iterative object (sequence, iterator, or other object that supports iterations), provided to Suite_to_repeat The statement block is used.
Example 1:
for in range: print("loop is:" , i)
Example 2:
for in range (5): print("------", i) for in range: Print(J )if J > 3: Break
Python Basics 2