Joke Collection Website - Joke collection - How to program like a Python master?
How to program like a Python master?
1.Zen in Python \ x0d \ Zen in Python is the guiding principle of Python language. By following these basic principles, you can program like a boa constrictor. You can check the details by typing import this at the Python command line: \ x0d \ the Zen of Python, by Tim Peters \ x0d \ x0d \ beautiful better than ugly. \ x0d \ # Beauty is better than ugliness (Python aims to write beautiful code) \x0d\\ X0d \ Explicit is better than implicit. \ x0d \ # Clarity is better than obscurity (beautiful code should be clear, with standardized naming and similar style) \ x0d \ \ x0d \ Simplicity is better than complexity. \ x0d \ # Simplicity is better than complexity (beautiful code should be concise without complicated internal implementation) \ x0d \ \ x0d \ Complexity is better than complexity. \ x0d \ # Complexity is better than clutter (if complexity is inevitable, there should be no incomprehensible relationship between codes, so keep the interface simple) \ x0d \ \ x0d \ Flat is better than nesting. \ x0d \ # Flat is better than nesting (beautiful code should be flat, and there should not be too many nesting) \ x0d \ x0d \ Sparse is better than dense. \ x0d \ # Spacing is better than compactness (beautiful code has proper spacing, so don't expect a single line of code to solve the problem) \ x0d \ \ x0d \ readability counties. \ x0。 # Readability is very important (beautiful code is readable) \ x0d \ \ x0d \ Exceptions are not so special as to violate the rules. \ x0d \ Although practicality trumps purity. \ x0d \ # Even in the name of the practicality of special circumstances, don't violate these rules (these rules are supreme) \ x0d \ \ x0d \ Errors should never pass silently. \ x0d \ Unless explicitly muted. \ x0d \ # Don't tolerate all mistakes unless you are sure that you need to do so (catch exceptions accurately and don't write code in the style of except:pass) \ X0d \ \ x0d \ Faced with ambiguity, refuse to explain and guess. \ x0d \ # When there are many possibilities, don't try to guess that \x0d\\x0d\ should have one-and preferably only one-objective way to do it. \ x0d \ # On the contrary, try to find an obvious solution (if you are not sure, use the exhaustive method) \x0d\\x0d\ Although this method may not be obvious at first, you are Dutch. \ x0d \ # Although this is not easy, because you are not the father of Python (which means Guido in Dutch here) \ x0d \ Now is better than nothing. \ x0d \ although it has never been so much better than it is now. \ x0d \ # Doing it may be better than not doing it, but not doing it is better (think twice before you do it) \ x0d \ If the implementation is difficult to explain, it is a bad idea. \ X0d \ This may be a good idea if the implementation is easy to explain. \ x0d \ # If you can't describe your plan to people, it's definitely not a good plan; Scheme evaluation standard \ x0d \ namespaces is a concept that respects great ideas-let's do more such things! \x0d\# namespace is a wonderful idea, and we should use \x0d\\x0d\ as a joke more often, but it does contain a lot of Python philosophy behind it. Python Zen has been officially written in PEP 20. See PEP 20\x0d\ II. PEP8: Python code \ x0d \ Abelson &; Sussman said in the book "Construction and Interpretation of Computer Programs": Programs are written for people, but executed by machines. So try to be readable when coding. PEP 8 is the coding specification of Python. See PEP8 for official documents. PEP is the abbreviation of Python Enhancement Proposal. PEP8 includes many coding specifications. The following mainly introduces indentation and naming. \x0d\ white space and indentation) \x0d\ \ white space and indentation are very important in Python language, which replaces the role of {} in other languages to distinguish code blocks from scopes. In this regard, PEP8 has the following suggestions: \x0d\ 1, use 4 spaces for each indentation \x0d\2, do not use Tab, and do not mix Tab with spaces \x0d\3, use a blank line between two methods, use two blank lines between two classes \x0d\4, and add one to the dictionary. Instead of the previous \x0d\5, add a space (except in the parameter list) \x0d\6 on both sides of assignment and comparison, and leave no space immediately after parentheses or in front of the parameter list. \x0d\ Python naming \x0d\ Naming specifications are the basis of programming languages, and most specifications are high-level languages & The basic specification of Python is as follows. Attribute: joined_lower\x0d\2, constant: joined_lower or ALL_CAPS\x0d\3, class: studycaps \ x0d \ 4, class attribute: interface, _ internal, _ _ private \ x0d \ 5. The camel case only confirms to the pre-existing agreement \ x0d \ \ x0d \ The above contents only give a very simple introduction to PEP8, so I won't talk about it here, because today's topic is not here. If you want to know more about Python coding specifications, you can read PEP8 official documentation and Google Python coding specifications. \x0d\ III。 Exchange values \ x0d \ In other languages, when exchanging two variable values, you can write: \ x0d \ temp = a \ x0d \ a = b \ x0d \ b = temp \ x0d \ x0d \ In Python, the comma (,) is the syntax of tuple data structure in Python; The above syntax will perform the following operations: \x0d\ 1, Python will first generate a tuple from A and B on the right and store it in memory; \x0d\ 2。 After that, the assignment operation will be carried out, and then the tuple will be disassembled; \x0d\ 3。 Then assign the first element of tuple to the first variable on the left and the second element to the second variable on the left. \x0d\ Give another example of tuple splitting: \ x0d \ in [1]: people = ['David',' Python ista','156514551234'] \ X0d \ in [3]: name \ x0d \ out [3]:' David' \ x0d \ x0d \ in [4]: title \ x0d \ out [4]:' Python ista' \ x0d \ n ['Wu',' 15101365547'] \ x0d \ x0d \ in [7]: For names, titles and people on the phone: \ x0d \ ...: Print the name and phone number \ x0d \. David15145551234 \x0d\ wu151365547 \ x0d \ x0d \ PS: When using this syntax .. \ x0d \ tuple. x0d\( 1,)\ x0d \ & gt& gt& gt( 1,)\x0d\( 1,)\ x0d \ & gt& gt& gt( 1)\x0d\ 1\x0d\ >& gt& gtvalue = 1,\ x0d \ & gt> value \ x0d \ (1,) \ x0d \ x0d \ We know that comma (,) is the constructor for creating a tuple in Python, so we can easily create a tuple by the above method; It should be noted that if you declare a tuple with only one element, you must add a comma at the end, but no more than two elements. The syntax for declaring tuples is simple, but it is also quite concise: if you find that a variable in Python mysteriously becomes a tuple, it is probably because you wrote an extra comma. . \x0d\ IV。 "_" (interactive "_") x0d \ This is a very useful function in Python, but many people don't know it (I didn't know it until I was in contact with Python for a long time). . In the interactive console of Python, when you evaluate an expression or call a method, the result of the operation will be placed in a temporary variable _. _ (underline) is used to store the last printing result, for example: \ x0d \>> import mathematics \ x0d \>> math.pi/3 \ x0d \1.047197551. >& gt& gtangle = _ \ x0d \ & gt& gt& gtmath . cos(angle)\ x0d \ 0.50000000000 1 1 \ x0d \ & gt; & gt> _ \ x0d \ 0.50000000000001\ x0d \ x0d \ PS: When the returned result is None, the console will not print, and the value stored in _ will not change. \x0d\ v. Building strings from substrings \ x0d \ If there are some strings in a list, you need to merge them into one string now. The simplest method is as follows: \x0d\colors = ['red',' blue', Green',' yellow'] \ x0d \ x0d \ result =' \ x0d \ for s in colors: \ x0d \ result+= s \ x0d \. String objects in Python are immutable, so any string operation, such as splicing and modifying, will produce a new string object, not based on the original string. Therefore, the above method will consume a lot of memory: it needs to calculate, store and discard the intermediate calculation results. The correct way is to use the join method in Python: \ x0d \ result =','. Join (colors) \ x0d \ x0d \ When there are few merge elements, you can't see much effect with the join method; But when there are many elements, you will find that the efficiency of join is still obvious. However, when using, please note that join can only be used for lists whose elements are strings, and it will not perform any forced type conversion. When a list is connected with one or more non-string elements, an exception is thrown.
- Previous article:Excerpted from "Xue Tao's Novel" Please help me
- Next article:How many suns, moons and stars are there in the sky?
- Related articles
- Zhao Xiaodao joked.
- Parents live together, and they will have food when they come home from work.
- 3 Good Night Stories for Children 3 Good Night Stories for Children 3 Good Night Stories for Children
- Sentences that make boys' hormones soar
- Which is the best episode of the first episode of Funny Family?
- How many years has Chinese civilization been passed down?
- Why register e-books in Fan Fan?
- Is there an epidemic in Meizhou?
- Why do you want to collect property tax?
- Myth of the richest man _ joke