Python编程从入门到实践(第五章if语句学习总结)

1、 条件测试(每条if语句的核心都是一个值为True或False的表达式)
2、检查是否相等或不相等时大小写区分
3、检查多个条件使用and 或 Or
4、检查特定值是否包含在列表中可使用in 或 not in
5、if语句结构(if…elif(>=0个)…else…) 执行一个代码块时使用
6、多个if语句结构,执行多个代码块时使用
7、确定列表是否为空
8、使用多个列表

#第五章 if 语句######
#1、 条件测试(每条if语句的核心都是一个值为True或False的表达式)

peoples = ['alice','tom','brown','tihua','john']
for people in peoples:
    if people == 'tom':  #如果结果等于tom,全大写显示
        print(people.upper())
    elif people == 'john':  #如果结果等于john,全大写显示
        print(people.upper())
    else:
        print(people.title())    #否则只首字母大写显示
#输出结果:
Alice
TOM
Brown
Tihua
JOHN

########################
#2、检查是否相等或不相等时大小写区分

people = 'tom'
print(people == 'Tom')
#输出结果:
False

#转换成首字母大写后再进行比对

people = 'tom'
print(people.title() == 'Tom')
#输出结果:
True

#不相等检查

people = 'tom'
print(people != 'Tom')
#输出结果:
True

#其他数字比较符>,<,>=,<=
#########################
#3、检查多个条件使用and 或 Or

age_1 = 15
age_2 = 20
print(age_1 >=16 and age_2 <=20)  #and条件,同时满足才为True
print(age_1 >=16 or age_2 <=20)  #or条件,一个满足即为True
#输出结果:
False
True

###################
#4、检查特定值是否包含在列表中可使用in 或 not in

peoples = ['alice','tom','brown','tihua','john']
print('john' in peoples)
people = 'Lucy'
if people not in peoples:
    print(people + " not in the list")
#输出结果:
True
Lucy not in the list

###################
#5、if语句结构(if…elif(>=0个)…else…) 执行一个代码块时使用

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
elif age > 65:
    price = 5
print("your cost is "+ str(price))
#输出结果:
your cost is 5

#else 包罗万象,只要 不满足任何if或elif中的条件测试,其中代码就会执行,可能会引起无效甚至恶意的数据。

##########################
#6、多个if语句结构,执行多个代码块时使用

peoples = ['alice','tom','john']
if 'alice' in peoples:
    print("has the person1")
if 'tom' in peoples:
    print("has the person2")
if 'john' in peoples:
    print("has the person3")
print("All in one!")
#输出结果:
has the person1
has the person2
has the person3
All in one!

#####################
#7、确定列表是否为空

peoples = []
if peoples: #在if语句中将列表名用在条件表达式中时,列表至少包含一个元素时返回True,为空时返回False
    for people in peoples:
        print("Adding " + people + ".")
    print("\nFinished add the person")
else: #列表为空时符合此条件
    print("Are you sure you want the empty body?")
#输出结果:
Are you sure you want the empty body?

####################
#8、使用多个列表

available_peoples = ['alice','tom','brown','tihua','john']
requested_peoples = ['alice','Lucy','brown','Lily','john']
for requested_people in requested_peoples:
    if requested_people in available_peoples:
        print(requested_people + "the person is  exit")
    else:
        print("sorry,can't find the person " + requested_people)
#输出结果:
alicethe person is  exit
sorry,can't find the person Lucy
brownthe person is  exit
sorry,can't find the person Lily
johnthe person is  exit

Python编程从入门到实践基础知识:https://blog.csdn.net/louzhu_lz/article/details/90721685
Python编程从入门到实践(第三、四章的列表和元祖):https://blog.csdn.net/louzhu_lz/article/details/91354506
Python编程从入门到实践(第五章if语句学习总结):https://blog.csdn.net/louzhu_lz/article/details/91409903
Python编程从入门到实践(第六章字典学习总结):https://blog.csdn.net/louzhu_lz/article/details/91910554
Python编程从入门到实践(第七章用户输入和while循环学习总结):https://blog.csdn.net/louzhu_lz/article/details/92384649
Python编程从入门到实践(第八章函数)学习总结:https://blog.csdn.net/louzhu_lz/article/details/93377817

你可能感兴趣的:(Python入门)