1.if judgment
In Python programming, if statements are used in the execution of control programs, in the basic form:
If judgment condition: EXECUTE statement else: EXECUTE statement
In Python, use indentation instead of curly braces in the C language to tell the program what to do.
Indent--Four spaces recommended (use 2, 3 spaces or tab is available)
Don't mix tabs with spaces
The display logic for the spaces is always the same for different software, but there are a variety of tabs.
Some software expands the tab into a space, and some does not. Some tab widths are 4, some widths are 8,
These inconsistencies can make the code confusing, especially by indenting the python that represents the block structure.
Where the "judging condition" is established (not 0), the following statements are executed, and the execution content can be multiple lines, in order to be indented to differentiate the same range.
Else is an optional statement that executes when the content needs to be executed when the condition is not established.
When you determine whether two data is equal, use "= =" to indicate, "=" to indicate an assignment.
When making multiple judgements, add the "elif" statement in the middle.
Example (maximum number and minimum number of output three numbers):
Code 1
NUM1 = input ("NUM1:") num2= Input ("Num2:") num3= Input ("Num3:")ifNUM1 >num2:ifNUM1 >num3:ifnum2 >num3:Print("Max_num is NUM1","Min_num is num3") Else: Print("Max_num is NUM1","Min_num is num2") Else: Print("Max_num is num3","Min_num is num2")elifnum2 >num3:ifNUM1 >num3:Print("Max_num is num2","Min_num is num3") Else: Print("Max_num is num2","Min_num is NUM1")Else: Print("Max_num is num3","Min_num is num2")
View Code
Code 2
= Input ("NUM1:") num2= Input ("Num2:") num3= Input ("Num3:")ifNUM1 >num2:ifNUM1 >num3:Print("Max_num is NUM1") ifnum2 >num3:Print("Min_num is num3") Else: Print("Min_num is num2") Else: Print("Max_num is num3") Print("Min_num is num2")elifnum2 >num3:Print("Max_num is num2") ifNUM1 >num3:Print("Min_num is num3") Else: Print("Min_num is NUM1")Else: Print("Max_num is num3") Print("Min_num is num2")
View Code
The two pieces of code output the same result.
Output
2.while Cycle
In Python programming, a while statement is used to loop the execution of a program, which, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is:
While judging condition: EXECUTE statement
The execution statement can be a single statement or a block of statements. The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true.
The loop ends when the condition false false is judged.
While statement there are two other important command Continue,break to skip the loop, Continue is used to skip the cycle, break is used to exit the loop, in addition, "judging condition" can also be a constant value, indicating that the loop must be true.
You can add an else statement after the while statement, and the statement in else is executed when the loop is executed normally (that is, the while is not interrupted by a break jump).
Three paragraphs use the while statement to guess the age code.
age_of_princal == Int (input (">>:"))if guess_age = = age_of_princal: Print ("yes,you got it! " Else: print ("no,it s wrong. ")
View Code
Print"Guess age!") Age= 24Flag=True whileFlag:use_input_age= Int (Input ("Age is :")) ifUse_input_age = =Age :Print("Yes") Flag=FalseelifUse_input_age <Age :Print("It ' s smaller") Else: Print("It ' s Biger")Print("end!")
View Code
Print("Guess age!") Age= 24 whileTrue:use_input_age= Int (Input ("Age is :")) ifUse_input_age = =Age :Print("Yes") Break elifUse_input_age <Age :Print("It ' s smaller") Else: Print("It ' s Biger")Print("end!")
View Code
When you run the third piece of code, the output is
If judgment of Python and while loop