and 和 or 是python的两个逻辑运算符,可以使用and , or来进行多个条件内容的判断。下面通过代码简单说明下and or的用法:
1. or:当有一个条件为真时,该条件即为真。逻辑图如下:
测试代码如下:
a=raw_input('please input somting:') if a=='a' or a=='b': print 'it is a or b' else: print 'it is not a or b'
执行代码,输入a,b,ac,结果如下:
please input somting:a
it is a or bplease input somting:b
it is a or bplease input somting:ac
it is not a or b
通过这个例子,我们可以看出,当输入为a或者b时,满足 a==‘a'或者a=='b'的条件,即满足if条件。
2.or:当所有条件为真时,该条件即为真。逻辑图如下:
测试代码如下:
a=raw_input('please input somting:') if a!='a' and a!='b': print 'it is not a or b' else: print 'it is a or b'
执行代码,输入a,b,ac,结果如下:
please input somting:a
it is a or bplease input somting:b
it is a or bplease input somting:ac
it is not a or b
通过这个例子,我们可以看出,只有当条件同时满足a!='a' 和 a!='b'时,才会执行 print 'it is not a or b'
3.为了深入了解and or的用法,考虑到当a='a' or 'b'或者a='a' and 'b'时,会是怎么样子的呢。让我们先测试or的用法看下,测试代码如下:
a=raw_input('please input somting:') if a=='a' or 'b': print 'it is a or b' else: print 'it is not a or b'
我们输入a,b,q,结果如下:
please input somting:a
it is a or b
please input somting:b
it is a or bplease input somting:q
it is a or b
我们发现,无论输入什么,都满足a==‘a' or 'b'这个条件,这是为什么呢"htmlcode">
a=raw_input('please input somting:') if a==('a' or 'b'): print 'it is a or b' else: print 'it is not a or b'
我们输入a和b,结果如下:
please input somting:a
it is a or b
please input somting:b
it is not a or b
因为‘a' or ‘b'这个条件,‘a'为第一个真值,所以这个条件其实返回的是‘a',所以只有当输入为a,时,才执行了 print 'it is a or b' 。
4.and :从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值。对于and的测试,同于or,这边就不做详细介绍了。文章观点如有什么错误的地方,欢迎指正。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 陈慧娴.1989-永远是你的朋友(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 何洛洛.2024-别叫醒我(EP)【光羽】【FLAC分轨】
- 林忆莲.1996-爱莲说2CD【华纳】【WAV+CUE】
- 黄妃.2005-红【亚律】【WAV+CUE】
- 刘美麟《同生》[FLAC/分轨][161.95MB]
- 群星《前途海量 电影原声专辑》[320K/MP3][106.26MB]
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】