【九月打卡】第2天Python第五章变量与运算符

2022/9/7 3:22:52

本文主要是介绍【九月打卡】第2天Python第五章变量与运算符,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

课程名称:全面系统Python3.8入门+进阶(程序员必备第二语言)

课程章节:5-8 不只是数字才能做比较运算

5-9 逻辑运算符

课程内容:

今天学习的内容包括:

不只是数字才能做比较运算

逻辑运算符

课程收获:

不只是数字才能做比较运算

b = 1

b+=b>=1

print(b)     --------  2

print(b>=1)   ----True   # 可以转化成  b+=True  或者 b = b + True

int(True) ----- 1

b = b + 1   ------- 2 # 可以证明这个算术题是等于2

https://img4.sycdn.imooc.com/6316a201000144eb04380358.jpg

<article data-content="[{"type":"block","id":"KwUF-1660643434730","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"63ds-1660643434729","leaves":[{"text":"" a'=""> 'b'   ---   False      #比较2个字母的阿斯克码","marks":[]}]}],"state":{}},{"type":"block","id":"OQCi-1660643477620","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"WZJz-1660643477619","leaves":[{"text":"ord('a')   ---97","marks":[]}]}],"state":{}},{"type":"block","id":"qqa8-1660643490006","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"LjI9-1660643490005","leaves":[{"text":"ord('b')   ---98","marks":[]}]}],"state":{}},{"type":"block","id":"sawj-1660643498704","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"510N-1660643498703","leaves":[{"text":"------------------------------------------------------------------------------------------------------","marks":[]}]}],"state":{}},{"type":"block","id":"cife-1660643519399","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"kErG-1660643519398","leaves":[{"text":"'abc' < 'abd'   -------True    # 先比较 a和a ,在比较b和b,最后比较c=99和d=100。 所以最后'abc' < 'abd'是True。","marks":[]}]}],"state":{}},{"type":"block","id":"AJwg-1660643541037","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"F5Bq-1660643541035","leaves":[{"text":"ord('abc')   -----   #  报错","marks":[{"type":"color","value":"#F33232"}]}]}],"state":{}},{"type":"block","id":"bGlQ-1660643555335","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"Mu6L-1660643555334","leaves":[{"text":"当一个字符串里面有多个元素的时候,他的比较规则----解析规则:他会吧字符串里面每个字符单独拿出来做比较。","marks":[]}]}],"state":{}},{"type":"block","id":"ekLD-1660643601256","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"1GaW-1660643601255","leaves":[{"text":"------------------------------------------------------------------------------------------------------","marks":[]}]}],"state":{}},{"type":"block","id":"Saga-1660643785229","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"OIEe-1660643785228","leaves":[{"text":"列表: 关系运算","marks":[]}]}],"state":{}},{"type":"block","id":"0UlJ-1660643817388","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"5iNK-1660643817387","leaves":[{"text":"[1,2,3] < [2,3,4]   ----- True   # 先比较2个列表第一个元素,就能得到结果。","marks":[]}]}],"state":{}},{"type":"block","id":"ujOI-1660643876909","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"zD2m-1660643876908","leaves":[{"text":"元组:","marks":[]}]}],"state":{}},{"type":"block","id":"onZh-1660643883954","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"jS1d-1660643883953","leaves":[{"text":"(1,2,3) < (1,3,2)   -----Ture    #第一个元素相等, 第二个元素2<3.所以能得出Ture","marks":[]}]}],"state":{}},{"type":"block","id":"vEGI-1660644028939","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"JjMQ-1660644028938","leaves":[{"text":"","marks":[]}]}],"state":{}}]'>

'a' > 'b'   ---   False      #比较2个字母的阿斯克码

ord('a')   ---97

ord('b')   ---98

------------------------------------------------------------------------------------------------------

'abc' < 'abd'   -------True    # 先比较 a和a ,在比较b和b,最后比较c=99和d=100。 所以最后'abc' < 'abd'是True。

ord('abc')   -----   #  报错

当一个字符串里面有多个元素的时候,他的比较规则----解析规则:他会吧字符串里面每个字符单独拿出来做比较。

------------------------------------------------------------------------------------------------------

列表: 关系运算

[1,2,3] < [2,3,4]   ----- True   # 先比较2个列表第一个元素,就能得到结果。

元组:

(1,2,3) < (1,3,2)   -----Ture    #第一个元素相等, 第二个元素2<3.所以能得出Ture


5-9逻辑运算符

and or not

https://img4.sycdn.imooc.com/6316a2120001d63904020190.jpg

逻辑运算符主要是用来操作布尔类型的。返回结果也是布尔类型的。

and (且),与      操作的2额类型都是真才,返回True

True and True   ----True

True and False ----- False

or (或)

True or False   ------ True

False or False ------ False

not  非 ; 他只操作一个变量

not False   ----- True

not True ----- False

not not True ---- True   # not True =Fasle ,not False=True

1 and 1  ---1

'a' and 'b' ------'b'

'a' or 'b' ----- 'a'

not 'a'-----False

上面数字和字母和布尔类型有转换关系:1 = True; a = True b = True

int float  0表示False,非0的时候将被任务是True

not 0.1 --------False

字符串【空字符串】将被认为False,负责被认为True。

not '' ------True

not ‘0’ ------ False     #字符串,有值会被认为是True,not后未False

列表:空列表将被认为是False,非空的列表将被认为是True

not [] -----True

not[1.2] ---- False

tuple(元组) set(集合) dict(字典):他们同样遵循和列表一样的逻辑。

[1] or [ ]  ------[1]  #他将得到第一个元素。

[ ] or [1]  -----[1] #不要关心or运算返回的是第一个元素还是第二个元素。要关心这个运算的结果到底代表着是Ture还是false,这是我们关注的重点。

'a' and 'b'  -----'b'

' ' and 'b' -----' '

not一定范围True和False。

1 and 0 ----0

0 and 1 ----0

#返回规律,当计算机读到第一个1的时候不能返回结果。他要比较2个数字。and是比较2个数字,他读到1 and时候不能判断结果。

1 and 2  -------2

#

https://img2.sycdn.imooc.com/6316a2230001013409681170.jpg

下载视频          



这篇关于【九月打卡】第2天Python第五章变量与运算符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程