程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

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

发布于2019-08-19 16:22     阅读(656)     评论(0)     点赞(4)     收藏(5)


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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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

people = 'tom'
print(people == 'Tom')
#输出结果:
False
  • 1
  • 2
  • 3
  • 4

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

people = 'tom'
print(people.title() == 'Tom')
#输出结果:
True
  • 1
  • 2
  • 3
  • 4

#不相等检查

people = 'tom'
print(people != 'Tom')
#输出结果:
True
  • 1
  • 2
  • 3
  • 4

#其他数字比较符>,<,>=,<=
#########################
#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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

###################
#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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

###################
#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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

#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!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

#####################
#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?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

####################
#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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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



所属网站分类: 技术文章 > 博客

作者:大魔王

链接:https://www.pythonheidong.com/blog/article/48851/6948239473eba702b9b0/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)