公众号#小鹏长翅

 

python中的布尔表达式&条件判断&深拷贝/浅拷贝

公众号‘小鹏长翅’同步发布

一、布尔表达式

布尔值 Ture False

3>2就算是一个布尔表达式,返回的值是True

1==2也是一个布尔表达式,返回False

=:表示赋值

==:判断恒等

!=:判断不相等

字符串之间的比较,根据ASCII码进行判断

字符串的比较,只比较第一位,第一位相同时,比较第二位

'a'>'A'--->Ture  #a=97,A=65

in,not in

list1 = [100,200,[300,400,500]]
100 in list1--->Ture
100 not in list1--->False
300 in list1--->False  #300属于子列表
300 in list1[-1]--->True

and,or

and:一假为假,全真为真

print(3>2 and 2>1 and 1>10)--->False

or:一真为真,全假为假

print(3>2 or 1>2 or 4>4)--->True

not,and,or组合条件

优先级:not>and>or

2>1 and 1>2 not True or 3>2--->True

括号可以改变优先级

2>1 and 1>2 (not True or 3>2)--->Fasle

二、条件判断语句

if elif else

if 2>1:#如果if后边的条件成立,则执行下面有缩进的语句
  print('hello')
score = input('请输入一个数字:')#input()获取用户输入值,返回str类型

if score.isdigit():
#sdigit(),判断对象是否是纯数字
  score = int(score)#把输入内容转化为int类型
  if score>=60>=80:#if语句内再有if语句,叫做嵌套
    print('及格')
  elif 80>score>=100:
    print('优秀')
  else:#不满足if/elif条件,则执行else语句
    print('不及格')
else:
  print('您输入的不是数字')
if a=1 and b=2:
  pass
等价于
if a=1:
  if b=2:
    pass

三、深拷贝浅拷贝

list2=[100,200,300,[400,500,600]]
list2_new=list2
#赋值,相当于起了一个别名,两个变量指向的是同一个对象
#对象发生变化时,两个变量会同时变化

浅拷贝

等同于完整切片[:]

import copy
list2 = [100,200,[300,400,500]]

list2_new = copy.copy(list2)#浅拷贝,生成的是新的对象,子列表仍然是同一个对象
等同于
list2_new = list2[:]

深拷贝

import copy
list2 = [100,200,[300,400,500]]
list2_new = copy.deerpcopy(list2)#列表与子列表都是新对象

posted on 2021-12-15 13:20  公众号#小鹏长翅  阅读(166)  评论(0编辑  收藏  举报

导航