一、python保留字(关键字)
python的保留字即关键字,我们不能把它们用作任何标识符名称。但是我们怎么知道那些词语是否是关键字呢?方法如下:
二、布尔表达式
1.布尔值 (Ture/False)
3>2就算是一个布尔表达式,返回的值是True
1==2也是一个布尔表达式,返回False
=:表示赋值
==:判断恒等
!=:判断不相等
字符串之间的比较,根据ASCII码进行判断
字符串的比较,只比较第一位,第一位相同时,比较第二位
2.in,not in
list1 = [100,200,[300,400,500]]
1.100 in list1--->Ture
2.100 not in list1--->False
3.300 in list1--->False #300属于子列表
4.300 in list1[-1]--->True
3.and,or
and:一假为假,全真为真
or:一真为真,全假为假
print(3>2 and 2>1 and 1>10) #1>10为假,所以全假
--->False
print(3>2 or 1>2 or 4>4) #3>2为真,所以真
--->True
4.not,and,or组合条件
优先级:not>and>or,但是括号可以改变优先级
1.
2>1 and 1>2 not True or 3>2
--->True
2.
2>1 and 1>2 (not True or 3>2)
--->Fasle
三、条件控制语句
if...elif...else
格式:
if 表达式1:
语句
if 表达式2:
语句
elif 表达式3:
语句
else:
语句
elif 表达式4:
语句
else:
语句
示例:
1.if判断
if 2>1:#如果if后边的条件成立,则执行下面有缩进的语句
print('hello')
2.if多层嵌套
score = input('请输入一个数字:')#input()获取用户输入值,返回str类型
if score.isdigit():
#sdigit(),判断对象是否是纯数字
score = int(score)#把输入内容转化为int类型
if score>=80:#if语句内再有if语句,叫做嵌套
print('优秀')
elif 80>score>=60:
print('及格')
else:#不满足if/elif条件,则执行else语句
print('不及格')
else:
print('您输入的不是数字')
四、浅拷贝&深拷贝
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)#列表与子列表都是新对象