2020 Python data analysis study notes control sentences (2)

table of Contents

One, conditional statement

Second, the loop statement

Three, other sentences


One, conditional statement

a = 6
b = 2
if a > b:
    print("yes")
else:
    print("no")
score = int(input("请输入成绩:"))        #条件流程控制
if ( score>=90):
    print("优秀!")
elif(score>=80):
    print("良好")
elif(score>=60):
    print("及格")
else:
    print("不及格")

Multiple nested conditional statements:

place = input("请输入你的户籍:")
social_insurance = int(input("请输入你的社保年限:"))
age = int(input("请输入你的年龄:"))

if place != '广州':
    if social_insurance > 5 and age >18:
        print('可以买房')
    else:
        print('不可以买房')
elif place == '广州' and age > 18:
    print('可以买房')
else:
    print('没有资格买房')

Second, the loop statement

Advantages: The loop statement is used to perform an operation repeatedly to avoid duplication of work.

Commonly used methods: for and while two loop methods , for uses traversal to iterate, while judges according to the conditions, whether to continue execution.

number = [1,2,3,4,5]   # 遍历列表
for i in number:
    print(i)

range(function):

for i in range(1,10,1):   # 从1开始,到9结束,间隔为1进行输出
    print(i, end=' ')     # end=' '表示输出的字符以空格的间隔形式输出
# >>>1 2 3 4 5 6 7 8 9

for i in range(1,10,2):  # 从1开始,到9结束,间隔为2进行输出
    print(i, end=' ')
# >>> 1 3 5 7 9 

Calculate the accumulation from 1 to 100:

a = 0
for i in range(1, 101):   # python语法中左开右闭,所以当要取到100时,就要定义到101
    a = a + i
print(a, end=' ')

while loop:

x = 1
total = 0
while x <= 100:
    total = total + x
    x = x + 1
print(total)

 

Three, other sentences

In Python loop statements, complex conditions may nest break, continue, and pass. Among them, break is used to terminate the entire loop statement, continue is used to jump out of the current loop and execute the next loop, and the pass statement is used to occupy a place. Do nothing to maintain the integrity of the structure.

count = 0
while count < 10:
    count = count +1
    if count % 3 == 0:
        continue      # 跳出3、6、9然后继续执行下一步
        print(count, end=' ')
    else:
        print(count, end=' ')
# >>>1 2 4 5 7 8 10 
count = 0
while count < 10:
    count = count +1
    if count % 3 == 0:
        break     # 跳出3然后终止程序
        print(count, end=' ')
    else:
        print(count, end=' ')

# >>>1  2
count = 0
while count < 10:
    count = count +1
    if count % 3 == 0:
        pass     # 用于占位。不做任何操作,保持结构的完整性
        print(count, end=' ')
    else:
        print(count, end=' ')
# >>>1 2 3 4 5 6 7 8 9 10 

Exception handling: When an exception occurs, the program execution will be terminated and an exception handling mechanism needs to be adopted.

# 一、异常处理  try...except...打印错误信息
import traceback
s = 0
for c in "1234567890987654321":
    try:
         s += 10 / eval(c)
    except:
         traceback.print_exc()
         continue
print("执行结束")
a = 3
dict = {'A':'1','B':'2'}
try:
    if a in dict['c']:
        print('OK')
    else:
        print('not found')
except:
    print('字典中没有此键')

 

 

Guess you like

Origin blog.csdn.net/weixin_44940488/article/details/106390333