We want to compare the values , For example, I want to see if a person 18 year , We can use symbols for comparison .
age=18
age==18
ture
The first one is to age Attach a value 18, The second two equal signs are used to judge whether age The value of is 18, If it is 18, So the result is ture.
Of course, there will be times when the answer is incorrect .
answer=17
if answer!=47
print(you is a loster)
you is a loster
Here, let's give answer Paid a value 17, Then we use if Statement to determine whether the value is 47,!= Means No , If not 47 Will be output you is a loster.
stay if Any comparison symbol can be used in the statement .
When you want to check multiple conditions , have access to and Juxtapose , If the and Both of the two conditions in parallel have passed , Then the answer to the expression is ture, If one of the expressions fails, the answer is false.
for example
age_0=22
age_1=18
age_0>=21 and age_1>=21
false
age_0=22
age_1=22
age_0>=21 and age_1>=21
ture
Use or You can also check , But for or As long as at least one condition is satisfied , You can pass the entire test , Only when both fail will the result be false.
age_0=22
age_1=18
age_0>=21 and age_1>=21
ture
age_0=19
age_1=18
age_0>=21 and age_1>=21
false
Sometimes we need to check whether a particular value is included in the list , To determine whether a particular value is already included in the list , You can use keywords in.
for example :
>>>cars=['bmw','audio','toyota']
>>>'bmw' in cars
ture
>>>'hongqi' in cars
false
Sometimes , It's important to make sure that specific values are not included in the list , We can use not in.
for example :
banned_user=['andrew','carolina','david']
user='marie'
if user not in banned_user:
print(f"{user.title(),you can post a response if you wish.")
Marie,you can post a response if you wish.
Boolean expression is a term you will encounter , In fact, it is the alias of conditional test , The result of a Boolean expression is either ture Or false.