一.逻辑运算符
not:逻辑非,也就是取反
print(not 1)#1在逻辑运算中代表true,not 1即为false
False
and:逻辑与(全真为真,一假为假)
print(1 and 4>1 and True)
True
print(3>4 and 0 and False and 1)
False
or:逻辑或(一真为真,全假为假)
print(1 or 4>1 or True)
1 #1在逻辑运算中代表True
print(3>4 or 0 or False)
False
注意:优先级:not >and>or ()拥有最高优先级,“()”里面的内容先算
二、成员运算符
1.in
判断一个字符或者字符串是否存在于一个大字符串中:
print("eogn" in "hello eogn")
print("e" in "hello eogn")
True
True
判断元素是否存在于列表:
print(1 in [1,2,3])
print('x' in ['x','y','z'])
True
True
判断key是否存在于列表
print('k1' in {'k1':111,'k2':222})
print('111' in {'k1':111},'k2':222)
2.not in:作用与in相反,判断一个字符或字符串是否不存在于另一个大字符串中。
二、身份运算符
1.is:判断两个标识符是不是引用自一个对象,如果是则返回True,不是则返回false
x=1
y=1
print(x is y)
True
2.not is:作用与is相反
x=1
y=1
print(x is not y)
False
x=1
y=2
print(x is not y)
True
三、if判断语句:
语法1:
if条件:
代码1
代码2
代码3
语法2:
if条件:
代码1
else:
代码2
语法3:
if条件:
代码1
elif:
代码2
else:
代码3
四、浅copy与深copy
l1 = ['xxq',18,'male'] #定义列表l1
l2 = l1 #把l1的值引用给l2
print(l1) #输出l1的内容
print(l2) #输出l2的内容
print(id(l1)) #输出l1的id
print(id(l2)) #输出l2的id
['xxq', 18, 'male']
['xxq', 18, 'male']
58970184
58970184
从上面可以看出,当使用l2=l1时,如果l1中的元素值改变,那么l2对应的元素值也会改变。如果要将两个列表区分开互不影响,那么就要使用copy。
1.浅copy:是把原列表第一层的内存地址不加区分完全copy一份给新列表。
例:
l1 = ['xxq',18,'male']
l3 = l1.copy()
print(l1)
print(l3)
print(id(l1))
print(id(l3))
['xxq', 18, 'male']
['xxq', 18, 'male']
30396488
60089832
由此可以看出:l3拷贝了l1的值,但是l3和l1的id是不同的。
对于不可变类型的赋值,都是产生了新值,让原列表的索引指向新的内存地址,并不会影响新列表。
l1 = ['xxq',18,'male']
l3 = l1.copy()
l1[0] = 'asd'
l3[0] = 'zxc'
print(l1)
print(l3)
print(id(l1))
print(id(l3))
print(id(l1[0]),id(l1[1]),id(l1[2]))
print(id(l3[0]),id(l3[1]),id(l3[2]))
['asd', 18, 'male']
['zxc', 18, 'male']
11718728
13362664
13484608 1494739136 13484576
13484640 1494739136 13484576
Process finished with exit code 0
这个例子中,l4浅Copy了l1,并且l1和l4列表中的列表元素值改变,可以看出l1和l4列表内的元素内存地址相同,列表内存地址不同
l1 = ['xxq',18,[1,2]]
l4 = l1.copy()
l1[2][0] = 20
l4[2][1] = 10
print(l1)
print(l4)
print(id(l1))
print(id(l4))
print(id(l1[0]),id(l1[1]),id(l1[2]),id(l1[2][0]),id(l1[2][1]))
print(id(l4[0]),id(l4[1]),id(l1[2]),id(l4[2][0]),id(l4[2][1]))
['xxq', 18, [20, 10]]
['xxq', 18, [20, 10]]
19654120
19654824
19777088 1494739136 18272328 1494739168 1494739008
19777088 1494739136 18272328 1494739168 1494739008
综合上可以得出,要想copy得到的新列表与原列表的改操作完全独立开
必须有一种可以区分开可变类型与不可变类型的copy机制,这就是深copy
2.深copy:要使用深Copy,要先导入库:
import copy
list1=[
'egon',
'lxx',
[1,2]
]
list3=copy.deepcopy(list1)
# print(id(list1))
# print(id(list3))
# print(list3)
# 不可变 不可变 可变
# print(id(list1[0]),id(list1[1]),id(list1[2]))
# print(id(list3[0]),id(list3[1]),id(list3[2]))
'''
4497919088 4498367856 4498449216
4497919088 4498367856 4498595328
'''
# print(list3)
# print(id(list1[2][0]),id(list1[2][1]))
# print(id(list3[2][0]),id(list3[2][1]))
list1[0]='EGON'
list1[1]='LXX'
list1[2][0]=111
list1[2][1]=222
# print(list1)
print(list3)
五、while循环
print(1)
while 条件:
代码1
代码2
代码3
print(3)
count=0
while count < 5: # 5 < 5
print(count) # 0,1,2,3,4
count+=1 # 5
print('顶级代码----->')
退出循环的两种方式:
①:将条件改为false:
tag=True
while tag:
inp_name=input('请输入您的账号:')
inp_pwd=input('请输入您的密码:')
if inp_name == username and inp_pwd == password:
print('登录成功')
tag = False # 之后的代码还会运行,下次循环判断条件时才生效
else:
print('账号名或密码错误')
# print('====end====')
②:break,只要运行到break就会立刻终止本次循环。
while True:
inp_name=input('请输入您的账号:')
inp_pwd=input('请输入您的密码:')
if inp_name == username and inp_pwd == password:
print('登录成功')
break # 立刻终止本层循环
else:
print('账号名或密码错误')
# print('====end====')
while +continue:结束本次循环,直接进入下一次
强调:在continue之后添加同级代码毫无意义,因为永远无法运行
count=0
while count < 6:
if count == 4:
count+=1
continue
# count+=1 # 错误
print(count)
count+=1
while +else:针对break
count=0
while count < 6:
if count == 4:
count+=1
continue
print(count)
count+=1
else:
print('else包含的代码会在while循环结束后,并且while循环是在没有被break打断的情况下正常结束的,才不会运行')
count=0
while count < 6:
if count == 4:
break
print(count)
count+=1
else:
print('======>')
应用案例:
要求:输入帐号密码,输错3次提示并退出
username = 'xxq'
password = '123'
count=0
while count < 3:
inp_name=input('请输入您的账号:')
inp_pwd=input('请输入您的密码:')
if inp_name == username and inp_pwd == password:
print('登录成功')
while True:
cmd=input("输入命令>: ")
if cmd == 'q': # 整个程序结束,退出所有while循环
break
else:
print('命令{x}正在运行'.format(x=cmd))
break
else:
print('账号名或密码错误')
count+=1
else:
print('输错3次,退出')
六:for循环
for 变量名 in 可迭代对象: #可迭代对象可以是:列表、字典、字符串、元组、集合
代码1
代码2
代码3
案例1:列表循环取值
l = ['xxq','qwe','asd']
i = 0
while i < 3:
print(l[i])
i += 1
xxq
qwe
asd
案例2:字典循环取值
dic = {'name':'xxq','age':18,'gender':'male'}
for i in dic:
print(i,dic[i])
name xxq
age 18
gender male
案例3:字符串循环取值
# 简单版
msg = 'my name'
for i in msg:
print(i)
m
y
n
a
m
e
for循环与while循环的异同:
相同之处:都是循环,for循环能做的while循环都能做。
不同之处;while循环被称为“条件循环”,循环次数取决于条件何时变为假。for循环称之为“取值”循环,循环次数取决于in后包含的值的个数。
注意:for循环可以与break、else和continue连用,用法和效果与while循环相同,且终止for循环只有用break一种方案。