布尔表达式:意义:表示判断中的是与否,一般用于条件测试中。
逻辑运算, 逻辑运算符:用于检测两个或两个以上的条件是否满足 。逻辑运算只存在于布尔类型中。
运算符 描述
and:逻辑“与” 当运算符两边的两个运算对象都为 true 时,结果为 true
or: 逻辑“或”当运算符两边的两个运算对象其中有一个运算对象为 true 时,结果即为 true
not: 逻辑“非” 用于反转运算对象的状态
表达式的应用——条件测试:
• 检查当前变量是否与一个特定值相等/不相等。
• 比较数字的大小。
• 检查特定值是否在某序列里。
Python 根据表达式的值为 True 还是 False 来决定是否执行 if 语句中的代码。
如果是 True,Python 就执行紧跟在 if 语句后面的代码。如果是 False,则不执行。
dog = '小黄'
print(dog == '小黄’)
首先用一个等号将 dog 的值设置为‘小黄’,接下来再使用两个等号检查 dog 的值是否为‘小黄’。如果相等返回 True,不等返回 False。注意 Python 中检查相等时是区分大小写的。两个大小写不同的值会被 Python 视作不相等。
需要判断特定的值是否已经存在于某序列中,我们可以用关键词 in。判断是否不存在,用 not in
students = ['lilei','hanmeimei’]
print(‘madongmei’ in students) #返回
False
print(‘lilei’ in students) #返回 True
表达式的应用——多条件检查: 使用 and 检查多个条件 使用 or 检查多个条件
And 要都成立才是 ture,否则就是false,or 只要有一个成立就是 ture,都不成立才是false。
if 条件判断:
Python 代码缩进问题:用四个空格或者一个 Tab 来表示缩进都可以,但不要混用,相同缩进位置的代码表示他们是同一个代码块

注意判断相等用双等号== 单等号=是赋值

多重 if 语句 注意不同层级的条件判断互不影响

字符串里只能放字符”1”,而不能放数字 1,元组里可以存字符串、字符、数字、列表等任意数据类型
while 循环:当满足条件时一直执行里面的代码块,死循环

当用户没有正确输入时如何重新获取用户输入?用户输错一次可以,几次都输错呢?

while 循环扩展模式:while…else…:当 while 循环正常执行之后,程序会继续执行 else 语句中内容。else 语句只在循环正常执行之后才执行并结束。
break:强制终止当前循环,并且跳出去 continue:跳过这一次循环,继续从下一次循环开始
for 循环:按顺序选出来 students_list 列表中的一个值,赋予到 student 变量,然后执行循环内的语句循环数字
用 range()函数可以得到一个整数序列对象
for 循环扩展模式:for…else…:当 for 循环正常执行之后,程序会继续执行 else 语句中内容。else 语句只在循环正常执行之后才执行并结束。
单分支结构

双分支结构

多分支结构

每个条件是有关系的,只能执行其中一个。而下面的这一个就不一样了,输入数字后都有机会输入,都是单独的
if 结构

遍历循环 for:


所有的遍历循环都可以用无限循环去写。




网友评论