0710Python summary -while bidirectional loop, pass break continue, for loop

I Classic practice of two-way circulation

1. print ten rows and ten columns of small stars (2 cycles)

# j outer loop is used to control the number of rows
j = 0
while j<10:

	# Print a line of ten stars
	i = 0
	while i <10:
		# Write the logic of the loop
		print("*",end="")
		i+=1
		
	# Print wrap
	print()

	j+=1

2. print ten rows and ten columns of small stars (alternate columns of small stars, two cycles)

i = 0
while i<10:
	# Print alternate color stars
	j = 0
	while j < 10:
		if j % 2 == 0:
			print("★",end="")
		else:
			print("☆",end="")
		j+=1

	# Print wrap
	print()
	i+=1

3. print ten rows and ten columns of small stars (alternate color of small stars, two cycles)

"""Outer layer circulates once,Inner layer circulates for 10 times,Slow circulation of outer layer,Inner circulation is fast"""
i = 0
while i<10:
	# Print alternate color stars
	j = 0
	while j < 10:
		if i % 2 == 0:
			print("★",end="")
		else:
			print("☆",end="")
		j+=1

	# Print wrap
	print()
	i+=1

4.99 multiplication table

Mode 1
# i control line
i = 1
while i <= 9:
	# j control column
	# Print expression
	j = 1
	while j<=i:
		# Print expression who * who = who%2d is displayed on the right by default
		print("%d*%d=%2d " % (i,j,i*j),end="")
		j+=1
	
	# Print wrap
	print()
	i+=1
Mode II
# i control line
i = 9
while i >= 1:
	# j control column
	# Print expression
	j = 1
	while j<=i:
		# Print expression who * who = who%2d is displayed on the right by default
		print("%d*%d=%2d " % (i,j,i*j),end="")
		j+=1
	
	# Print wrap
	print()
	i-=1
Mode III
i = 1
while i<=9:
	
	# 1. print spaces
	"""
	The first line loops through 8 groups of spaces 8~1 Is a cycle of 8 groups of spaces
	The second line circulates 7 groups of spaces 7~1 Is a cycle of 7 groups of spaces
	...
	Line 8 loop 1 group of spaces 1 is loop 1 group of spaces
	The ninth line doesn't need a space, so k>0
	"""
	k = 9 - i
	while k>0:
		print("       " , end="")
		k -= 1 

	# 2. print expression 
	j = 1
	while j<=i:
		# Print expression who * who = who%2d is displayed on the right by default
		print("%d*%d=%2d " % (i,j,i*j),end="")
		j+=1
		
	# 3. print line breaks
	print()
	
	i+=1
Mode 4
i = 9
while i>=1:
	
	# 1. print spaces
	"""
	The first line loops through 8 groups of spaces 8~1 Is a cycle of 8 groups of spaces
	The second line circulates 7 groups of spaces 7~1 Is a cycle of 7 groups of spaces
	...
	Line 8 loop 1 group of spaces 1 is loop 1 group of spaces
	The ninth line doesn't need a space, so k>0
	"""
	k = 9 - i
	while k>0:
		print("       " , end="")
		k -= 1 

	# 2. print expression 
	j = 1
	while j<=i:
		# Print expression who * who = who%2d is displayed on the right by default
		print("%d*%d=%2d " % (i,j,i*j),end="")
		j+=1
		
	# 3. print line breaks
	print()
	
	i-=1

5. find the Geely number 100 ~ 999666888111222333444... 12378956765432

Bits: 987% 10 = 7
Ten digit: 987 / / 10% 10 = 8
Hundredth: 987 / / 100 = 9

Method 1
i = 100
while i<1000:
	gewei = i % 10
	shiwei = i // 10 % 10
	baiwei = i // 100
	
	if gewei == shiwei and shiwei == baiwei:
		print(i)
		
	#123
	elif shiwei  == gewei-1 and  shiwei == baiwei+1:
		print(i)
		
	#765
	elif shiwei == gewei+1 and shiwei == baiwei-1:
		print(i)
	i+=1
Method II
i = 100
while i<1000:
	strvar = str(i) # "123"
	baiwei = int(strvar[0])
	shiwei = int(strvar[1])
	gewei = int(strvar[-1])
	if gewei == shiwei and shiwei == baiwei:
		print(i)
		
	#123
	elif shiwei  == gewei-1 and  shiwei == baiwei+1:
		print(i)
		
	#765
	elif shiwei == gewei+1 and shiwei == baiwei-1:
		print(i)
	i+=1

6. a hundred dollars for a hundred chickens

Cock x hen y chick z
A rooster costs 1 yuan, a hen costs 3 yuan and a chick costs 50 cents
Q: how many ways to buy 100 chickens for 100 yuan?
Both conditions are met at the same time
x+y+z = 100
x1 + y3 + z0.5 = 100*

