<> Program structure

* Three structures of program
* order
* loop
* branch
<> Branching structure

*
Basic grammar of branching structure
if Conditional expression Statement one Statement 2 Sentence 3 ......
*
A conditional expression is an expression that evaluates to a Boolean value

*
The colon after the expression cannot be less

*
be careful if Statements that follow , If it belongs to if Statement block , Indent the same level

*
The conditional expression results in True implement if The following indented statement block
# if Sentence connection # If you all give me money , I'll be rich a = " " # True or false of string : # Only empty strings are False, The rest are all True if a:
print(" I'll be rich ") print(" ha-ha ") print(" I still have to live ") I'll be rich ha-ha I still have to live # if Contact 2 age = 19 if
age> 16: print(" Drink ") print(" Next time you invite me ") Drink Next time you invite me
<> Bidirectional branching

*
if…else… expression

*
Grammatical structure :
if Conditional expression : Statement one Statement 2 ... else: Statement one Statement 2 ... # if Bidirectional branching # If you all give me money , I'll be rich a = " " #
True or false of string : # Only empty strings are False, The rest are all True if a: print(" I'll be rich ") print(" ha-ha ") else: print(
"emmmm") print(" Keep eating dirt ") print(" I still have to live ") I'll be rich ha-ha I still have to live # input The role of # 1.
Output the string in brackets on the screen # 2. Receives user input and returns to the program # 3. input The returned content must be of string type #
input Responsible for receiving user input and returning content to variables gender = input(" Please enter your gender ") # Print the input print(gender) if
gender== "man": print(" go , Smoking, drinking and shaving ") print(" Let's play together ") else: print(" What the hell are you ") print(
" I'm sorry , I am a boy ") Please enter your gender man man go , Smoking, drinking and shaving Let's play together # Judgment of examination results # Grades are entered by the user # 90 above : Excellent output #
80-90: good # 70-80: in # 60-70: flat # 60 following : output : I don't have a stupid student like you # Input grades , Need to use input function #
input All values entered are of string type score = input(" Please input the grade , It has to be a number ") # Solve the problem that the input is a string score = int(score)
if score >= 90: print(" You did very well in the exam , children ") if score >= 80 and score < 90: print(" be doomed ")
if score >= 70 and score <70: print(" OK ") if score >= 60 and score <70: print(
" That's it ") if score < 60: print(" Next time, take the exam ") Please input the grade , It has to be a number 1 Next time, take the exam
<> Multichannel branching

*
A lot of branches , It's called multichannel branching

if Conditional expression :
sentence 1

elif Conditional expression :
sentence 1

elif Conditional expression :
sentence 1

else:
sentence 1
… …

*
elif There can be many , According to the actual situation

*
else Optional

*
At most, multiple branches will perform only one case

<>if Sentence complement

* if Statements can be nested , Not recommended
* python No, switch sentence # score Keep students' grades # be careful input Return value type of score = input(" Please input student grade :")
# You need to str convert to int score = int(score) if score >= 90: print("A") elif score >= 80:
print("B") elif score >= 70: print("C") elif score >= 60: print("D") else: print
(" Let's go , I don't have a stupid student like you ") Please input student grade :90 A
<> Loop statement

* To perform a fixed action or task repeatedly
* classification
* for
* while
<>for loop

*
grammar
for variable in sequence : sentence 1 sentence 2 ... # for Circular case # such as [1,2,3,4,5,6,7] list_one = [1,2,3,4,5,
6,7] for num in list_one: print(num) print(num+100) print(num+1000) 1 101 1001
2 102 1002 3 103 1003 4 104 1004 5 105 1005 6 106 1006 7 107 1007 # Print student list names #
If it is jingjing, That must be my favorite # If it's another student , It's hard to say no to him stu_list = [' Wang Dayan ',' Li Meili ',' Wang Xiaojing '] for stu
in stu_list: if stu == " Wang Xiaojing ": print(" Xiaojing, where have you been ") else: print(" Sorry, classmate , Please forgive me ")
Sorry, classmate , Please forgive me Sorry, classmate , Please forgive me Xiaojing, where have you been
<>for-else sentence

