先来看一个简单的例子:我的食物中,如果是beef则大写,如果不是beef则首字母大写。

#简单例子
my_foods=['pizza','beef','noodles','chicken',]

for a in my_foods:
    if a=='beef':
        print(a.upper())
    else:
        print(a.title())

python if数字大于小于 python中的if函数可以大写吗_python if数字大于小于

条件测试
if语句中是以True和False表达的,如果条件测试的值为True就执行代码,如果是False则忽略这些代码。

条件判断
一个等号=是赋值。
两个等号==判断是否相等,判断是否相等是是区分大小写的。
判断是否不相等 != ,其中!表示不的意思。
判断小于 < ,小于等于<=,大于>,大于等于>=

多条件判断(关系)
1.and 和,同时满足
2.or 或者,满足其中之一

数值判断举例:

#数值比较
age=21
age==21#返回True
age==11#False
age<23#True
age<=22#True
age>21#False
age>=21#True
#and 和 or 
age_1=12
age_2=23
age_1<=13 and age_2>=23#True
age_1>=11or age_<14#True

检查特点值是否在列表中或不在列表中
在列表中 in
不在列表 用 not in

#in 和 not in
my_foods=['pizza','beef','noodles','chicken',]
'pizza' in my_foods#True
'pizza' not in my_foods#False
'apple' in my_foods#False
'apple' not in my_foods#True

像上面返回值是True和False布尔类型返回对或错。

if语句

简单的if语句

name='张三'
if name=='张三':
    print('He is my friend')

python if数字大于小于 python中的if函数可以大写吗_python if数字大于小于_02


这里if语句后也要有冒号,也要缩进。符合判断条件的代码执行,不符合就不执行。

if-else结构

name='李四'
if name=='张三':
    print('He is my friend')
else:
    print('Nice to meet you')

python if数字大于小于 python中的if函数可以大写吗_for循环_03


这了判断条件如果是张三执行if下面的语句,其他情况则执行else下面的语句。

if-elif-else结构

name='李四'
if name=='张三':
    print('He is my friend!')
elif name=='李四':
    print('He is my teacher!')
else:
    print('Nice to meet you!')

python if数字大于小于 python中的if函数可以大写吗_python_04


上面例题,判断是否是张三如果是执行if后命令,如果是李四则执行elif后命令,否则其他执行else后命令。

使用多个人elif 和省略elsse

#多elif
score=98
if score<=100 and score>=90:
    print('A')
elif score<90 and score>=80:
    print('B')
elif score<80 and score>=70:
    print('C')
elif score<70 and score>=60:
    print('D')
else:
    print('E')

python if数字大于小于 python中的if函数可以大写吗_for循环_05


像上面成绩,在100-90之间为A,在90-80之间为B,在80-70之间为C,在70-60之间为D,其他情况为E。这里使用了多个elif。

#不要else
score=16
if score<=100 and score>=90:
    print('A')
elif score<90 and score>=80:
    print('B')
elif score<80 and score>=70:
    print('C')
elif score<70 and score>=60:
    print('D')
elif score<60 and score>=0:
    print('E')

python if数字大于小于 python中的if函数可以大写吗_学习_06

像上面这个例子,不要else。

如果多个并列测试可以用多个if进行判断。像下面的例子,用多个if实现,如果使用 if - elif - else不能实现,当if测试 通过后后面的就不在进行测试了。

a = ['football','lol','basketball']
if  'football' in  a:
    print('I like playing football')
if 'lol' in a:
    print('I like playing lol')
if 'basketball' in a:
    print('I like playing basketball')

python if数字大于小于 python中的if函数可以大写吗_python_07

a = ['football','lol','basketball']
if  'football' in  a:
    print('I like playing football')
elif 'lol' in a:
    print('I like playing lol')

python if数字大于小于 python中的if函数可以大写吗_for循环_08


检查特殊元素

比如我们去吃面要在面里加一些食材,把加的食材进行输出,我们可以使用for循环来打印。

noodles=['tomatoes','beef','green peppers']
for noodles in noodles:
    print(f"Adding {noodles}")

python if数字大于小于 python中的if函数可以大写吗_学习_09


如果现在西红柿卖完了,没有西红柿,我们可以输出西红柿已卖完,然后再输出顾客要加的材料,我们可以加一个if条件判断。

noodles=['tomatoes','beef','green peppers']
for noodles in noodles:
    if noodles == 'tomatoes':
        print('The tomatoes have been sold out\n')
    else:
        print(f"Adding {noodles}")

python if数字大于小于 python中的if函数可以大写吗_python if数字大于小于_10

如果我们上面都不加,我们可以输出你确定上面都不加吗?我们用if判断嵌套for循环来做。

noodles=[]
if noodles:
    for noodles in noodles:
        print(f"Adding {noodles}")
else:
    print("Are you sure don't want to add anything?")

python if数字大于小于 python中的if函数可以大写吗_python_11


如果,顾客点餐要加的食材没有打印没有食材,而有的食材我们输出已加。

我们可以定义两个变量一个是店里有的食材,一个是顾客要加的食材,用for循环嵌套if判断来实现。

noodles_1=['tomatoes','beff','green peppers']
noodles_2=['beff','green peppers','drumstick']
for noodles_2 in noodles_2:
    if noodles_2 in noodles_1:
        print(f"Adding {noodles_2}")
    else:
        print(f"Sorry,we don't have {noodles_2}")

python if数字大于小于 python中的if函数可以大写吗_if语句_12