Python basics-what should I do if I always confuse the break, continue, pass, and exit keywords in python?

There are a large number of keywords in python. These keywords have their own special meanings and usages. However , for the keywords of break, continue, pass, and exit , some small partners may always confuse their usage and differences.
Next, we will go through the case Come and explain to explain...

break

for num in range(0,11):
	if num == 6:
		break
	print(num)

print("循环到此结束... ...")

operation result
Insert picture description here

braek: interrupt and terminate the loop that is closest to him, enter the outer loop to execute

continue

for num in range(0,11):
	if num == 6:
		# break
		continue
	print(num)

print("循环到此结束... ...")

operation result
Insert picture description here

Skip this cycle and execute the next cycle

pass

for num in range(0,11):
	if num == 6:
		# break
		# continue
		pass
	print(num)

print("循环到此结束... ...")

operation result
Insert picture description here

Act as a code question, in order to ensure that the grammar can pass normally

exit

import sys


for num in range(0,11):
	if num == 6:
		# break
		# continue
		# pass
		sys.exit()
	print(num)

print("循环到此结束... ...")

operation result
Insert picture description here

Terminate the execution of the program

to sum up

  • break, continue keywords
    break and continue keywords are used incycleThe function of the keyword
    break: terminate the loop (the nearest layer of loop to it)

  • continue function: skip this cycle and enter the next cycle

  • pass keyword:
    pass keyword can be usedAnywhere, Its function is currently not known how the code is implemented. For the
    time being, in order to ensure that the grammar can be passed normally, pass
    guarantees the grammatical integrity, and continue is not the same thing.

  • exit issys moduleThe method in is used to terminate the execution of the program...

Guess you like

Origin blog.csdn.net/XY0918ZWQ/article/details/111406852