Python full stack development-Day2 Boolean, process control, loop, python-day2
Full-stack python development-Day2 Boolean Process Control LoopI. Boolean
1. Overview
# Boolean value, a value of True and a value of False # A computer is commonly known as a computer. That is, when we write a program to run a computer, the computer should be infinitely close to the human brain, or what the human brain can do, what Should computers do,
The main role of the human brain is data operation and logical operation. Here, the Boolean Type simulates the logical operation of a person. That is, when a condition is determined to be True, the human brain uses True to mark it. If the condition is not True, the human brain uses False to mark it.
1 a = 3 2 B = 5 3 4 a> B # False if not True, that is, False 5 # False 6 7 a <B # True is True, true 8 # True 9 10 # Next we can do different things based on the conditional results: 11 if a> B: 12 print (a is bigger than B) 13 14 else: 15 print (a is smaller than B) 16 # It means that a computer can perform different actions based on different results like a human brain.
2,Key knowledge of Boolean Type !!! :
# All data types contain boolean values 1, None, 0, and null (empty string, empty list, and empty dictionary). The boolean values are False2 and others are true.
3. Key Points:
#1. variable type: If the id remains unchanged, the value can be changed, which is called a variable type, such as list and dictionary #2. immutable type: Once the value changes, the id also changes, which is called an immutable type (id change, which means a new memory space is created)
2. if... else of Process Control
Since the purpose of programming is to control the computer to work like a human brain, what the human brain can do requires corresponding mechanisms in the program to simulate.
The human brain is nothing more than mathematical and logical operations. We have already said mathematical operations in the previous section. For logical operations, that is, people make different responses based on changes in external conditions, such:
1,If a woman is over 30 years old, she is named aunt.
Age_of_girl = 31if age_of_girl> 30: print ('Aunt hao ')
2,If a woman is more than 30 years old, she is called Aunt. Otherwise, she is called Miss.
Age_of_girl = 18if age_of_girl> 30: print ('Aunt hao') else: print ('Miss hao ')
3,If the age of a woman is greater than or equal to 18 and the age is <22 years old and the height is> 170 and the weight is <100 and beautiful, then: Confess, otherwise: called Aunt
Age_of_girl = 18 height = 171 weight = 99is_pretty = Trueif age_of_girl> = 18 and age_of_girl <22 and height> 170 and weight <100 and is_pretty = True: print ('Statement... ') else: print ('Aunt hao ')
4. If: score> = 90, then: Excellent
If the score is greater than or equal to 80 and less than 90, the score is good.
If the score is greater than or equal to 70 and less than 80, then: normal
Other cases: Poor
Score = input ('>:') score = int (score) if score> = 90: print ('excellent ') elif score> = 80: print ('good ') elif score> = 70: print ('normal ') else: print ('poor ')
If condition 1: indented code block elif condition 2: indented code block elif Condition 3: indented code block... else: indented code block
3. while loop of Process Control
1. Why loop?
# In the previous lesson, we have learned how to use if. else to guess the age of the game. However, the chance of only one guess is too small. What if I want to give players three chances?
That is, after the program is started, players can try three times at most. How can this problem be solved? You don't always want to copy the code three times ....
Age_of_oldboy = 48 guess = int (input (">>:") if guess> age_of_oldboy: print ("it's too big to guess, try it in a small space... ") elif guess <age_of_oldboy: print (" the guess is too small. Try it in the big picture... ") else: print (" Congratulations, you guessed it... ") # 2nd times guess = int (input (" >>: ") if guess> age_of_oldboy: print (" it's too big to guess, try it in a small space... ") elif guess <age_of_oldboy: print (" the guess is too small. Try it in the big picture... ") else: print (" Congratulations, you guessed it... ") # 3rd Times guess = int (input (" >>: ") if guess> age_of_oldboy: print (" it's too big to guess, try it in a small space. .. ") Elif guess <age_of_oldboy: print (" the guess is too small. Try it in the big picture... ") else: print (" Congratulations, you guessed it... ") # Even if you are a little white, do you think it is too low? In the future, you have to modify the function three times. Therefore, remember that writing duplicate code is the best behavior for programmers. So how can we make the program repeat a piece of code multiple times without writing duplicate code? Loop statements come in handy.
2. Conditional loop: while. Syntax:
While condition: # loop body # If the condition is true, the loop body is executed. After the execution is complete, the loop body repeats again and then judges the condition again... # If the condition is false, the loop body is not executed and the loop ends.
# Print 0-10count = 0 while count <= 10: print ('loop ', count) count + = 1 # print an even count between 0 and 10 = 0 while count <= 10: if count % 2 = 0: print ('loop ', count) count + = 1 # print the odd count between 0 and 10 = 0 while count <= 10: if count % 2 = 1: print ('loop ', count) count + = 1
3. endless loop
Import time # num = 0 while True: print ('Count', num) time. sleep (1) num + = 1
4. loop nesting and tag
Tag = True # define True while tag: ...... while tag: False # define False
# Exercise, the requirements are as follows: 1. cyclically verify that the user's user name and password have passed the authentication. 2. Run the user to repeatedly execute the command. 3. When the user enters the command quit, exit the entire program.
1 # Implementation 1: 2 name = 'egon' 3 password = '000000' 4 5 while True: 6 inp_name = input ('username: ') 7 inp_pwd = input ('password: ') 8 if inp_name = name and inp_pwd = password: 9 while True: 10 cmd = input (' >>:') 11 if not cmd: continue12 if cmd = 'quit': 13 break14 print ('run <% s> '% cmd) 15 else: 16 print ('user name or password error ') 17 continue18 break19 20 # implementation 2: Use tag21 name = 'egon' 22 password = '000000' 23 24 tag = True25 while tag: 26 inp_name = input ('user name :') 27 inp_pwd = input ('password: ') 28 if inp_name = name and inp_pwd = password: 29 while tag: 30 cmd = input (' >> :') 31 if not cmd: continue32 if cmd = 'quit': 33 tag = False34 continue35 print ('run <% s> '% cmd) 36 else: 37 print ('user name or password error ')
4. break and continue
# Break is used to exit this layer loop while True: print "123" break print "456" # continue is used to exit this loop and continue the next loop while True: print "123" continue print "456"
5. while + else
# Else is generally different from other languages except if. in Python, there is also a while... else statement. The else after while refers,
When the while Loop is executed normally and the break is not aborted in the middle, the statement count = 0 while count <= 5: count + = 1 print ("Loop ", count) else: print ("the loop is normal") print ("----- out of while loop ------") output Loop 1 Loop 2 Loop 3 Loop 4 Loop 5 Loop 6 the loop is executed normally. ----- out of while Loop ------ # If break is triggered during execution, the else statement is not executed. count = 0 while count <= 5: count + = 1 if count = 3: break print ("Loop", count) else: print ("the loop is normal") print ("----- out of while Loop ------") Output Loop 1 loop 2 ----- out of while loop ------
Iv. Process Control-for Loop
1. Iterative loop: for. Syntax:
For I in range (10 ):
Indented code block
2 break and continue (same as above)
3. loop nesting
# Print the 9-9 multiplication table for I in range (): for j in range (1, I + 1): print ('% s * % s = % s' % (I, j, I * j), end = '') # print has a default line feed. Here we change the default line feed to an empty string print ()
Analyze how to print the pyramid ''' # max_level = 5 * # current_level = 1, number of spaces = 4, number of * = 1 *** # current_level = 2, number of spaces = 3, * Number = 3 ***** # current_level = 3, number of spaces = 2, number of * = 5 ******* # current_level = 4, number of spaces = 1, * Number = 7 ********* # current_level = 5, number of spaces = 0, * Number = 9 # mathematical expression space number = max_level-current_level * number = 2 * current_level-1 ''' # max_level = 5for current_level in range (1, max_level + 1 ): for I in range (max_level-current_level): print ('', end ='') # print multiple spaces consecutively in a row for j in range (2 * current_level-1 ): print ('*', end = '') # print multiple spaces consecutively in a row. print ()