while语句,for语句、Break语句、range语句,count方法。
while 循环
for循环
while语句
作用:
根据一定条件,重复执行一条语句或多条语句
语法:
while 真值表达式:
语句块一(此部分的语句肯或重复执行多次)
else:
语句块二
说明:1、先执行真值表达式,得到布尔值为true或false
2、如果真值表达式的值为false,则执行语句块一,然后再返回第一步,重复进行测试真值表达式的值
3、如果真值表达式的值为false,则执行else子句部分语句块二,然后结束while语句的执行
注意:else语句可以省略(同if语句类似)
while语句注意事项:
1、创建并初始化一个控制while循环的变量
2,、通过真值表达式来控制循环体
3、通过循环变量来控制循环条件,防止死循环
练习:
1、用while循环打印1~20的整数
def circle(x): while x<=20: print(x) x+=1 circle(1)
2、用while打印1~20,打印在一行显示,每个数字之间用一个空格
def circle(x):
while x<=20:
print(x,' ',end='')
x+=1
circle(1)
3、用while打印1-20,四行,一行五个
def circle(x):
while x<=20:
print('%2d'%x,' ',end='')
if x%5 == 0:
print('\n')
x += 1
circle(1)
while 循环嵌套
while语句本身就是语句,和其他语句一样,可以嵌套到,任何复合语句中
语法:
while 真值表达式:
··· ···
while 真值表达式:
··· ···
例题、嵌套打印倒三角
def angle1(line): lin = 0 while lin <= line: print('*'*lin) lin = lin + 1 def angle(line): while line > 0: print('*'*line) line = line - 1 def angle2(line): while line > 0: print('*'*5) line = line -1 angle1(5) print() angle(5) print() angle2(5)
其实下面才是嵌套。。
def angle4(line): while line > 0: i = line while i >0: print('*',end='') i=i-1 else: print() line = line - 1 print() angle4(5)
练习:输入一个整数n,打印指定宽度正方形,输入5 打印 5 行1 2 3 4 5
def angel5(x): t = x while x > 0: i = 0 while i <= t: print(i,' ',end='') i += 1 print() x -= 1 angel5(8) 其实这里打印了八行 结果: 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8
打印九九乘法表
def angle6(): x=1 while x <= 9: y = 1 while y <= x: print(x,'*',y,'=',x*y,end=' ') y += 1 print() x += 1 angle6() 运行结果: 1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
下面这个一样的功能
for i in range(1,10): for j in range(1,i+1): print(j,'*',i,'=',i*j,end=' ') print()
break语句
问题:
如果在循环过程中不想再继续循环语句的执行了,怎么办?
可以用break语句结束循环
作用:
用于循环语句(while,for语句中),用来终止当前循环语句
说明:
当break语句执行后,此部分语句不载执行
当break语句通常和if语句组合使用
break语句终止循环时,循环语句的else 子句的语句不会执行
break语句只能终止当前循环语句的执行,如果有嵌套循环时,不会跳出,嵌套的外重循环
break语句只能在break和for中使用
for循环
作用:
用来遍历可迭代对象的数据元素
可迭代对象是指能一次获取数据元素的对象
语法:
for 变量列表 in 可迭代对象
语句块1(此处为可重复执行的语句块)
else:
语句块2
说明:
当前循环体内用break终止循环时,else子句部分的语句不会执行else子句部分可以省略。
ii = 1 while ii<100: print('循环开始时 ii = ',ii) if ii == 25: print('循环结束时ii = ',ii) break ii += 1 else: print('我是while语句的else语句的print') print('语句即将结束,ii = ',ii) 运行后: ················ 循环开始时 ii = 23 循环开始时 ii = 24 循环开始时 ii = 25 循环结束时ii = 25 语句即将结束,ii = 25
range()函数
range(start ,stop)用来生成0~stop区间内的整数,直到stop停止为止(不包含stop)
range(start,stop[,step])用来生成start~stop区间内的整数,直到stop为止(不包含stop),每个整数间隔step步长
作用:
用来创建 一个生成一系列整数的可迭代对象(页脚序列生成器)
练习:
1、写程序,任意输入一行字符串,打印这个字符串内有多少空格(要求用for循环,不能用S.count方法),思考,用while循环实现吗?
2、输入一个整数,代表结束值,求1+2+3+4+5.....+N的值
#这个是for的
def countspace(str): j = 0 for i in str: if i == ' ': j += 1 print('这个字符串',str,'中有',j,'个空格') countspace('asd asd sdj as df d g ') 运行结果 : 这个字符串 asd asd sdj as df d g 中有 8 个空格
#这个是while的
def countspace1(str): j = 0 i = 0 while i < len(str): if str[i] == ' ': j += 1 i += 1 print('这个字符串',str,'中有',j,'个空格') countspace1('asd asd sdj as df d g ')
#求1~N的值 def countnum(n): i = 0 j = 0 for i in range(1,n+1): print(i,end=' ') j = i + j print(j) countnum(6) 运行结果: 1 2 3 4 5 6 21 Process finished with exit code 0
#这个是用count方法,计算空格数。 from collections import Counter ssct = 'asd asd sdj as df d g ' print(Counter(ssct)[' '])