一.逻辑运算符
and 与
可以用and连接多个条件,会按照从左到右的顺序依次判断,一旦某一个条件为False,则无需再往右判断,可以立即判定最终结果就为False,只有在所有条件的结果都为True的情况下,最终结果才为True。
or 或
可以用or连接多个条件,会按照从左到右的顺序依次判断,一旦某一个条件为True,则无需再往右判断,可以立即判定最终结果就为True,只有在所有条件的结果都为False的情况下,最终结果才为False
练习题:
2 > 1 or 1 != 1 or True or 3 > 2
False
not 非(取反)
二.成员运算符
判断某个个体是否在某个群体中
符号:in(在) not in(不在)
name_list = ['kevin', 'jack', 'tony', 'tom']
print('kevin' in name_list) # True
print('aaa' not in name_list)
print('bbb' in name_list)
print('k' in 'kevin')
print('hello' in 'helloworld')
三.身份运算符
判断两个值是否相等
符号:is(比较的是内存地址)
==(比较的是值)
s1 = ['a', 'b', 'c']
s2 = ['a', 'b', 'c']
print(s1 == s2) #Ture
print(id(s1))
print(id(s2))
print(s1 is s2) #False
'''
值相等的内存地址不一定相等
内存地址相等的值一定相等
'''
四.流程控制(重点)
4.1流程控制(重点)
# 控制事物的执行流程
流程控制总共有3种情况:
1. 顺序结构
# 就是自上而下的执行
2. 分支结构
# 分支结构就是根据条件判断的真假去执行不同分支对应的子代码
3. 循环结构
# 循环结构就是重复执行某段代码块
4.2分支结构
"""
注意事项:
1. 根据条件的成立与否,决定是否执行if代码块
2. 我们通过缩进代码块,来表示代码之间的从属关系
3. 不是所有的代码都拥有子代码块
4. 我们推荐使用缩进4格
5. 同属于一个代码块的子代码块缩进量一定要一样
ps:遇到冒号就要回车换行,缩进
"""
# 1. 单if判断
关键字:if
"""
语法格式:
if 判断条件:
print('小姐姐好')
"""
# 2. 双分支结构
"""
语法格式:
if 判断条件:
条件成立执行的子代码块
else:
条件不成立执行的子代码块
"""
# 3. 多分支结构
"""
语法格式:
if 条件1:
条件1成立执行的子代码块
elif 条件2:
条件1不成立条件2成立执行的子代码块
elif 条件3:
条件1、2不成立条件3成立执行的子代码块
elif 条件4:
条件1、2、3不成立条件4成立执行的子代码块
else:
以上条件都不成立的时候执行的代码块
"""
# else语句是可有可无的
score = 56
if score >= 90:
print('优秀')
elif score >=80:
print('良好')
elif score >=70:
print('一般')
elif score >=60:
print('及格')
4.3if判断
age = 18
height = 1.6
weight = 90
is_beautiful=True
is_success=True
if age < 20 and height == 1.6 and weight<100 and is_beautiful:
print('给个微信呗!!!')
'''要么给,要么不给'''
if is_success:
print('吃饭 看电影...')
else:
print('慢走不送!!!')
else:
print('好吧')
4.4if练习题
1. 写一个登录功能,用户名是:kevin 密码是:123
# 要求:用户名和密码输入正确,打印登录成功,否则登录失败
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('登录成功')
else:
print('登录失败')
2. 根据用户名的不同打印不同的身份
kevin:管理员 jack:保安 tom:保洁 tank:销售 其余的都是普通人
username = input('请输入你的名字:>>>')
if username == 'kevin':
print('管理员')
elif username == 'jack':
print('保安')
elif username == 'tom':
print('保洁')
elif username == 'tank':
print('销售')
else:
print('普通人')
4.5while循环结构
"""
while语法格式
while 条件:
循环体
"""
while True:
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('登录成功')
else:
print('登录失败')
4.6while+break
# count = 0
while True:
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('登录成功')
break # 结束本层循环
else:
print('登录失败')
4.7break本层含义
while True:
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('欢迎光临')
while True:
cmd=input('请输入你的指令:>>>')
if cmd == 'q':
# 结束程序
break
print('正在执行你的指令:%s' % cmd)
break
else:
print('登录失败')
print(123)
4.8标志位的使用
flag = True
while flag:
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('欢迎光临')
while flag:
cmd=input('请输入你的指令:>>>')
if cmd == 'q':
# 结束程序
flag = False
print('正在执行你的指令:%s' % cmd)
else:
print('登录失败')
4.9作业
猜年龄的游戏
1 普通要求
只允许输错3次,输入正确直接结束
2 当输错3次之后,不要直接结束,问用户是否还要在次输入,如果要,继续输入,如果不要,直接结束
# 猜年龄的游戏
"""
1. 普通要求
三次机会
2. 拔高要求
三次之后再次询问是否继续输入
"""
# 三次机会,猜对了,就直接结束
# 先写主体功能
# 1. 先定义一个年龄出来
real_age = 18
# 定义一个计数器
count = 1
while count < 4:
# 2. 用户输入年龄
guess_age = input('请输入你猜测的年龄:>>>') # 数字
# 2.1 把字符串的数字转为整型,int,可能会出现bug
guess_age = int(guess_age)
# 3. 判断输入的年龄和真实年龄是否一样
if guess_age > real_age:
print('猜大了')
# count+=1
elif guess_age < real_age:
print('猜小了')
# count+=1
else:
print('哎呀,猜对了')
break
# 优化
count+=1
'''拔高'''
# 1. 先定义一个年龄出来
real_age = 18
# 定义一个计数器
count = 1
while count < 4:
# 2. 用户输入年龄
guess_age = input('请输入你猜测的年龄:>>>') # 数字
# 2.1 把字符串的数字转为整型,int,可能会出现bug
guess_age = int(guess_age)
# 3. 判断输入的年龄和真实年龄是否一样
if guess_age > real_age:
print('猜大了')
# count+=1
elif guess_age < real_age:
print('猜小了')
# count+=1
else:
print('哎呀,猜对了')
break
# 优化
count+=1
if count == 4:
# 询问:是否再次输入
cmd = input('三次机会使用完了,是否再次输入yes/no')
if cmd == 'yes':
count=1
else:
print('结束')
break