Task02:条件循环结构
1、条件语句
1.1 if语句
if语句是通过一条或多条语句的执行结果 ( True 或者 False ) 有选择性的执行的代码块,在Python 语言中,任何非 0 和非空 ( null ) 值为 True,0 或者 null 为 False。
if 判断条件:
执行语句……
- 当条件表达式 expression 结果为真时,if 语句的 expr_true_suite 代码块才执行,否则将跳过expr_true_suite 代码块,执行expr_true_suite 代码块后面的语句。
- 单个 if 语句中的 expression 条件表达式可以通过布尔操作符 and , or 和 not 实现多重条件判断,也可以使用>、>=、<、<=和!=实现条件判断。
if 5 > 4 and not 1 > 3:
print('True!')# True!
if 5 > 4 and not 6 > 3:
print('True!');
print('False!') # False!
1.2 if-else语句
if-else语句
if 判断条件:
执行语句……
else:
执行语句……
Python 提供与 if 搭配使用的 else,如果 if 语句的条件表达式结果布尔值为假,那么程序将执行 else 语句后的代码。
if 语句支持嵌套,即在一个 if 语句中嵌入另一个 if 语句,进而构成不同层次的选择结构。Python 使用缩进而不是大括号来标记代码块边界,因此需要注意 else 的悬挂问题。
score =89
if score>=90:
print ('优秀')
else :
print ('良') # 良
1.3 if-elif-else语句
if-elif-else语句
elif 语句为 else if,用于检查多个表达式是否为真,并在为真时,执行特定代码块中的代码。
if 判断条件1:
执行语句1……
elif 判断条件2:
执行语句2……
elif 判断条件3:
执行语句3……
else:
执行语句4……
在多分支条件语句,使用elif子句可以设置更多的条件。elif后面可以是逻辑表达式,也可以是代码块,所以后面要用冒号(:)结尾。另外,在if语句中,if和else部分只能有一个,而elif部分可以有任意多个。
score = 75
if score > 90:
print('优秀')
elif score > 80:
print('良好')
elif score > 70:
print ('中') #中
elif score > 60:
print('及格')
else :
print ('不及格')
2、循环语句
2.1 while循环
while循环在给定的判断条件为 true 时执行循环体,否则退出循环体。
count = 0
while count < 3:
temp = input("输入数字:")
guess = int(temp)
if guess > 7:
print("大")
elif guess == 7:
print("猜对啦")
count = 3
else:
print("小")
count = count + 1
print("Game Over!")
举例:布尔表达式返回0,循环终止。
string = 'hello'
while string:
print(string)
string = string[1:]
#hello
#ello
#llo
#lo
#o
2.2 while-else循环
while … else 在循环条件为 false 时执行 else 语句块
while 布尔表达式:
代码块1
else:
代码块2
num = 6
while num < 10:
print("%d is less than 10" % num)
num = num + 1
else:
print("%d is not less than 10" % num)
#6 is less than 10
#7 is less than 10
#8 is less than 10
#9 is less than 10
#10 is not less than 10
如果 while 循环中执行了跳出循环的语句,比如 break ,将不执行 else 代码块的内容。
num = 6
while num < 10:
print("%d is less than 10" % num)
num = 11
break
else:
print("%d is not less than 10" % num)
#6 is less than 10
2.3 for循环
for 循环是迭代循环,在Python中相当于一个通用的序列迭代器,可以遍历任何有序序列,如 str、list、tuple等,也可以遍历任何可迭代对象,如 dict 。
for 迭代变量 in 可迭代对象:
代码块
每次循环,迭代变量被设置为可迭代对象的当前元素,提供给代码块使用
def deduplication(self, nums):#找出排序数组的索引
for i in range(len(nums)):
if nums[i]==self:
return i
i=0
for x in nums:
if self>x:
i+=1
return i
print(deduplication(4, [1,2,3,4])) # 3
member = ['张三', '李四', '王二', '刘六',]
for j in member:
print(j)
# 张三
# 李四
# 王二
# 刘六
for i in range(len(member)):
print(member[i])
# 张三
# 李四
# 王二
# 刘六
2.4 for - else 循环
当 for 循环正常执行完的情况下,执行 else 输出,如果 for 循环中执行了跳出循环的语句,比如 break ,将不执行 else 代码块的内容,与 while - else 语句一样。
for 迭代变量 in 可迭代对象:
代码块
else:
代码块
for num in range(10, 20): # 迭代 10 到 20 之间的数字
for i in range(2, num): # 根据因子迭代
if num % i == 0: # 确定第一个因子
j = num / i # 计算第二个因子
print('%d 等于 %d * %d' % (num, i, j))
break # 跳出当前循环
else: # 循环的 else 部分
print(num, '是一个质数')
# 10 等于 2 * 5
# 11 是一个质数
# 12 等于 2 * 6
# 13 是一个质数
# 14 等于 2 * 7
# 15 等于 3 * 5
# 16 等于 2 * 8
# 17 是一个质数
# 18 等于 2 * 9
# 19 是一个质数
2.5 range函数
range([start,] stop[, step=1])
- 这个BIF(Built-in functions)有三个参数,其中用中括号括起来的两个表示这两个参数是可选的。
- step=1 表示第三个参数的默认值是1。
- range 这个BIF的作用是生成一个从 start 参数的值开始到 stop 参数的值结束的数字序列,该序列包
含 start 的值但不包含 stop 的值。