Note directory
-Code block
Start with a colon and use indentation to divide the same scope, called a code block, a code block as a whole, or a file as a code block
Scope: The area of action
Other languages use {} to divide scopes, while python uses code blocks
python code blocks can improve overall integrity and development efficiency
# python if 5 == 5: print(1) print(2) if True: print(3) print(4) if False: print(5) print(6) print(7)
# php c js if(False){ print(5) print(6) }
Note: Either use all 4 spaces or all 1 indent so that the same scope cannot be mixed up
Process Control
- Definition of process control
Process: The process of code execution
Process Control: Manage the process of code execution - Structure of process control
Sequential structure: code is executed top-down by default
Branch structure: subdivided as follows
Loop structure: while...for...
Branch structure
Keyword: if elif else
- Types of branching structures
- Single Branch
- Binomial Branch
- Multiple Branches
- Nested Branch
- Characteristics of Branch Structure
Single Branch
if Conditional expression: code1 code2
If the conditional expression is true, return True to execute the following code block,
If the conditional expression is not valid, return False without executing the following code block
zhangyaowen = "Handsome guy" if zhangyaowen == "Handsome guy": print("Please have a kebab") print("Have a beer") print("Please eat a large waist")
Binomial Branch
if Conditional expression: code1 code2 else: code3 code4
If the conditional expression is valid, execute the code block below if
If the conditional expression is not valid, execute the code block below else
Code blocks in if can be called true intervals
Code blocks in else can be called pseudo-intervals
zhangyaowen = "Beast" if zhangyaowen == "Blind Flow": print("Hit him") print("Pinch him") print("hung up,Feed him chili water") else: print("Coax him") print("Love him") print("Defraud him of the money in his pocket") # Input waits for user input string res = input("Please enter your name:") print(res, type(res))
Exercise: Prompt user for username and password:
If the user name is admin and the password is 000,
Prompt users to congratulate you on your successful landing
Otherwise prompt for username or password error
username = input('please enter your username:') pswd = input('please enter your password:') if username == 'admin' and pswd == '000': print('Landing Success!') else: print('Wrong login or password entry,Logon Failure!')
Multiple Branches
f Conditional expression 1: code1 code2 elif Conditional expression 2: code3 code4 elif Conditional expression 3: code5 code6 ... else: code7
If conditional expression 1 is true, execute the code block in if if if it is not
Judge down one by one to see if conditional expression 2 is valid, then execute the corresponding code block.
Conversely, continue to judge conditional expression 3 down, if the corresponding block of execution code is established.
Conversely, proceed down to determine if none of the conditions are met and execute the code block in else directly
youqian = False youfang = False youche = False youyanzhi = False youtili = False if youqian == True: print("I will marry him 1") elif youfang == True: print("I will marry him 2") elif youche == True: print("I will marry him 3") elif youyanzhi == True: print("I will marry him 4") elif youtili == True: print("I will marry him 5") else: print("Let's go, man,No. 2 car will catch up in a while 6")
Nested Branch
(Single Branch Binomial Branch Multiple Branches Nested with each other)
youqian = True youfang = True youche = True youyanzhi = False youtili = True if youqian == True: if youfang == True: if youche == True: if youyanzhi == True: if youtili == True: print("You are my favorite Langjun,1") else: print("Congratulations,My number 1 spare birth2") else: print("I have never had a relationship with an ugly person,No Fate 3") else: print("You're a great person Ah 4")
Cyclic structure
Keyword: while / for...in...
- Types of circular structure
- while loop
- for loop
- Characteristics of the cycle structure
while loop
Can improve the efficiency of code, reduce code redundancy
while Conditional expression: code1 code2
If the conditional expression is valid and returns True, the block of code in it is executed
Basic Syntax: Print 1 ~ 100
# (1) Initialization variable i i = 1 # (2) Write cyclic criteria while i <= 100: print(i) # (3) Self-increasing and self-decreasing conditions i += 1 """ #Code parsing: Initialize i = 1 first Then judge that 1<=100 satisfies the return True execution block Then print(1) i+=1 i = i+1 i=> 2 To go back to the conditional expression to make a judgment is to go back to 16 lines 2 <= 100 satisfies the return True execution block Then print(2) i+=1 i = i+1 2+1 i =>3 To go back to the conditional expression to make a judgment is to go back to 16 lines 3 <= 100 satisfies the return True execution block Then print(3) i+=1 i = i+1 3+1 i =>4 ... ... When do you jump out of the condition? When i = 101 101 <= 100 does not satisfy returning False without executing code block Loop termination... """
The sum of 1~100
# (1) First method i = 1 total = 0 while i <= 100: # Write Logic # print(i) total += i i += 1 print(total) ''' # Code parsing: total += i => total = total + i => 0 + 1 => 1 i+=1 => i = i+1 => 1+1 => 2 2 <= 100 Satisfy Return True True total += i => totoal = total + i => 0 + 1 + 2 => 3 i+=1 => i = i+1 => 2+1 => 3 3 <= 100 Satisfy Return True True total += i => totoal = total + i => 0 + 1 + 2 + 3 => 6 i+=1 => i = i+1 => 3+1 => 4 4 <= 100 Satisfy Return True True total += i => totoal = total + i => 0 + 1 + 2 + 3 + 4 + 5 + ...+ 100 => 5050 //When i = 101 101 <= 100 Not satisfied with cycle termination... '''
# (2) Dead-cycle writing # while True: # print(1) i = 1 flag = True total = 0 while flag: total += i i += 1 # Add a condition to jump out of if i == 101: flag = False print(total)
for loop
String-related operations
-
String Stitching
strvar1 = "I love you," strvar2 = "Dear Mushroom Cool" res = strvar1 + strvar2 print(res)
-
Repetition of strings
strvar1 = "Important things are to be repeated for 3 times" res = strvar1 * 3 print(res)
-
String splicing across lines
str1 = "sdfsssssssssssssssssssssssssssssssssssssssssssss" \ "sssdsfsdfasdf" \ "Your Speak Hand" print(str1)
-
Index of strings
# 0 1 2 3 4 5 strvar = "The road is bright" # -6-5-4-3-2-1 res = strvar[4] print(res) res = strvar[-2] print(res)