Python 基础学习之if语句

cars=['audi','xiali','bwm','benz',]
##根据要求bmw全部为大写,其他的首字母为大写
for car in cars:
    if car=='bmw':
        print(car.upper())
    else:
        print(car.title())

输入结果为:

Audi
Xiali
Bwm
Benz

2.知识点2,检查特定值是否包含在列表中。

使用关键字in

例如:

exampleList=['aaa','bbb','ccc']
'aaa' in exampleList
True
'ddd' in exampleList
False
news=['NewYork Time','Router','Wind','xinhuashe']

baoshe='renminribao'

if baoshe not in news:
    print(baoshe.title()+",try try")

返回结构为:Renminribao,try try

 

3. and 和 or 

age_1=10
age_2=100
if age_1>9 and age_2>90:
    print("age_1: "+str(age_1)) ## int to string  使用str(int),相反 string to int 使用 int(string)
    print("age_2: "+str(age_2))
    
结果是:

age_1: 10
age_2: 100

 

4.elif=else if

##测试5,使用 if-elif-else  其中elif=else if
age=12;
if age<12:
    print("erron");
elif age==12:
    print("right")
else:
    print("go")

返回结果为:right

5.使用多个列表

example_begin=['a',b','c','d','e']
example_end=['a','g','e']

for list in example_begin:
    if list in example_end:
        print("包含:"+str(list))
    else:
        print("不包含:"+str(list))


返回结果为:

包含:a
不包含:b
不包含:c
不包含:d
包含:e

 

posted @ 2018-10-29 22:28  ~@@~  阅读(206)  评论(0编辑  收藏  举报