python full Stack development-day2 Boolean Process Control loop
A, Boolean
1. Overview
#布尔值, a true one false #计算机俗称电脑, that is, when we write a program to let the computer running, it should be to let the computers infinitely close to the human brain, or what the human brain can do, the computer should be able to do,
The main function of the human brain is data operation and logical operation, where the Boolean type of simulation of the logic of human operation, that is, when a condition is established, with a true identity, is not established with false identity
1A=32B=53 4A > B#not set up is false, that is false5 #False6 7A < b#The establishment is true, that is true8 #True9 Ten #then you can do different things according to the result of the condition: One ifA >B: A Print(A isbigger than B) - - Else: the Print(A issmaller than B) - #The above means that the computer can perform different actions as the human brain does, depending on the outcome of the judgment.
2, the key knowledge of Boolean type!!! :
#所有数据类型都自带布尔值1, none,0, empty (empty string, empty list, empty dictionary, etc.) three cases Boolean value is False2 and the rest is true
3. Key points :
#1. mutable type: In the case of an ID invariant, value can be changed, then called a mutable type, such as a list, dictionary # #. Immutable type: Once the value changes, the ID also changes, which is called the immutable type (ID variable, which means that a new memory space is created)
ii. If...else of Process Control
Since the purpose of our programming is to control the ability of a computer to work like a human brain, what the human brain can do is that it needs to have mechanisms in the process to simulate it.
The human brain is nothing more than a mathematical and logical operation, as we have said in the previous section on mathematical operations. For logical operations, people make different responses based on changes in external conditions, such as:
1, if: Woman's age >30 years old, then: Call Aunt
Age_of_girl=31if age_of_girl >: print (' Auntie Good ')
2, if: Woman's age >30 years old, then: Call aunt, otherwise: called Miss
Age_of_girl=18if age_of_girl >: print (' Auntie Good ') Else: print (' Miss Good ')
3, if: Woman's age >=18 and <22 years old and height >170 and weight <100 and is beautiful, then: vindicate, otherwise: call Aunt
Age_of_girl=18height=171weight=99is_pretty=trueif Age_of_girl >= and Age_of_girl < and height > Wei Ght < Is_pretty = = True: print (' confession ... ') Else: print (' Auntie Good ')
4, if: Score >=90, then: excellent
If the score >=80 and <90, then: good
If the scores are >=70 and <80, then: normal
Other situation: very poor
Score=input (' >>: ') Score=int (score) if score >=: print (' excellent ') Elif score >=: print (' good ') elif Score >=: print (' normal ') Else: print (' very poor ')
If condition 1: Indented code block elif Condition 2: Indented code block elif Condition 3: Indented code block ... else: indented block of code
third, the process control while cycle
1, why to use the cycle
#上节课我们已经学会用if: Else to guess the age of the game, but only to guess the odds are too small, if I want to give the player 3 chance?
Is the program starts, the player can try up to 3 times, this How to do? You don't want to copy the code 3 times ....
Age_of_oldboy = 48guess = Int (input (">>:")) if guess > Age_of_oldboy: print ("Guess too big, try it in the small ...") elif guess < age_of_oldboy: print ("Guess too small, try to go to Dali ...") Else: print ("Congratulations, guess right ...") #第2次guess = Int (input (">>:")) if Guess > Age_of_oldboy: print ("Guess too big, try it in the small ...") Elif guess < age_of_oldboy: print ("Guess too small, try it on the Dali ...") Else : Print ("Congratulations, guess right ...") #第3次guess = Int (input (">>:")) if guess > Age_of_oldboy: print ("Guess too big, try it in the small ...") Elif Guess < age_of_oldboy: print ("Guess too small, try the Dali ...") Else: print ("Congratulations, guess right ...") #即使是小白的你, too low, isn't it, You will have to modify the functionality 3 times later, so remember that writing repetitive code is the least shameful behavior of the programmer. So how do you do not have to write duplicate code and let the program repeat a piece of code multiple times? The loop statement comes in handy.
2, Conditional loop: While, the syntax is as follows
While condition: # loop Body # If the condition is true, then the loop body is executed, the execution is completed again after the cycle, re-judge the condition ... # If the condition is false, then the loop body does not execute and the loop terminates
#打印0 -10count=0while count <=: print (' Loop ', count) count+=1# prints an even number between 0-10 Count=0while Count <= 10: if count%2 = = 0: print (' Loop ', count) count+=1# prints an odd number between 0-10 count=0while count <=: if count%2 = = 1: print (' Loop ', count) count+=1
3. Dead Cycle
Import Time #调用模块后面会讲num =0while True: print (' count ', num) time.sleep (1) num+=1
4. Loop nesting and tag
Tag=true #定义真 while tag: ... while tag: ... while Tag:tag=false #定义假-in-the-loop
#练习, the requirements are as follows: 1 Loop Verify user entered user name and password 2 authentication passed, run user repeatedly execute command 3 when user input command is quit, exit entire program
1 #Implement one:2Name='Egon'3password='123'4 5 whileTrue:6Inp_name=input ('User name:')7Inp_pwd=input ('Password:')8 ifInp_name = = Name andInp_pwd = =Password:9 whileTrue:TenCmd=input ('>>:') One if notCmd:Continue A ifcmd = ='quit': - Break - Print('Run <%s>'%cmd) the Else: - Print('incorrect user name or password') - Continue - Break + - #implementation two: using tag +Name='Egon' Apassword='123' at -tag=True - whiletag: -Inp_name=input ('User name:') -Inp_pwd=input ('Password:') - ifInp_name = = Name andInp_pwd = =Password: in whiletag: -Cmd=input ('>>:') to if notCmd:Continue + ifcmd = ='quit': -tag=False the Continue * Print('Run <%s>'%cmd) $ Else:Panax Notoginseng Print('incorrect user name or password')
4. Break and Continue
#Break to exit this layer loop whileTrue:Print "123" Break Print "456"#continue is used to exit this loop and continue the next cycle whileTrue:Print "123" Continue Print "456"
5, While+else
#Unlike other languages else, which is usually only the same as if, there is a while ... else statement in Python, and the else function after the while is the
When the while loop executes normally and the middle is not interrupted by a break, the statement after the else is executedCount =0 whileCount <= 5: Count+ = 1Print("Loop", Count)Else: Print("The loop is running, it's done.")Print("-----out of a while loop------") output Loop1Loop2Loop3Loop4Loop5Loop6The loop is running, it's done.-----Out of whileLoop------#if it is break during execution, then the Else statement is not executed.Count =0 whileCount <= 5: Count+ = 1ifCount = = 3: Break Print("Loop", Count)Else: Print("The loop is running, it's done.")Print("-----out of a while loop------") output Loop1Loop2-----out of whileLoop------
Iv. Process Control for the loop
1 iterative loops: For, the syntax is as follows
For I in range (10):
Indented block of code
2 Break and Continue (IBID.)
3 Loop nesting
# Print 99 multiplication table for in range (1,10): is in range (1,i+1): print(' %s*%s=%s' % (i,j,i*j), end=') # Print has a default line break, where the default newline is changed to an empty string print()
analyze how to print pyramids" "#max_level =5 * #current_level = 1, number of spaces =4,* =1 * * * #current_level = 2, number of spaces =3,* = 3 * * * * * #current_level = 3, number of spaces =2,* =5 ******* #current_level = 4, number of spaces =1,* numbers =7********* #current_level = 5, number of spaces =0,* number =9# mathematical expression space = max_level-current_level* numbered =2*current_level-1" "#ImplementMax_level=5 forCurrent_levelinchRange (1,max_level+1): forIinchRange (max_level-current_level):Print(' ', end="')#print multiple spaces consecutively in a row forJinchRange (2*current_level-1): Print('*', end="')#print multiple spaces consecutively in a row Print()
Python Full Stack development-day2 Boolean, Process Control, loop