• python while 循环语句


    1.while 循环结构

    特征:减少代码冗余,提升代码效率
    语法:
    while 条件表达式:
    	code1
    	code2
    	...
    	
    1.初始化一个变量
    2.写上循环的条件表达式
    3.自增自减的变量值
    

    案例1:打印1~100

    i = 1
    while i<=100:
    	# 要执行的逻辑 ... 
    	print(i)
    	i += 1
    
    """
    初始化一个变量 i
    
    第一次循环:
    i <= 100 条件成立,执行循环
    直接打印 print(i) => 1
    i += 1 => i = 2
    
    第二次循环:	
    回到17行,重新回到判断,看一看是否满足条件
    2 <= 100 条件成立,执行循环
    直接打印 print(i) => 2
    i += 1 => i = 3
    
    第三次循环:
    回到17行,重新回到判断,看一看是否满足条件
    3 <= 100 条件成立,执行循环
    直接打印 print(i) => 3
    i += 1 => i = 4
    
    ...
    ...
    依次类推进行循环 ... 
    
    到什么时候结束?
    当打印100 结束之后 
    i += 1 => i = 101 回到循环的判断条件中
    发现101 <= 100 不满足条件,不成立,不执行循环体代码块
    到此为止,循环直接结束.
    
    """
    

    案例2 : 打印1~100的累加和?1+2+3+4+ .....+100 ?

    #方法一
    total 负责做累加和
    total = 0
    i = 1
    while i<=100:
    	total += i
    	i+=1
    
    print(total)
    
    """
    # 代码解析:
    
    第一次循环:
    1 <= 100 条件为真 , 执行循环
    total += i => total = total + i => total = 0 + 1
    i+=1 => i=2
    
    
    第二次循环:
    回到条件表达式中进行判断 
    2<=100  条件为真 , 执行循环
    total += i => total = total + i => total = 0 + 1 + 2
    i+=1 => i=3
    
    第三次循环:
    回到条件表达式中进行判断 
    3<=100  条件为真 , 执行循环
    total += i => total = total + i => total = 0 + 1 + 2 + 3
    i+=1 => i=4
    
    ...
    ...
    依次循环
    
    
    什么时候结束?
    total = 0 + 1 + 2 + 3 + ... + 100
    i+=1 => i = 101
    回到条件表达式中进行判断 
    101 <= 100 条件不成立,不执行循环体;
    直接终止循环;
    
    print(total)  5050
    """
    
    # 方法二
    # 1+2+3+4+ .....+100 ? 用while True 的方式实现;
    total = 0 
    i = 1
    sign = True
    while sign:
    	total += i
    	i+=1
    	
    	# 手动加上终止的条件
    	if i == 101:
    		sign = False
    
    print(total)
    # 这种情况下就不执行循环了;用这样的方式终止循环;
    while False:
    	print(111)
    

    2.死循环

    while True:
    	print(123)
    

    (1)单循环练习:

    1.打印一行十个小星星

    i = 1
    while i <= 10:
    	print("★",end="")
    	i += 1
    

    2.用一个变量打印出一行十个小星星

    i = 0
    strvar = ""
    while i<10:
    	# 循环10次,拼接10个星星,每循环一次拼接一个星星
    	strvar += "★"
    	i+=1
    print(strvar)
    

    3.打印一行十个小星星 奇数个打印★ 偶数个打印☆

    i = 1
    while i <= 10:
    	if i % 2 == 0:
    		print("☆",end='')
    	else:
    		print("★",end="")
    	i += 1
    
    #方法二
    print("-----分割线-------")
    count = 1
    str_var = ""
    while count <= 10:
    	if count % 2 == 0:
    		str_var += "☆"
    	else:
    		str_var += "★"
    	count += 1
    print(str_var)
    

    4.一个循环,打印十行十列小星星

    #循环100和星星
    i = 1
    while i <= 100:
    	#1.打印小星星
    	print("★",end='')
    	#2.打印换行
    	if i % 10 == 0:
    		print()
    	i += 1
    

    5.一个循环 打印十行十列隔列变色小星星

    #方法一
    i = 0
    while i < 100:
    	if i % 2 == 0:
    		print("☆",end="")
    	else:
    		print("★",end='')
    	if i % 10 == 9:
    		print()
    	i += 1
    
    #方法二
    _str_var = ""
    count = 1
    while count <= 100:
    	if count % 2 == 0:
    		_str_var += "☆"
    	else:
    		_str_var += "★"
    	if count % 10 == 0:
    		_str_var +="
    "
    	count += 1
    print(_str_var)
    

    6.一个循环 打印十行十列隔行变色小星星

    i = 0
    while i < 100:
    	#打印星星
    	if i // 10 %2 == 0:
    		print("☆", end="")
    	else:
    		print("★",end="")
    	if i % 10 == 9:
    		print()
    	i += 1
    

    (2)pass break continue

    1.pass 过 (当不能直接在代码块中写上具体代码时,先用pass占位;)

    num = 10
    if num == 10:
    	pass
    
    
    i = 0
    while i<10:
    	pass
    	i+=1
    

    2.break 终止当前循环,只能用在循环当中

    # 打印1~10 ,遇到5就终止循环
    # 单循环 (终止当前循环)
    i = 1
    while i<=10:
    	
    	if i == 5:
    		break
    		
    	print(i)
    	
    	i+=1
    

    多循环 (终止当前循环)

    i = 0 
    while i<3:
    	j = 0
    	while j<3:
    		if j ==2:
    			break
    		print(i,j)
    		j+=1
    	i+=1
    # 不能再循环外使用break
    """
    if 5==5:
    	print(111)
    	break
    """
    

    3.continue 跳过当前循环,从下一次开始 ,只能用在循环当中

    打印1 ~ 10 不包括5
    i = 1
    while i<=10:
    	
    	if i == 5:
    		# 手动加1 , 防止跳过下面代码,忽略自增形成死循环;
    		i+=1
    		continue
    	print(i)
    	i+=1
    

    打印1~100 所有不含有4的数字;

    """
    // 地板除可以取一个数的高位
    %  取余  可以取一个数的低位
    
    任意数 // n 会产生n个相同的数字
    任意数 %  n 取值范围是0~(n-1)
    """
    # 方法一
    i = 1
    while i<=100:
    	
    	if i % 10 == 4 or i // 10 == 4:
    		# 手动自增1,防止死循环
    		i+=1
    		continue
    	print(i)
    	i+=1
    	
    
    # 方法二 in 
    i = 1
    while i<=100:
    	strvar = str(i)
    	if '4' in strvar:
    		i+=1
    		continue
    		
    	print(i)
    	i+=1
    

    (3) 双层循环练习

    1.十行十列小星星

    #j动1次,i动10次,外层循环动的慢.内层循环动的快
    j = 0
    while j<10:
    	#打印星星
    	i = 0
    	while i < 10:
    		print("*",end='')
    		i += 1
    	# 打印换行
    	print()
    
    	j += 1
    

    2 双层 十行十列隔列换色小星星

    j = 0
    while j<10:
    	#打印一行十个星星
    	i = 0
    	while i < 10:
    		if i % 2 == 0:
    			print("☆",end='')
    		else:
    			print("★",end='')
    		i += 1
    	#打印一个换行
    	print()
    	j += 1
    

    3双层 十行十列隔行换色小星星

    """
    当j = 0 时,在内存循环,循环了10次 ☆
    当j = 1 时,在内层循环,循环了10次 ★
    """
    print("------分割线------")
    j = 0
    while j < 10:
    	#打印一行十个小星星
    	i = 0
    	while i < 10:
    		if j % 2 == 0:
    			print("★",end='')
    		else:
    			print("☆",end='')
    		i += 1
    	#打印一个换行
    	print()
    	j += 1
    

    4.打印99乘法表

    """
    外层的i控制行 里层的j控制列
    当前j跟当前行数有关系,不能大于当前行 所以j <= i
    """
    #方法一(正序 1~9)
    i = 1
    while i <= 9:
    	j = 1
    	while j <= i:
    		#字符串的格式化 让最后的乘积占用2位,据右边显示;
    		print("%s*%s =%2s"%(i,j,i*j),end=" ")
    		j += 1
    	print()
    	i += 1
    """
    '字符串'%(值1,值2,值3,......)
    %s 字符串占位符
    res = "%s * %s = ?"% (5,6)
    print(res)
    """
    # 方法二(倒序 9 ~1)
    i = 9
    while i >= 1:
    	#字符串的格式化 让最后的乘积占用2位,据右边显示;
    	j = 1
    	while j <= i:
    		#字符串的格式化 让最后的乘积占用2位,据右边显示;
    		print("%d*%d=%2d"% (i,j,i*j),end=' ')
    		j += 1
    	#打印换行
    	print()
    	i -= 1
    
    # 方法三
    i = 1
    while i <= 9:
    	#(1)先打印空格
    	k = 9 - i
    	while k > 0:
    #	"""打印空格"""
    		print("      ",end="")
    		k -= 1
    
    	#print("       "*(9-i),end="")
    	#(2)打印表达式
    	j = 1
    	while j <= i:
    		print("%d*%d=%2d"% (i,j,i*j),end="")
    		j += 1
    	#(3)打印换行
    	print()
    	i +=1
    
    print("-----分割线-----")
    #方法四
    i = 9
    while i > 0:
    	k = 9 - i
    	while k > 0:
    		print("         ",end='')
    		k -= 1
    	# print("       "*(9-i),end='')
    	# (2) 打印表达式
    	j = 1
    	while j <= i:
    		#字符串的格式化 让最后的乘积占用2位,据右边显示;
    		print("%d*%d = %2d"% (i,j,i*j),end=' ')
    		j += 1
    	#打印换行
    	print()
    	i -= 1
    

    5 求吉利数字100 ~ 999 666 888 111 222 333 444 ... 123 789 567 987 321 543 210

    """
    // 地板除可以获取一个数的高位
    % 取余 可以获取一个数的低位
    
    98 98 % 10 => 8
    98 98 // 10 => 9
    
    678
    个位 = 678 % 10 => 8
    十位 = 678 // 10 %10 => 7
    百位 = 678 // 100 => 6
    """
    #方法一
    i = 100
    while i <= 999:
    	gewei = i % 10
    	shiwei = i // 10 % 10
    	baiwei = i // 100
    
    	#gewei = shiwei = baiwei 三者相同
    	if shiwei == gewei and shiwei ==baiwei:
    		print(i)
    
    	#678
    	if shiwei == gewei -1 and shiwei == baiwei + 1:
    		print(i)
    
    	#876
    	if shiwei == gewei + 1 and shiwei == baiwei - 1:
    		print(i)
    	i += 1
    
    #方法二 字符串形式写
    print("-----分割线-------")
    i = 100
    while i<= 999:
    	num = str(i)
    	#"987"
    	geiwei = int(num[-1]) #7
    	shiwei = int(num[1]) #8
    	baiwei = int(num[0]) #9
    
    	if shiwei == gewei and shiwei == baiwei:
    		print(i)
    
    	if shiwei == gewei - 1 and shiwei == baiwei + 1:
    		print(i)
    
    	if shiwei == gewei + 1 and shiwei == baiwei - 1:
    		print(i)
    
    	i += 1
    

    经典题: 百钱买百鸡

    """
    公鸡,母鸡,小鸡,公鸡1块钱一只,母鸡3块钱一只,小鸡是5毛钱一只,问100块钱买100只鸡,有多少种买法
    穷举法: 一个一个拿出来试
    x [1,2]
    y [3,4]
    z [5,6]
    满足 x+y+z = 10
    
    1 + 3 + 5 = 9
    1 + 3 + 6 = 10 ok
    
    1 + 4 + 5 = 10 ok
    1 + 4 + 6 = 11 
    
    2 + 3 + 5 = 10 ok
    2 + 3 + 6 = 11
    
    2 + 4 + 5 = 11
    2 + 4 + 6 = 12
    """
    
    #公鸡x只,母鸡y只,小鸡z只
    #x+y+z = 100 #一百只鸡
    #x+3+y*1+0.5*z = 100
    #两个条件同时满足
    x = 0
    total = 0
    while x<=100:
    	y = 0
    	while y<= 33:
    		z = 0
    		while z <= 100:
    			if(x+y+z == 100) and (x+3*y+0.5*z == 100):
    				total += 1
    			z += 1
    		y += 1
    	x += 1
    print(total)
    
  • 相关阅读:
    MemCached总结二:数据管理指令
    MemCached总结一:Unbutu操作系统下memcached服务器安装和telnet方式连接memcache
    Laravel5 开启Debug
    状压dp
    树形dp
    区间dp
    线性dp
    背包九讲
    dp求解各种子串子序列
    线段树详解
  • 原文地址:https://www.cnblogs.com/CrownYP/p/11341941.html
Copyright © 2020-2023  润新知