* for At the end of the cycle , Sometimes it is necessary to carry out some finishing work , You need to use the else sentence
* else Statement is optional # for-else sentence # Print students in the list # If not in the list , Or the list is over , We need to print prompt statements , No more love
stu_list= [' Wang Dayan ',' Li Meili ',' Wang Xiaojing '] for stu in stu_list: if stu == " Wang Xiaojing ": print(
" Xiaojing, where have you been ") else: print(" Sorry, classmate , Please forgive me ") else: print(" No more love ") Sorry, classmate , Please forgive me
Sorry, classmate , Please forgive me Xiaojing, where have you been No more love
<>break,continue,pass

* break: Unconditionally end the entire cycle , It is referred to as sudden circulatory death
* continue: continue
* pass: It's just a space occupying symbol , It means doing nothing , There is no skip function # Determines whether a number queue contains numbers 7 #
Are you sure to include , Just find one to be sure , There's no need to keep looking , So use break dig_list = [3,4,6,7,54,3,23,2,4,7] for
digin dig_list: if dig == 7: print(" Ha ha ha , eureka ") break else: print(dig) 3 4 6
Ha ha ha , eureka # continue Sentence practice # In numbers 1-10 in , Find all even numbers , Print even numbers when found dig_list = [1,2,3,4,5,6,7,8,9
,10] ''' # continue case 1 for dig in dig_list: if dig % 2 == 0: print(dig)
print(" ha-ha , You are a double ") else: continue ''' # This code is equivalent to the above code for dig in dig_list: if dig %
2 == 1: continue print(dig) print(" ha-ha , You are a double ") 2 ha-ha , You are a double 4 ha-ha , You are a double 6 ha-ha , You are a double
8 ha-ha , You are a double 10 ha-ha , You are a double # pass case 1 age = 19 if age > 19: pass else: print(" You are still young ")
You are still young # pass case 2 ages = [2,23,43,54,65,2] for age in ages: pass print(age) 2 23 43
54 65 2
<>range function

* Generating ordered sequence
* Generating digital queues can be customized # range case 1 # Generate a 1 reach 100 The number sequence of # range The two numbers of the generated sequence are left included and right excluded dig_list
= range(1,101) for dig in dig_list: print(dig) #
In general, the python in , Even the numbers representing the range are left included and right excluded ,randint Function is a special case # range Function in python2.x and python3.x There are serious differences in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 # range3 # Print from 1 reach 9 The number of
for i in range(1,10): print(i) 1 2 3 4 5 6 7 8 9
<>while loop

*
A loop statement

*
When the condition is true , On the cycle , Suitable for not knowing the specific number of cycles , But it can be determined that if a certain condition is true, it will cycle

*
while grammar
while Conditional expression : Statement block # Another way of expression while Conditional expression : Block statement 1 else: Statement block 2 #
If the annual interest rate is 6.7%, Capital and profit are rolling every year , How many years will the principal double benqian = 10000 year = 0 # The number of years of deposit that need to be changed while
benqian< 20000: benqian = benqian * (1 + 0.067) year += 1 # year = year + 1
print(year) # Annual interest rate case 2 # In this case, the loop body is not executed while benqian < 20000: benqian = benqian * (
1 + 0.067) year += 1 # year = year + 1 else: print(year) 11 11

Technology
©2020 ioDraw All rights reserved
error: (-215:Assertion failed) Solution python Simple record of network programming mac solve Enter passphrase for key Enter the password each time Huawei Hongmeng system learning notes 9- Ecological construction of developers Apple iPhone 12 Price leakage : The official highest price is close to 1 Ten thousand yuan Docker Import of containers and mirrors , export Cross analysis of data analysis Notes on core principles of reverse engineering ( One )——Hello World-1Python Student information management system ( Lite ) Ant old employees leave early and miss millions of wealth , Some people say they don't regret it