Condition test
The core of each if statement is an expression with a value of ture or false, which is known as a condition test. Python determines whether to execute the code in the If statement based on the value of the condition test as ture or false. If the value of the condition test is Ture,python, the code immediately following the IF statement is executed, and if it is False,python, the code is ignored.
string comparison
string checking is case-sensitiveCheck for equality
" BMW " " BMW " True " Audi " " BMW " False
Check for any unequal
" BMW " " BMW " False " Audi " True
Compare numbers
== |
is equal |
!= |
is not equal |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
>>> age = 18>>> Age = = +true>>> Age! *true>>> age <Tru E>>> Age >false>>> age <= +True>>> age >=False
Check multiple conditions
True if the and multiple bars are set, otherwise false
and and age >True and and age <False
True if more than one condition is set, false if all are not true
or or age >Trueoror age >False
in to determine whether a particular value is in the included list
>>> name = ['Frank','Alex','Bob ' ]'Frank' in nametrue' May ' inch Namefalse
&NBSP; < Span style= "COLOR: #ff0000" >not in to determine that a specific value is not in the included list
>>> name = ['Frank','Alex','Bob ' ]'Frank' not in namefalse' May ' not inch nametrue
Boolean expression
The result is either True or FALSE, true or false.
If-n a ELIF-ELSE structure
This is the longest case if the structure is the shortest case, just use if on the line
# election age =if age >=: print("enough to vote! " )# Run results enough to vote!
Two choice cases, If-else
# election age =if age >=: print("your is oldenough to vote! " )else: print("sorry,you is too young to vote! " )# running results sorry,you is too young to vote!
In many cases if-elif-else,elif can have multiple
# amusement park Selling Tickets age = 12 if Age < 4: print ( " you admission cost is $. " ) elif Age < 18 : print ( " your admission cost is $. " ) else : print ( " your admission cost is $. ")
Python does not require the IF-ELIF structure to have an else code block, else is an all-encompassing statement, as long as the non-satisfied will execute else under the statement, may cause invalid or even malicious data, if you know the final test conditions, you should consider the elif instead of else! No matter if-n a elif-else, as long as one satisfies, it will only execute the condition that evaluates to true and will not perform the other.
Python returns True when the list contains at least one element, and returns false! when the list is empty
Name = []ifName:Print("Name in")Else: Print("No name")#Run ResultsNo NameName= ["Frank"]ifName:Print("Name in")Else: Print("No name")#Run ResultsNameinch
Python programming from Get started to practice _ Chapter Fifth _IF statements