current position:Home>Python 3 process control statement

Python 3 process control statement

2022-02-01 13:04:53 Work hard tomorrow

「 This is my participation 11 The fourth of the yuegengwen challenge 27 God , Check out the activity details :2021 One last more challenge

Python The code is executed in a top-down order .

Through process control statements , You can change the execution order of the program , You can also have the specified program execute repeatedly .

Process control statements are divided into two categories : Conditional statements , Loop statement .

1. Conditional statements (if sentence )

if sentence 
 grammar :if  Conditional expression  :   sentence 
 By default ,if Statement controls only the statement that follows , If you want to if Control multiple statements , Then you can if Followed by a code block ( Code blocks start with indents , Until the code returns to its previous indentation level );
 Two indentation methods :1. Use tab key ;2. Use four spaces 
if	True:
    print(123)
    print(456)
input(), This function is used to get the user's input ; After calling , The program will be suspended immediately , Waiting for user input .
	 When user input is complete , Click enter to continue the downward execution ;
     When user input is complete , Its input will be returned with a value ( character string ) Form return of ;
     Function can set a string as an argument , This string will be displayed as prompt text ;
    input() It can also be used to temporarily prevent the program from ending 
    a = input(' Please input anything :')
    print(' The user input is :',a)
if-else sentence 
 grammar :( Only one block of code is executed )
	if  Conditional expression  :
         Code block 
    else:
         Code block 
if-elif-else sentence 
 grammar :
	if  Conditional expression  :
         Code block 
    elif  Conditional expression  :
    	 Code block 
    elif  Conditional expression  :
       	 Code block 
    elif  Conditional expression  :
         Code block 
    else :
         Code block 
 Copy code 

2. Loop statement

while loop 
 grammar :while  Conditional expression  :
    	 Code block 
     else :
         Code block 
i = 0
while i < 10 :
    i +=1
    print(i)
else :
	print('else Code block in ')

break: Used to immediately exit a loop statement ( Include elsecontinue: Used to skip the current loop 
pass: Used to occupy a place in a loop or judgment statement 
i = 0
while i < 5 :
    i += 1
	if i == 2:
        continue
    print(i)
 Copy code 

Loop nested cases

 Print triangles 
python How to write it :
i = 0
while i < 5 :
  j = 0
  while j < i + 1 :
    print('*',end = '')
    j += 1
  print()
  i += 1
javaScript How to write it :
for (var i = 0; i < 5; i++) {
	for (var j = 0; j < i + 1;j++){
		document.write('*')
	}
	document.write('<br/>')
 Copy code 

copyright notice
author[Work hard tomorrow],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011304512558.html

Random recommended