Code block (start with colon and divide the same code block with indentation. This whole is code block)
Scope: scope of scope
if 10 == 11: print(1) print(2) print(3) print(4)
#Indent: either all use / t Tab, or all use 4 spaces to express the same scope, which cannot be mixed together;
if 10 == 10: print(1) print(2)
Writing in other languages (understand)
if(10 == 10){ print(1) print(2) print(3) }
Process control
Process: the process of code execution
Control: control during code execution
Three structures:
(1) Sequential structure: the default code is executed from top to bottom
(2) Branch structure: single branch, bidirectional branch, multiple branches, nested branch
(3) Loop structure: while / for
Single branch
if conditional expression:
code1
code2
When the conditional expression holds, it returns True and executes the corresponding code block
zhiye = "programmer" if zhiye == "programmer": print("Get a high salary") print("More money,Few words,Die early") print("The amount of hair is becoming scarce")
Bidirectional branch (one out of two)
if conditional expression:
code1 ...
else:
code2...
If the conditional expression holds, it returns True and executes the code block of the if interval
If the conditional expression does not hold, return False and execute the code block of else interval
The code of if # branch is fast, which is also called town interval
The code block of else branch is also called false interval
zhiye = "Meituan takeout rider" zhiye = "lawyer" if zhiye == "Meituan takeout rider": print("Hit him") print("Scold him") print("Feed him chili water") else: print("Give you a red flower")
input waits for the user to enter a string (Note: the result must be a string)
name = input("Hello~ What's your mother's name") print(name,type(name))
Simulated website login
If admin = Sam, password: password = 111, login success will be displayed; otherwise, login failure will be displayed
admin = input("Please enter your account number:") password = input("Please enter your password:") if admin == "Sam" and password == "111": promt("Login successful") else: print("Login failed")
Multiple branches (one out of many)
"""
if conditional expression 1:
code1
elif conditional expression 2:
code2
elif conditional expression 3:
code3
else:
code4
If the conditional expression 1 is true, execute the corresponding branch code1, otherwise judge whether the conditional expression 2 is true
If conditional expression 2 is true, execute the corresponding branch code2, otherwise judge whether conditional expression 3 is true
If the conditional expression 3 is true, execute the corresponding branch code3. If it is not true, directly go to the ekse branch and the program is completed
elif can be 0 or more
else can be 0 or one
"""
youqian = False youfang = False youche = False if youqian == True: print("It shows that this person is very powerful") elif youfang == True: print("Can you make friends") elif youche == True: print("Drove the Yadi Emma transfer car,Let's touch it") else: print("You'd better be a meituan rider") print("<=======================>")
Nested branch
"" "single branch, two-way branch, nested combination of multiple branches" ""
youqian = True youfang = True youche = True youyanzhi = True youtili = False if youqian == True: if youfang == True: if youche == True: if youyanzhi == True: if youtili == True: print("I want to marry you~") else: print("You go eat some big kidney and come back~") else: print("You go to Thailand+the republic of korea,Plastic surgery") else: print("You're a good man~")
Little practice
#height
#Girls looking for someone
#Boys are between 1 meter and 1.5 meters. Xiaoqiang, where are you?
#Boys have no sense of security between 1.5 and 1.7 meters~
#Boys between 1.7 and 1.8 meters leave a phone call
#Boys are handsome between 1.8 and 2 meters. Do you suggest having another girlfriend
# General writing method height = float(input("Please enter your height:")) if 1 <= height and height < 1.5: print("Xiaoqiang, where are you?") elif 1.5 <= height and height < 1.7: print("No sense of security~") elif 1.7 <= height and height < 1.8: print("Handsome guy, leave a phone") elif 1.8 <= height and height < 2: print("Do you suggest having another girlfriend") else: print("i 'm sorry,There are no suitable options")
Shortcut key usage:
Indent right
shift + tab indent left
Cyclic structure
Features: reduce redundant code and improve execution efficiency
Syntax:
while conditional expression:
code1
(1) Initialize a variable
(2) Write the condition of the loop
(3) Self increasing and self decreasing value
Print 1 ~ 100
# (1) Initialize a variable i = 1 # (2) Write the condition of the loop while i <= 100: # (4) Write the logic of the loop print(i) # (3) Self increasing and self decreasing value i += 1 # i = i + 1"""
Code parsing:
First cycle
i = 1 i<=100 Judge as true,Execution loop body print(1)
i += 1 i => 2
Second cycle
The code goes back to line 17,Condition determination again
i = 2 i<=100 Judge as true,Execution loop body print(2)
i += 1 i => 3
Third cycle
The code goes back to line 17,Condition determination again
i = 3 i<=100 Judge as true,Execution loop body print(3)
i += 1 i => 4
....
and so on
until i = 101 i <= 100 Judged as false,Do not execute loop body,This is the end of the cycle...
1 ~ 100
"""
Sum of 100 ~ 1
# (1) Initialize a variable i = 1 total = 0 # (2) Write the condition of the loop while i <= 100 : # (4) Write custom logic total += i # (3) Self increasing and self decreasing value i += 1 print(total) """ Code parsing: First cycle i = 1 i <= 100 Judged to be true True Execution loop body total += i => total = total + i => 0 + 1 i += 1 => i = 2 Second cycle i = 2 i <= 100 Judged to be true True Execution loop body total += i => total = total + i => 0 + 1 + 2 i += 1 => i = 3 Third cycle i = 3 i <= 100 Judged to be true True Execution loop body total += i => total = total + i => 0 + 1 + 2 + 3 i += 1 => i = 4 ... And so on When i = 101 101 <= 100 Judged as false False Do not execute loop body,Here we are,End of cycle.. total += i => total + i => 0 + 1 + 2 + 3 + 4 + .... + 100 => 5050 """
Dead cycle
"""
while True:
print(1)
"""
Using the method of dead cycle to realize the accumulation and sum of 1 ~ 100
i = 1 total = 0 sign = True while sign: total += i i+=1 # Judge whether i is added to 101 and does not participate in the cycle if i == 101: # Terminate cycle sign = False print(total) #1 ~ 100 = 5050
One way circular exercise
(1) Print a line with a little star * help(print)
#help view the documentation of a method
help(print)
""" # print("*",end='') # print("*",end='') # print("*",end='') # print("*",end='') # print("*",end='') # print("*",end='') # print("*",end='') # print("*",end='') # print("*",end='') # print("*",end='') """
If the above method is not only a lot of code, but also too stupid, let's use the while loop
i = 0 while i<10: # End = 'when printing, there is no line break at the end by default print("*",end='') i += 1 # Default Wrap # print()
(2) By printing a variable, show a row of ten small stars
print("<======>") i = 0 strvar = "" while i < 10: # Write the logic of the loop strvar += "*" # strvar = strvar + "*" i +=1 print(strvar) process: """ strvar += "*" => strvar = "*" strvar += "*" => strvar = "*" + "*" = "**" strvar += "*" => strvar = "**" + "*" = "***" ... strvar += "*" => strvar = "********" + "*" = "*********" """
(3) A row of ten stars with different colors ★★★★★★★★★★★★
Method 1:
i = 0 while i < 5: print("★☆",end="") i+=1
Method 2:
i = 0 while i < 10: if i % 2 == 0 : print("★",end="") else: print("☆",end="") i+=1
Method 3:
i = 0 strvar = "" while i < 10: if i % 2 == 0 : strvar += "★" else: strvar += "☆" i+=1 print(strvar)
***Formula: any number and N are used for remainder. The range of remainder: 0 ~ (n-1)***
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
Dividend% 2 = > 0 or 1
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
7 % 5 = 2
Dividend% 5 = > 0 or 1,2,3,4
(4) Print ten rows and ten columns of small stars in one cycle
"""
★★★★★★★★★★
★★★★★★★★★★
★★★★★★★★★★
★★★★★★★★★★
★★★★★★★★★★
★★★★★★★★★★
★★★★★★★★★★
★★★★★★★★★★
★★★★★★★★★★
★★★★★★★★★★
"""
Method 1:
i = 0 while i < 100: # The logic is written here print("*" , end="") # Print line breaks (at 9 19 29.. 99) if i % 10 == 9: print() i += 1
"""
0123456789
**********
10111213141516171819
**********
20212223242526272829
**********
...
90919293949596979899
**********
9 19 29 39 49 59 69 79 89 99
9 % 10 = 9
19 % 10 = 9
29 % 10 = 9
...
99 % 10 = 9
"""
Method 2:
i = 1 while i <= 100: # The logic is written here print("*" , end="") # Print line breaks (on 9 19 29.. 99) if i % 10 == 0: print() i += 1
"""
12345678910
**********
11121314151617181920
**********
21222324252627282930
**********
...
919293949596979899100
**********
10 20 30 ... 100
"""
(5) A cycle to achieve ten rows and ten columns, grid color change of small stars
"""
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
"""
i = 0 while i < 100: # (1) Print stars if i % 2 == 0 : print("★",end="") else: print("☆",end="") # (2) Print line breaks (on 9 19 29.. 99) if i % 10 == 9: print() i += 1
(6) A cycle to achieve ten rows and ten columns, interlaced color change of small stars
"""
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
"""
Formula: n identical numbers will appear at any number and N base plate
0 // 10 = 0 1 // 10 = 0 2 // 10 = 0 .. 9 // 10 = 0 0 ~ 9 // 10 = > 0 (10 identical 0) 10 // 10 = 1 11 // 10 = 1 12 // 10 = 1 ... 19 // 10 = 1 10 ~ 19 // 10 = > 1 (10 identical 1) .... and so on 20 ~ 29 // 10 = > 2 (10 identical 2) 30 ~ 39 // 10 = > 3 (10 identical 3) 40 ~ 49 // 10 = > 4 (10 identical 4) ... 90 ~ 99 // 10 = > 9 (10 identical 9) 0~ 100 Ten identical zeros will appear,1,2 , 3 ... 9 0 // 3 0 1 // 3 0 2 // 3 0 3 // 3 1 4 // 3 1 5 // 3 1 """
Method 1:
i = 0 while i < 100: # (1) Print stars if i // 10 % 2 == 0: print("★",end="") else: print("☆",end="") # (2) Print line breaks (on 9 19 29.. 99) if i % 10 == 9: print() i += 1
Method 2:
print("<=================>") i = 10 while i < 110: # Print stars num = int(str(i)[-2]) if num % 2 == 0 : print("★",end="") else: print("☆",end="") # Print wrap if i % 10 == 9: print() i+=1 #Resolution: """ 10 ~ 100 101 102 103 110... 10 ~ 19 => 1 20 ~ 29 => 2 30 ~ 39 => 3 90 ~ 99 => 9 100 ~ 109 => 0 """