This article mainly introduces the organizational structure of Python programs. Interested partners can refer to it.
Organizational structure of the program
Sequential structure
The program executes the code from top to bottom without any judgment and jump until the end of the program.
Boolean value of the object
All Python objects have a Boolean value
Use the built-in function bool() to get the Boolean value of the object
You can directly put the Boolean value of the object in the conditional expression for judgment
The Boolean value of the following object is False
- False
- Value 0
- None
- Empty string
- Empty list
- Empty tuple
- Empty dictionary
- Empty set
The Boolean value of the object on is False, and the Boolean value of all other objects is True. Finally, if your time is not very tight and you want to improve python quickly, the most important thing is not afraid of hardship. I suggest you can build a micro platform ♥ Letter: 2121846671, that's really good. Many people make rapid progress. You need to be not afraid of hardship! You can add it and have a look~
Select structure
The program selectively executes part of the code according to the Boolean value of the judgment condition
Clearly let the computer know what to do under what conditions
Single branch structure
If
Syntax structure:
if Conditional expression: Conditional executor
Small experiment
money=1000 #Balance s=int(input('Please input withdrawal amount ')) #Withdrawal amount #Judge whether the balance is sufficient if money >= s: money=money - s print('Withdrawal succeeded, and the balance is:',money)
Double branch structure
Chinese semantics
If... Not satisfied... Just
Grammatical structure
if Conditional expression: Condition executor 1 else: Conditional actuator 2
Small experiment
num = int(input('Please enter an integer')) #Conditional judgment if num%2==0: print(num,'It's an even number') else: print(num,'It's an odd number')
Chinese semantics of multi branch structure:
Is the score above 90? no
Is the score between 80 and 90? no
Is the score between 70 and 80? no
Is the score between 60 and 70? no
Is the score below 69? yes
Syntax structure:
if Conditional expression: Conditional executor
Small experiment
money=1000 #Balance s=int(input('Please input withdrawal amount ')) #Withdrawal amount#Judge whether the balance is sufficient if money >= s: money=money - s print('Withdrawal succeeded, and the balance is:',money)
The unique writing method of python code
You can write python code mathematically
if 90<=a<=100: print('A')elif 80<=a<=89: print('B')elif 70<=a<=79: print('C')elif 60<=a<=69: print('D')elif 0<=a<=59: print('E')else:
Branching structure_ Use of nested if
Nested if
Syntax structure:
if Conditional expression 1: if Inner conditional expression: Internal condition actuator 1 else: Internal condition actuator 2 else: Conditional executor
Small experiment
'''member>= 200 8 fracture>= 100 9 fracture Non discount non members>=200 9.5 fracture No discount'''a=input('Are you a member? t/f') money=float(input('Please enter amount:')) #The outer organization structure determines whether it is a member if a =='t ': #member if money >=200: print('Give a 20% discount and the payment amount is',money*0.8) elif money >= 100: print('Give a 10% discount and the payment amount is', money*0.9) else: print('No discount',money)else: #Non member if money > = 200: print('Hit 9.5 Discount, payment amount is',money*0.95) else: print('No discount, payment amount is',money)
Conditional expression
Conditional expressions are short for if... else
Grammatical structure
x if judgment condition else y
Operation rules
If the Boolean value of the judgment condition is True, the return value of the condition expression is x; otherwise, the return value of the condition expression is False
# Enter two integers from the keyboard and compare the size of the two integers a=int(input('Please enter the first integer')) b=int(input('Please enter the second integer')) # Compare size '' 'general writing if a > b: print(a,'Greater than or equal to',b) else: print(a,'less than',b)''' #Compare using conditional expressions print( str(a)+'Greater than or equal to'+str(b) if a>=b else str(a)+'less than'+str(b))
Conditional expression: if the conditional judgment result is True, execute the content on the left; if the result is False, execute the content on the right
PASS statement
The statement does nothing, but is just a placeholder used where the syntax requires a statement
When to use:
First build the syntax structure, before you think about how to write the code
Which statements can be used:
- Conditional executor of if statement
- Loop body of for in statement
- Function body when defining a function
#pass statement, which does nothing, is just a placeholder used where statements need to be written a=input('Are you a member? t/f') #Judge whether you are a member if a = ='y ': passelse: Pass #It is used to occupy the position of the statement without thinking about how to write code, so that the program does not report errors
On the way of learning Python, we often encounter many problems, but the problem we have together is not a problem. We can find Nordic DA in Xiaobian and study together. We can also get learning dry goods by private letter "01". If you encounter any problems, you can ask Xiaobian DA in time.