程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] use of conditional statements, loop statements and pass statements

編輯:Python

Catalog

One 、 Conditional statements

1、if……else…… sentence  

1) Single branch

2) Double branch  

2、if……elif……else sentence

3、 multiple if nesting

Two 、 Loop statement

1、for loop

1) Calculation 1+2+3+……+100 The sum of

2) Find out if a number is in a list

2、while loop

1) Calculation 1+2+3+……+100 The sum of

 2) Find out if a number is in a list

3、 ... and 、pass sentence


One 、 Conditional statements

  • Conditional statements can change Python Procedure execution flow , Whether to execute this code block or another code block .
  • Any program that needs judgment to determine how to execute the next step should use conditional statements .

         The general conditional statements are

1、if……else……

2、if……elif……else

3、 multiple if nesting

1、if……else…… sentence  

1) Single branch

  • A single branch is actually just one if sentence , If if The following conditional expression is true , Then execute this if Subsequent statements

Scene one

        Suppose Xiao Ming bought an apple , If apple If it is bad, contact the merchant to change it .

Code example

apple = "good"
if apple == "good":
print("apple is {}".format(apple))
if apple == "bad":
print("apple is {}".format(apple))
print(" You need to contact the merchant to change it ")

Output

         Apple on top Is a good output , If the apple is bad, The output is as follows

2) Double branch  

  • Double branch is to execute the corresponding statement if the conditional expression is true , Otherwise, execute another statement (else Subsequent statements )

          Or the scene above , If you use a single branch, you need to write multiple if To judge , It will be easier to use double branch

Code example

apple = "bad"
if apple == "good":
print("apple is {}".format(apple))
else:
print("apple is {}".format(apple))
print(" You need to contact the merchant to change it ")

Output

2、if……elif……else sentence

  • There are only two situations in the above scenario , If there are many situations, you can use if……elif……else Statement .

Scene two

         Suppose that the full score of a certain subject is 100 branch ,60 It is divided into the following D level ,60 branch ~74 It is divided into C level ,75 branch ~89 It is divided into B level ,90 The above points are A level . If Xiao Ming gets the exam 80 branch , Print his grades and corresponding grades .

Code example

score = 80
if score < 60:
print("score = {}".format(score), " by D level ")
elif 60 <= score <= 74:
print("score = {}".format(score), " by C level ")
elif 75 <= score <= 89:
print("score = {}".format(score), " by B level ")
else:
print("score = {}".format(score), " by A level ")

Output

3、 multiple if nesting

  • if Statements can also be used if sentence , This constitutes if Nesting of statements

         Or scene 2 above , Use multiple if Nesting is written as follows

Code example

score = 80
if score < 75:
if score >= 60:
print("score = {}".format(score), " by C level ")
else:
print("score = {}".format(score), " by D level ")
else:
if score <= 89:
print("score = {}".format(score), " by B level ")
else:
print("score = {}".format(score), " by A level ")

Output

Two 、 Loop statement

         Loop statements have  for loop   and  while loop  , Generally speaking , Use a fixed number of cycles for Circulation and while Loops can solve , However, the cycle problem with variable cycle times can only be used while Cycle solution .

1、for loop

1) Calculation 1+2+3+……+100 The sum of

Code example

total = 0
for i in range(1, 101): # range(1, 101) The values for 1~100
total += i
print(" Sum to :{}".format(total))

Output

2) Find out if a number is in a list

Code example

numList = [2, 4, 6, 8, 10, 11, 13, 15]
num = 10
for i in numList:
if num == i:
print("num stay numList Inside ")
break
else:
print("num be not in numList Inside ")

Output

         It says that bug, That is, if it is not equal, it will be printed once num be not in numList Inside , It will not print until it is equal num stay numList Inside And exit the loop , It can be written as follows .

Code example

numList = [2, 4, 6, 8, 10, 11, 13, 15]
num = 10
for i in numList:
if num == i:
print("num stay numList Inside ")
break
else:
print("num be not in numList Inside ")

Output

  

2、while loop

1) Calculation 1+2+3+……+100 The sum of

Code example

total = 0
index = 1
while 1:
if index > 100:
break
total += index
index += 1
print("total = {}".format(total))

Output

 

 2) Find out if a number is in a list

Sample code

numList = [2, 4, 6, 8, 10, 11, 13, 15]
num = 15
index = 0
flag = False
while index < len(numList):
if num == numList[index]:
flag = True
break
index += 1
if flag:
print("num stay numList Inside ")
else:
print("num be not in numList Inside ")

Output

3、 ... and 、pass sentence

  • pass It's an empty statement , To maintain the integrity of the program structure
  • pass Not doing anything , Generally used as occupation statement

Sample code

sex = " male "
if sex == " male ":
print(" He's a man ")
else:
pass

Output

         In other words, if you don't know what needs to be done after the program , You can use pass Statement to maintain the integrity of the program structure .

The end ……

Originality is not easy. , Reprint please indicate the source .

If it's helpful to you, you can press one key three times , It will be updated continuously ( Hee hee ).


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved