1. Conditional Judgment Statement
The keywords for conditional selection statements in Python are: If, elif, else three. Its basic form is as follows:
AGE_OF_CC = 27age = Int (Input ("Guessage:")) if Age = = age_of_cc: print ("Yes,you got It!") Elif Age > AGE_OF_CC: print ("Big guess!") else: print ("Guess small!") ")
If statement execution is characterized by judging from the top down;
Where the elif and else statement blocks are optional. The branch statement executes only if and elif are judged true, and the Else branch is executed only if and all of the elif's judgments are false. Note in the conditional selection statement in Python, there is a colon behind the judgment.
2. Looping statements2.1 While Loop
Usage:
While condition: xxxxxx
The while loop executes the statement that is subordinate to it until the condition is False (false)
2.1.1 Break Skip Loop
code example:
AGE_OF_CC = 27Count=0 whileCount < 3: Age= Int (Input ("Guessage:")) ifAge = =AGE_OF_CC:Print("yes,you got it!") Break elifAge >AGE_OF_CC:Print("Big guess! ") Else: Print("Guess small! ") Count+ = 1Else: ifCount = = 3: Print("Too many mistakes! ")
2.1.2 Continue skip the cycle
code example:
i = 1while i <: i + = 1 if i%2 > 0: # non-even when skipping output continue print (i) # output even 2, 4, 6, 8, 10
2.2 for Loop
The For loop requires a pre-set number of cycles (n), and then executes the statement that is subordinate to the for (n) times.
code example:
For I in range: print (i) #输出0 1 2 3 4 5 6 7 8 9
The while Loop Judgment statement code example:
AGE_OF_CC = 27count =0while Count < 3: Age = Int (Input ("Guessage:")) if age = = age_of_cc: print ("Yes,you go T it! ") Break Elif Age > AGE_OF_CC: print ("Big guess!") Else: print ("Guess small!") ") count + = 1else: if Count = = 3: print (" error too many times! ") ")
For condition Judgment code example:
AGE_OF_CC = 27count = 0for i in range (3): Age = Int (Input ("Guessage:")) if age = = age_of_cc: print ("Yes,you go T it! ") Break Elif Age > AGE_OF_CC: print ("Big guess!") Else: print ("Guess small!") ") count + = 1else: if Count = = 3: print (" error too many times! ") ")
3 Other
3.1 Input
Input is a function in which the user can enter a string to be saved into a variable
code example:
Name = input ("Please input your name")
3.2 Print
Using print () to add a string to the parentheses, you can output the specified text to the screen
code example:
Print ("hello! ")
3.3 Type Conversions
As you can see above, input inputs are considered to be strings (see) in Python, so we need to type-convert the contents of input:
Convert to int example:
age = Int (input ("Age is:"))
Convert back string: str ()
Python Learning Notes (iii) conditional judgments and loops