Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the code of the Python convention

Shulou Source: shulou.com Published: 2022-06-02 12:47:26 09月10日 Update

This article mainly explains "what is the code of Python convention". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the code of Python convention".

One. So that the code can be imported and executed.

If _ _ name__ = ='_ _ main__':

Two. Judge whether the logic is "true" or "false" in the following way.

If x:if not x:

Good code:

Name = 'jackfrued'fruits = [' apple', 'orange',' grape'] owners = {'1001': 'Luo Hao', '1002': 'Wang Da Hammer'} if name and fruits and owners: print ('I love fruits')

Bad code:

Name = 'jackfrued'fruits = [' apple', 'orange',' grape'] owners = {'1001': 'Luo Hao', '1002': 'Wang Da Hammer'} if name! =''and len (fruits) > 0 and owners! = {}: print (' I love fruits')

Three. Be good at using in operator.

If x in items: # contains for x in items: # iterations

Good code:

Name = 'Hao LUO'if' L'in name: print ('The name has an L in it.')

Bad code:

Name = 'Hao LUO'if name.find (' L')! =-1: print ('This name has an L in itinerary')

Four. Do not use temporary variables to exchange two values.

A, b = b, a

Five. Build a string with a sequence.

Good code:

Chars = ['jacks,' asides, 'canals,' kicks, 'fags,' rallies, 'uprights,' eBay,'d'] name = '.join (chars) print (name) # jackfrued

Bad code:

Chars = ['jacks,' asides, 'caches,' kills, 'fags,' rudes, 'uplands,' eBay,'d'] name =''for char in chars: name + = charprint (name) # jackfrued

Six. EAFP is superior to LBYL.

EAFP-Easier to Ask Forgiveness than Permission.

LBYL-Look Before You Leap.

Good code:

D = {'xboys:' 5'} try: value = int (d ['x']) print (value) except (KeyError, TypeError, ValueError): value = None

Bad code:

D = {'x':'5'} if'x'in d and isinstance (d ['x'], str)\ and d ['x'] .iskeeper (): value = int (d ['x']) print (value) else: value = None

Seven. Iterate using enumerate.

Good code:

Fruits = ['orange',' grape', 'pitaya',' blueberry'] for index, fruit in enumerate (fruits): print (index,':', fruit)

Bad code:

Fruits = ['orange',' grape', 'pitaya',' blueberry'] index = 0for fruit in fruits: print (index,':', fruit) index + = 1

Eight. Generate a list in generative form.

Good code:

Data = [7, 20, 3, 15, 11] result = [num * 3 for num in data if num > 10] print (result) # [60, 45, 33]

Bad code:

Data = [7,20,3,15,11] result = [] for i in data: if I > 10: result.append (I * 3) print (result) # [60,45,33]

Nine. Use zip key combinations and values to create a dictionary.

Good code:

Keys = ['1001', '1002', '1003'] values = ['Luo Hao', 'Wang Dahun', 'Bai Yuanfang'] d = dict (zip (keys, values)) print (d)

Bad code:

Keys = ['1001', '1002', '1003'] values = ['Luo Hao', 'Wang Dahun', 'Bai Yuanfang'] d = {} for I, key in enumerate (keys): d [key] = values [I] print (d) Thank you for your reading. That's what the code of Python convention is all about. After the study of this article, I believe you have a deeper understanding of what the code of Python convention has. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Code bad convention Wang Da learning content generation iteration two variables dictionaries characters strings that is sequences ideas situations articles ways more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS NVidia Apple Docker