python judgment statement and loop statement (syntax, execution process, code)

*Judgment statement if*

*Single branch*

*Grammar*

"""

if condition:

Code block

1. The colon must be in English

2. You must indent

"""

flow chart

 

*Code*

"""

Eval (string)

1. Convert string to int type

a = eval("10")

print(a) #10

print(type(a)) #<class 'int'>

2. Calculate the string

   a = "3*2" 
   print(a) #3*2
   b = eval(a)
   print(b) #6

"""

#input("please guess a number") --- Str

#eval() converts Str to int

guess = eval(input("Please guess a number"))

if guess ==99:

print("Congratulations, you guessed right") #Congratulations, you guessed right

*Two branches*

*Grammar*

"""

if condition:

Code block

else:

Code block

"""

*Execution process*

 

 

*Code*

guess = eval(input("Please guess a number"))

if guess == 99:

print("Congratulations, you guessed right")

else:

print("Thank you for your patronage")

*Multi branch*

*Grammar*

"""

if condition:

Code block

elif condition:

Code block

elif condition:

Code block

........

else:

Code block

 

"""

*Execution flow chart*

 

score = eval(input("Please enter a grade:"))

if score >= 90:

  print("a")

elif 90 > score>=80:

  print("B")

elif 80 >score>=70:

  print("C")

elif 70>score>=60:

  print("D")

else:

  print("fail,")

*Ternary operator*

*Grammar*

Code block that meets the condition if condition else code block that does not meet the condition

*Code*

"""

ternary operator

Abbreviation for if

Code block that meets the condition if condition else code block that does not meet the condition

"""

guess = eval(input("Please guess a number"))

print("Congratulations, you guessed right")if guess == 99 else print("Thank you for your patronage")

*Execution process*

 

 

*Attention*

\1. It must be a two branch statement

*Conditional judgment and combination*

 

'''
classification      International * * BMI value * * (kg/m^2 * *)**      Domestic * * BMI value * * (kg/**m^2)**
Thin     < eighteen point five     < eighteen point five
normal      18.5 ~ 25      18.5 ~ 24
Overweight      25 ~ 30      24 ~ 28
Obesity      ≥30      ≥28

Formula: BMI = weight (kg) / height 2 (m2)
Demand: input a set of data according to domestic indicators to judge whether it is fat or thin

'''



#Enter height and weight
height,weight=eval(input("Please enter your height(rice)And weight(kg .),Separated by commas"))#How many power is pow (height 2)
#Formula: BMI = weight (kg) / height 2(m2) bmi=weight /pow(height,2)
print("BMI The value is:{:2f}".format(bmi))#Define a null value to facilitate the following assignment nat = ""
if bmi <18.5:
nat ="Thin "
elif 18.5<=bmi <24:
nat ="normal"
elif 24 <=bmi < 28:
nat ="Overweight" else:
nat ="Obesity"
print("BMI The domestic index is:{0}".format(nat))

 

*Loop statement for*

*Grammar*

"""

for variable in traversal structure:

Code block

"""

*Execution process*

 

*Usage*

*String traversal*

Str = "hello python"

for i in Str:

print(i)

"""

h   e  l   l   o   p   y   t   h   o   n

"""

*List traversal*

list01 = ["Zhao Liying","happy","Qi Wei","Zhu Zixiao"]

for i in list01:

print(i)

"""

Zhao Liying

happy

Qi Wei

Zhu Zixiao

"""

*Dictionary traversal*

"""

1. Use normal traversal, and only key can be printed

dict01 = {"Tianlong Babu":"duan yu","The story of relying on heaven to kill Dragons":"zhang wuji","The nine Yin manual":"Zhou Zhiruo","Legend of Shooting Heroes":"Guo Jing"}

for i in dict01:

print(i)

"""

Tianlong Babu  
The story of relying on heaven to kill Dragons
 The nine Yin manual
 Legend of Shooting Heroes

"""

2. Dictionary. items() returns a tuple ("Tianlong Babu", "Duanyu")

('Tianlong Babu', 'Duan Yu')

<class 'tuple'>

('the story of relying on heaven to kill dragons',' Zhang Wuji ')

<class 'tuple'>

(the nine Yin manual classics, 'Zhou Zhi Luo')

<class 'tuple'>

('Legend of Shooting Heroes',' Guo Jing ')

<class 'tuple'>

"""

dict01 = {"Tianlong Babu":"duan yu","The story of relying on heaven to kill Dragons":"zhang wuji","The nine Yin manual":"Zhou Zhiruo","Legend of Shooting Heroes":"Guo Jing"}

for t in dict01.items():

  print(t)

print(type(t))
'''('Tianlong Babu', 'duan yu')
('The story of relying on heaven to kill Dragons', 'zhang wuji')
('The nine Yin manual', 'Zhou Zhiruo')
('Legend of Shooting Heroes', 'Guo Jing')'''

"""

3. To obtain separate key and value is to find a way to split tuples and return basic data types

Assign tuples to two variables

Tianlong Babu

<class 'str'>

duan yu

<class 'str'>

"""

for k,v in dict01.items():

print(k)

print(type(k)) #Str

print(v)

print(type(v)) #Str

*S**et set traversal***

set01 = {1,2,3,4}

for i in set01:

  print(i) 
  '''
1
2
3
4'''

*Infinite loop (* * while)***

*Grammar*

"""

while condition:

Variable change formula

Code block

"""

*Code*

a = 10

while a>0:

#Variables change constantly, in order to meet the conditions and avoid entering the dead cycle

 a  -= 1

 print(a)
 '''9
8
7
6
5
4
3
2
1
0'''

*break* *continue*

break jumps out and ends the current whole loop without executing the statement after the loop (loop end)

continue ends the current cycle and continues to execute subsequent cycles (the cycle continues)

*Code*

for i in [1,2,3,4,5]:

if i==2:

break

print(i) #1



for i in [1,2,3,4,5]:

if i==2:

continue

print(i) #1

"""

1

3

4

5

"""

*Cyclic plus else*

*Grammar*

"""

for variable in traversal structure:

Code block

else:

Code block

"""

*Execution process*

"""

1. Execute the for loop first

2. Execute else after execution

"""

*Code*

for i in [1,2,3,4,5]:

print(i)

else:

print("End of cycle")

Similarly, can else be added to while? Yes, just like the above

*while syntax*

While < condition >:

< statement block 1 >

else :

< statement block 2 >

#At the end of the loop, the else code is executed

 

Tags: Python

Posted on Fri, 26 Nov 2021 06:26:40 -0500 by 01706