程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

0 basic learning python (12)

編輯:Python

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.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved