#一、条件语句
1.if 语句
if expression: ←expression表达式可以通过布尔操作符and,or,not实现多重判断
expr_true_suite ←当expression为真时才执行,否则执行后面的语句
if 2 > 1 and not 2 > 3:
print('Correct Judgement!')
Correct Judgement!
- if-else语句
if expression:
expr_true_suite ←如果布尔值为False,执行else后的代码
else:
expr_false_suite
temp = input("猜一猜小姐姐想的时哪个数字")
guess = int(temp) # 将temp转换为Int,因为input 函数将接受的任何数据类型都默认为str
if guess == 666:
print("你太了解了!")
print("才对也么奖励")
else:
print("猜错了,是666")
print("游戏结束")
猜一猜小姐姐想的时哪个数字3
猜错了
游戏结束
#if支持嵌套,注意else的悬挂问题
hi = 6
if hi>2:
if hi>7:
print("great")
else:
print('hehe')
hi = 6
if hi>2:
if hi>7:
print("great")
else:
print('hehe')
hehe
temp = input("猜一猜小哥哥想的时哪个数字")
guess = int(temp)
if guess > 8:
print("大了")
else:
if guess == 8:
print("你真棒")
print("但是没奖励")
else:
print("小了")
print("游戏结束")
猜一猜小哥哥想的时哪个数字2
小了
游戏结束
- if-else-else语句
elif即else if
if expression1:
expr1_true_suite
elif expression2:
expr2_true_suite
elif expressionN:
exprN_true_suite
else:
expr_false_suite
temp = input('请输入成绩:')
source = int(temp)
if 100 >= source >= 90:
print('A')
elif 90 > source >= 80:
print('B')
elif 80 > source >= 60:
print('C')
elif 60 > source >= 0:
print('D')
else:
print('输入错误')
请输入成绩:80
B
4.assert关键词
“断言” assert后条件为False时,程序自动崩溃并抛出AssertionError异常
用来在进行单元测试时,在程序中置入检查点,只有条件为True才能让程序正常工作
my_list = ['lsgogroup']
my_list.pop(0)
assert len(my_list) > 0
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-10-3aa07d738e73> in <module>
1 my_list = ['lsgogroup']
2 my_list.pop(0)
----> 3 assert len(my_list) > 0
AssertionError:
assert 3 > 7
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-11-0295be16ec13> in <module>
----> 1 assert 3 > 7
AssertionError:
#二、循环语句
1.while语句
while循环的代码块会一直循环执行,知道布尔表达式的值为假
布尔表达式不带有< > == != in,not in等运算符,仅给出数值之类的条件,也可以
while后写入一个非零整数,视为真值,执行循环体
写入0时,视为假值,不执行循环体
while后也可以写入str,list或任何序列,长度非零则视为真值,执行循环体,否则视为假值
while 布尔表达式:
代码块
count = 0
while count < 3:
temp =input("不妨猜下你哥现在心里想的数字:")
guess = int(temp)
if guess > 85:
print("大了")
else:
if guess == 85:
print("wow")
print("你真棒")
count = 3
else:
print("小了")
count = count +1
print("游戏结束 拜拜")
不妨猜下你哥现在心里想的数字:7
笑了
不妨猜下你哥现在心里想的数字:23
笑了
不妨猜下你哥现在心里想的数字:85
wow
你真棒
游戏结束 拜拜
string = 'abcd'
while string:
print(string)
string = string[1:]
abcd
bcd
cd
d
2.while-else语句
while循环正常执行完的情况下,执行else输出,如果while循环中执行了跳出循环的语句,比如break,将不执行else代码块的内容
while 布尔表达式:
代码块
else:
代码块
count = 0
while count < 5:
print("%d is less than 5"%count)
count = count +1
else:
print("%d is not less than 5" % count)
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
3.for 循环
迭代循环,在Python中相当于一个通用的序列迭代器,可以遍历任何有序序列
如str、list、tuple等,也可以遍历任何可迭代对象,如dict
for 迭代变量 in 可迭代对象:
代码块
每次循环,迭代变量被设置为可迭代对象的当前元素,提供给代码块使用
for i in 'ILoveLSGO':
print(i,end=' ')
I L o v e L S G O
4.for-else 循环
for 迭代变量 in 可迭代对象:
代码块
else:
代码块
5.range()函数
range([start,] stop[,step=1])
这个BIF(Built-in functions)有三个参数,其中用中括号括起来的两个表示这两个参数时可选的
step=1 表示第三个参数的默认值为1
range 生成一个从start参数的值开始到stop参数的值结束的数字序列,该序列包含start的值但不包含stop的值。
6.enumerate()函数
enumerate(sequence,[start=0])
sequence:一个系列、迭代器或其他支持迭代选项
start–下标起始位置
返回enumerate(枚举)对象
用 enumerate(A) 不仅返回了 A 中的元素,还顺便给该元素一个索引值 (默认从 0 开始)。此外,用 enumerate(A, j) 还可以确定索引起始值为 j。
7.break语句
跳出当前所在曾的循环
8.continue语句
continue终止本轮循环并开始下一轮循环
9.pass语句
空语句,不做任何操作,只起到占位作用,为了保持程序结构的完整性
10.推导式
列表推导式
[expr for value in collection [if condition]]
元组推导式
( expr for value in colleciton [if condition])
字典推导式
{ key_expr: value_expr for value in collection [if condition]}
集合推导式
{ expr for value in collection [if condition]}
#11.综合例子
passwdList = ['123','345','890']
valid = False
count = 3
while count > 0:
password = input('enter password:')
for item in passwdList:
if password == item:
valid = True
break
if not valid:
print('invalid input')
count -=1
continue
else:
break
enter password:23
invalid input
invalid input
invalid input