# cock
x = 0
count = 0
while x <= 100:

	# y hen
	y = 0
	while y <= 33:
		
		# z chicken
		z = 0
		while z <= 100:
			if x+y+z == 100 and x*1 + y*3 + z*0.5 == 100:
				count += 1
				print(x,y,z)
			z+=1
	
		y+=1

	x += 1

print(count)

II Keyword use pass break continue

1.pass (occupied)

if 10 == 10:
	print(123)
	
while True:
	pass

2.break terminate the current cycle

# Print 1~10 and terminate the cycle when 5 is encountered
i = 1
while i <=10 :
	if i == 5:
		break
	print(i)
	i+=1

break terminates the current loop

i = 1
while i <= 3:
	j = 1
	while j<=3:
		if j == 2:
			break
		print(i,j)
		j+=1
	i+=1

3.continue skip the current cycle and start from the next cycle

Print 1~10 Skip 5
i = 1
while i<=10:

	if i == 5:
		# Need to manually add 1 When continue is executed, the following code is not executed and jumps to the condition judgment of the while loop, resulting in an endless loop
		i+=1
		continue
	print(i)

	i+=1

Print all numbers from 1 to 100 that do not contain 4
//Can get the high order of a number
%Can get the low order of a number
46 // 10 = 4
46 % 10 = 6

Method 1
i = 1
while i<=100:
	if i // 10 == 4 or i % 10 == 4:
		i+=1
		continue
	print(i)
	i+=1
Method II
i = 1
while i <= 100:
	strvar = str(i)
	if "4" in strvar:
		i+=1
		continue

	print(i)
	i+=1

III for loop

Traversal, loop, iteration

lst = ["Liu Xin","Liu Zihao","Liuzitao","Yanguozhang"]
i = 0
while i<len(lst):
	# Logic of code
	print(lst[i])
	i+=1
# for is mainly used to traverse data. while has limitations when traversing data
for variable in Iteratible object:
	code1
	code2
 Iteratible object(Container type data,range object,iterator )
Traversal string

container = "thunder GABA, ZBC, ruthless Hala Shao"

Traversal list

container = ["Liu Xin", "liuzihao", "liuzitao", "Yan Guozhang"]

Traversal tuple

container = (1,2,3,45)

Traversal set

container = {"white star", "Gaofeng peak", "sunzhihe", "Liu Peng", "Mu Shuren"}

Traverse the dictionary (only traverse the key when traversing the dictionary)

container = {"ww": "great man, romantic", "msr": "tree man, great man", "mh": "wretched old man"}

for i in container:
	print(i)
Traversing unequal length secondary containers
container = [["Liu Cong","Maohonglei","Yurui","Zhangjiahao"],("Caiwenji","Zhang Jie")]

for i in container:
	# print(i)
	for j in i:
		print(j)
Unpacking of variables
a,b = 1,2
a,b = [3,4]
a,b = {"a":5,"b":6}
print(a,b)
Traversing equal length secondary containers
container = [("Wang Jianlin","Wangsicong","Meili Wang") , ("Mayun","pony ","Ma Shengping") , ("Wangbaoqiang","MaRong","Songxiaobao")  ]
for a,b,c in container:
	print(a,b,c)
# a. B, C = ("wangjianlin", "wangsicong", "wangmeili")
# a. B, C = ("Ma Yun", "Ma Huateng", "Ma Shengli")
# a. B, C = ("wangbaoqiang", "Ma Rong", "songxiaobao") 

range object

Range (start value, end value, step size)
The end value itself cannot be obtained. The number before the end value is obtained

Only one value 0~9

for i in range(10):
	print(i)

Only two values

for i in range(3,11):
	print(i)

Only three values

for i in range(1,10,3):
	print(i)  # 1,4,7
#1 (1+3) =>4 (4+3) =>7 (7+3) =>10 cannot be obtained

Reverse order printing 10 ~ 1

for i in range(10,0,-1):
	print(i)
# 10 9 8 7 6 .... 1

summary

while: complex logic
for: data traversal
while and for codes can be converted to each other

Distinctive writing 1
i = 1
while i<=10:
	if i == 5:
		i+=1
		continue
	print(i)
	i+=1

for i in range(1,11):
	if i == 5:
		continue
	print(i)
Distinctive writing 2
i = 1
while i <= 9:
	j = 1
	while j<=i:
		print("%d*%d=%2d " % (i,j,i*j),end="")
		j+=1
	print()
	i+=1


for i in range(1,10):
	for j in range(1,i+1):
		print("%d*%d=%2d " % (i,j,i*j),end="")
	print()

Tags: Python

Posted by whitepony6767 on Sun, 29 May 2022 22:52:28 +0530