The branch and cycle of Python 3

1: Conditional branch

if condition:

Statement

else:

Statement

2. Abbreviation

else:

  if :

It can be abbreviated to elif, so Python can effectively avoid "hanging else"

give an example:

#suspension else
score = int(input('Please enter a score:'))
if 80 > score >= 60:
    print('C')
else:
    if 90 > score >= 80:
        print('B')
    else:
        if 60 > score >= 0:
            print('D')
        else:
            if 100 >= score >= 90:
                print('A')
            else:
                print('Input error!')


#Avoid "hanging else"
score = int(input('Please enter a score:'))
if 80 > score >= 60:
    print('C')
elif 90 > score >= 80:
    print('B')
elif 60 > score >= 0:
    print('D')
elif 100 >= score >= 90:
    print('A')
else:
    print('Input error!')                                       

3: Conditional expression (ternary operator)

small = x if x<y else y

The example changes the following code to a 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

After modification

small = x if (x < y and x < z) else (y if y < z else z)

 

 

4: Assert (assert)

When the condition after assert is false, the program automatically crashes and throws an exception of AssertionError

It can be used to place checkpoints in a program

assert 3<4
>>>True
assert 1<0
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    assert 1<0
AssertionError
>>>

5:while loop

grammar

while condition:

Circulatory body

6: for loop

grammar

for target in expression:

Circulatory body

BIF range()

grammar

range([start],[stop],step=1)

Example

for i in range(5)
    print(i)
fori in range(2,9)
    print(i)
fori in range(1,10,2)
    print(i)

Tricolor problem:

Red, yellow and blue balls of three colors, red balls of three yellow balls of three, blue balls of six, from which 8 balls can be arbitrarily touched, and the color matching of the touched balls can be calculated by programming

print('red\tyellow\tgreen')
for red in range(0, 4):
    for yellow in range(0, 4):
        for green in range(2, 7):
            if red + yellow + green == 8:
                # Note that the lower edge is not string concatenation, so you do not need to“+"Oh~
                print(red, '\t', yellow, '\t', green)

7: Two key sentences

break terminate the current loop

continue terminates this cycle and enters the next cycle but tests the cycle conditions before entering the next cycle!

Membership operator

obj [not] in sequence

The return value of this operator is True or False

Example

Design a password verification program for users, three times, input the password prompt with "*" number from new input, without reducing the number of times

count = 3
password = 'FishC.com'

while count:
    passwd = input('Please input a password:')
    if passwd == password:
        print('Correct password, enter the program......')
        break
    elif '*' in passwd:
        print('Password cannot contain"*"Number! You have', count, 'Opportunity!', end=' ')
        continue
    else:
        print('Password input error! You have', count-1, 'Opportunity!', end=' ')    
    count -= 1

Tags: Python Programming

Posted on Mon, 04 May 2020 03:51:37 -0700 by shibbi3