Learn Python with zero basics (7)-great branches and loops1,2

elif is the abbreviation of else...if, which
embodies the simplicity and readability of Python

Python can effectively avoid "hanging else"

Indentation is the soul of Python. The strict requirements of indentation make Python code very streamlined and hierarchical. This mandatory specification makes the code must be aligned correctly. That is, the program must be very sure which if the else belongs to when programming, and there is no ambiguity.

Conditional expression (ternary operator)

There are three operand
syntax :x if 条件 else y

if x < y:
	small = x
else:
	small = y

Equivalent to

small = x if x < y else y

Assert

  • The keyword assert is called "assertion". When the condition behind this keyword is false, the program automatically crashes and throws an AssertionError exception
  • Generally speaking, we can use assert to put checkpoints in the program. When we need to ensure that a certain condition in the program must be true to make the program work properly, the assert keyword is very useful.
    Insert picture description here

Task

  1. if not (money <100): This line of code above is equivalent?
    if money >= 100

  2. What is the role of assert?
    Determine whether the following program is true or false

  3. Suppose there are x = 1, y = 2, z = 3. How can I quickly exchange the values ​​of the three variables?
    x, y, z = z, y, x

  4. Guess what function (x <y and [x] or [y])[0] does?

  5. Have you heard of the membership operator?

Python has a membership operator: in, which is used to check whether a value is in the sequence, if it returns True in the sequence, otherwise it returns False

>>> name = '小甲鱼'
>>> '鱼' in name
True
>>> '肥鱼' in name
False
  1. In the video, if-elif-else used by small turtles is more efficient than using if in most cases, but according to general statistical laws, the results of a class generally follow a normal distribution, which means that the average score is generally concentrated in 70~ 80 points, so according to the statistical law, we can also improve the program to improve efficiency.
    Question Memo: According to the 100-point system, scores above 90 are A, 80 to 90 is B, 60 to 80 is C, and 60 is D. Write a program, when the user enters the score, it will be automatically converted to ABCD for printing.
score = int(input('请输入一个分数:'))
if 80 > score >= 60:
	print('C')
elif 100 >= score >=90:
	print('A')
elif  90 > socre >= 80:
	print('B')
elif 60 > score >= 0:
		print('D')
else:
	print('输入错误!')
  1. The author of Python refused to join the ternary operator for a long time because he was afraid of creating an international garbled competition like the C language. The complexity of the egg pain makes beginners daunting. However, once you figure out the ternary operator Using skills, perhaps some more complex problems can be solved.

Please modify the following code to implement the ternary operator:

x, y, z = 6, 5, 4
if x < y:
    small = x
    if z < small:
        small = z
elif y < z:
    small = y
else:
    small = z
x, y, z = 6, 5, 4
small = x if(x < y and x < z) else (y if y < z else z)

Guess you like

Origin blog.csdn.net/qq_44520665/article/details/112699900