1. Conditional statements
Indent with 4 spaces
If condition:
#条件成功,
Else
#条件不成功
If condition: {
#条件成功,
#条件成功,
}else{
#条件不成功,
#条件不成功,
}
The IF syntax is this or is used: or with {}
2. If for a single condition
[[email protected] python]#python3.5 if.py>>>>franksuccess[[email protected] python]#python3.5 if.py>>>>aaaerror[[email protected] python]#Cat if.py#!/usr/bin/env python#_*_ Coding:utf8 _*_#when there is input, wait forever until the user enters a value, the input value will be assigned to N,n on behalf of the user input contentN1= Input ('>>>>')ifN1 = ='Frank': Print("Success")Else: Print("Error"when defining a variable assignment, use an equals sign=The character is enclosed in double quotation marks to use the 2 equals sign when the If comparison, the character resulting single quotation marks.3If statement [[email protected] python]#python3.5 if.py First>>>Franksecond>>>Yesfrank Niubi First judgment [[email protected] python]#python3.5 if.py First>>>Franksecond>>>Nnnnfrank Error A second judgment [[email protected] python]#python3.5 if.py First>>>Oooerror third judgment [[email protected] python]#Cat if.py#!/usr/bin/env python#_*_ Coding:utf8 _*_#when there is input, wait forever until the user enters a value, the input value will be assigned to N,n on behalf of the user input contentN1= Input ('first>>>')ifN1 = ='Frank': N2= Input ('second>>>') ifN2 = ="Yes": Print('Frank Niubi The first judgment of') Else: Print("Frank Error A second judgment")Else: Print("error A third judgment")
4, multiple conditions of the IF condition judgment of the syntax, pass represents nothing to execute, Python can not be left blank, or error
If condition 1:
Pass
Elif Condition 2:
Pass
Elif Condition 3:
Pass
Else
Pass
5, and and OR
[[email protected] python]#python3.5 if2.pyUser name>>>Frank Password>>>frank123 input correct [[email protected] python]#python3.5 if2.pyUser name>>>Frank Password>>>aaerroe[[email protected] python]#Cat if2.py#!/usr/bin/env python#_*_ Coding:utf8 _*_#when there is input, wait forever until the user enters a value, the input value will be assigned to N,n on behalf of the user input contentN1= Input ('User name >>>') N2= Input ('Password >>>')ifN1 = ="Frank" andN2 = ="frank123": Print('Enter the correct')Else: Print('Erroe')
Or is not tested, anyway is and is 2 satisfied to be true, or is a casual satisfaction is true
6.if Judging if the number is even
[[email protected] python]#Cat if3.py#!/usr/bin/env python#_*_ Coding:utf8 _*_#when there is input, wait forever until the user enters a value, the input value will be assigned to N,n on behalf of the user input contenta=0 whileA < 20: Temp= A% 2iftemp = =0:PrintaElse: Passa= A + 1[[email protected] python]#python if3.py024681012141618
Syntax and case of the IF condition statement for Python