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
The general conditional statements are
1、if……else……
2、if……elif……else
3、 multiple if nesting
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
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
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
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
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 .
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
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
Code example
total = 0
index = 1
while 1:
if index > 100:
break
total += index
index += 1
print("total = {}".format(total))
Output
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
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 ).