Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the code of the Python convention

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report