Python's elegant writing method makes you get twice the result with half the effort

Elegant writing of Python

Python's design philosophy is "elegant", "clear", "simple". That's probably why many people choose python. Here are some authentic Python dialects to make your code more elegant and concise.

  • Assign values to multiple variables

    When we need to assign values to multiple variables, we often write as follows:

    
    height = 20
    weitht = 10
    width = 50
    
    

    In Python, we can write it elegantly

    
    height, weight, width = 20, 10, 50
    
    
  • Sequence unpacking

    You need to take out each element of a sequence for printing, which is usually written as follows:

    
    info = ['steve', 'male', 58]
    name = info[0]
    gender = info[1]
    age = info[2]
    
    print(name, gender, age)
    
    

    In Python, we can write it elegantly

    
    info = ['steve', 'male', 58]
    name, gender, age = info
    print(name, gender, age)
    
    
  • Grace your judgment

    Define the absolute value function with the judgment statement, usually as follows:

    
    def my_abs(x):
        if x < 0:
            return -x
        else:
            return x
    
    

    In Python, we can write it elegantly

    
    def my_abs(x):
    
        return -x if x < 0 else x
    
    
  • Interval judgment

    The statement judged by and twice in a row is executed when all conditions are met. We usually write as follows:

    
    score = 85
    
    if score >= 80 and socre < 90:
        print('B')
    
    

    It's easy to write in Python

    
    score = 85
    
    if 80 <= score < 90:
        print('B')
    
    
  • Multiple values meet the condition judgment

    If any value of multiple values meets the condition, it is True. Our usual way of writing is as follows:

    
    num = 3
    type = ''
    
    if num == 1 or num == 3 or num == 5 or num == 7:
        type = 'Odd number'
    
    print(type)
    
    

    In Python, we can simply write

    
    num = 3
    type = ''
    
    if num in (1, 3, 5, 7):
        type = 'Odd number'
    
    print(type)
    
    
  • Judge whether it is empty

    Determine whether the element is empty or not. We usually use the len() function to judge whether the length is equal to 0 or not, which means it is empty, which is usually written like this

    
    a, b, c = [1, 4, 5], {}, ()
    
    if len(a) > 0:
        print('a Is not empty')
    elif len(b) > 0:
        print('b Is not empty') 
    elif len(c) > 0:
        print('c Is not empty')   
    
    

    In Python, the execution condition after if can be abbreviated. As long as the condition is a non-zero value, a non empty string, a non empty list, etc., it is judged to be True. Otherwise, it is False. So, we can write that

    
    a, b, c = [1, 4, 5], {}, ()
    
    if a:
        print('a Is not empty')
    elif b:
        print('b Is not empty') 
    elif c:
        print('c Is not empty')   
    
    
  • At least one of the multi condition content judgments is valid

    Usually, we use or to divide conditions, which is what we write

    
    math, english, computer = 89, 88, 90
    
    if math > 60 or english > 60 or computer > 60:
        print('pass')
    else:
        print('fail')
    
    

    In Python, we can use the any function to concatenate the judgment conditions

    
    math, english, computer = 89, 88, 90
    
    if any(math > 60, english > 60, computer > 60):
        print('pass')
    else:
        print('fail')
    
    
  • Multi condition content judgment is all true

    Usually we use and to concatenate the sentences that need to be judged, which is how we write them

    
    math, english, computer = 89, 88, 90
    
    if math > 60 and english > 60 and computer > 60:
        print('pass')
    else:
        print('fail')
    
    

    In Python, we can use the all function to concatenate the judging conditions

    
    math, english, computer = 89, 88, 90
    
    if all(math > 60, english > 60, computer > 60):
        print('pass')
    else:
        print('fail')
    
    
  • Elements and element subscripts of traversal sequences

    We usually use the for loop to traverse elements and subscripts.

    
    L =['math', 'English', 'computer', 'Physics']
    
    for i in L:
        print(i, ":", L[i])
    
    

    In Python, we can use the enumerate function to make it more concise

    
    L =['math', 'English', 'computer', 'Physics']
    
    for k, v in enumerate(L):
        print(k, ":", v)
    
    
  • List generation

    We usually use the for loop to generate the list. For example, we need to generate a list of [1x1,2x2,3x3, 4x4, 5x5]

    
    L = []
    for i in range(1, 5):
        L.append(i * i)
    
    print(L)
    
    

    In Python, we can use list derivation to generate this list quickly

    
    L = [i * i for i in range(1, 5)]
    print(L)
    
    

Tags: Programming Python

Posted on Mon, 18 May 2020 05:10:26 -0400 by emilyfrazier