Study Notes Day08] [Python 2.6 branch and loop two

1. The problem - the score level problem

100 made in accordance with, more than 90 points score of A, 80-90 points score is B, C is between 70-80 minutes, 60-70 minutes for the D, about 60 minutes to E, when the input user's score, Analyzing printout ABCDE

1.1 The method used if

performing a low code efficiency cpu

score = int(input("请输入用户的一个分数:"))

if 90<= score <= 100:
    print("A")

if 80<= score < 90:
    print("B")

if 70<= score < 80:
    print("C")

if 60<= score < 70:
    print("D")

if 0<= score < 60:
    print("E")

if score <0 or score > 100:
    print("输入错误!")

End Analyzing the first after the first one, also need to perform a subsequent determination, a waste of time cpu

1.2 Method II if ... else
score = int(input("请输入用户的一个分数:"))
if 90<= score <= 100:
    print("A")
else:
    if 80<= score < 90:
        print("B")
    else:        
        if 70<= score < 80:
            print("C")
        else:
            if 60<= score < 70:
                print("D")
            else:
                if 0<= score < 60:
                    print("E")
                else:
                    print("输入错误!")
1.3 Method three if elif
score = int(input("请输入用户的一个分数:"))
if 90<= score <= 100:
    print("A")
elif 80<= score < 90:
    print("B")
elif 70<= score < 80:
    print("C")
elif 60<= score < 70:
    print("D")
elif 0<= score < 60:
    print("E")
else:
    print("输入错误!")

When the judge finished the first condition, if true, then exit

For the third program, according to the general rule of statistics, scores of 70-80 is generally more concentrated, and therefore statistical rule,
may be in the range of 70-80 to the condition on the front, in order to achieve efficient use of cpu.

2.python can effectively avoid suspension else,

# Hanging else

    if()
        if()
            printf();
    else
        printf();

C language syntax error will be suspended, in python not exist, since the python indentation is controlled by the logic block

3. The conditional expression (ternary operator)

A ternary operator conditional statement may be a statement to
the format: variable name = Equation 1 (IF condition) else two equations

x,y = 4, 5
if x<y:
    small = x
else:
    small = y

The above example can be changed to:

small = x if x<y else y

4. assert assertion

When the condition after the keyword is false, the program automatically collapse and throw exceptions AssertionError

assert 3> 4 # will prompt AssertiongError
In general, we can use Ta placed checkpoints in the program, when the need to ensure that the program of one of the conditions must be true in order for the program to work, assert key measurement is very useful

For example, when a program interface only want to enter a number, but other invalid input that can be used to assert assert statement
is generally used for the test program

#################################################################################################

5. Exercise:

5.1 if not (test <100): equivalent

#if test >= 100:

5.2 Suppose x = 1, y = 2, z = 3, the value of a rapid exchange situation three variables:
x = 1
y = 2
z = 3
print(x,y,z)
x,y,z = z,x,y
print(x,y,z)

operation result:
Here Insert Picture Description

5.3 What is the function to achieve the following expression

(x <y and [x] or [y]) [0] to achieve what functions?
# This is the ternary operator, involves slicing and list of knowledge
# flexible use and and or with the use of ternary operator

5.4 Membership operators: in

#python have a membership operator: in
# for checking whether or not in the list, if in the sequence, it returns True, otherwise return False
# often used for in the

name = 'weivid'
print('we' in name) #True
print('wvi' in name) #Flase
print('h' in name) #False

operation result:
Here Insert Picture Description

Published 105 original articles · won praise 71 · views 40000 +

Guess you like

Origin blog.csdn.net/vivid117/article/details/104295415