The 141 original Python article for beginners

It takes about 4 minutes to read this article

Stackoverflow is one of the most popular sites in the world where you can answer a lot of questions about programming, installation, or debugging errors. I crawled nearly 900,000 data on Stackoverflow a year ago. I want to write a visual sharing report later! Today we’re going to take a look at the most voted questions and see how many can you answer?

1.What does the “yield” keyword do?

Number of voters: 7,700 Number of spectators :1,591,544

  • There seems to be a lot of interest in the most yield usage in Python, and indeed it’s a very awkward and confusing use to learn at first, because it doesn’t exist in other languages.
  • Yield is closely related to generators in Python, and it may seem awkward at first, especially if the use and logic of generator functions with yield are strange. For example, when scrapy is used, you can see that the yield header is large because scrapy uses a lot of yield to crawl data.
  • But as you get used to it, this usage is very Python beautiful

2.What are metaclasses in Python?

Number of voters: 4,232 Number of spectators :565,158

  • Metaclass is a metaclass. There are a few very difficult concepts in Python, such as yield above, and decorators, which may be confusing for beginners, but there are even more complicated ones: metaclasses!
  • I personally find metaclasses to be one of the most obscure concepts in Python. Understanding metaclasses requires a good understanding of the internal structure and logic of classes, and can be considered when inheriting encapsulation between multiple classes.
  • I personally prefer to use design patterns to solve the problem, but the metaprogramming techniques are well worth studying for Python students.

3.How to check whether a file exists?

Number of voters: 3,592 Number of spectators :2,553,760

  • This question seems so simple, why so many people ask it. Os.path. exist(Path) doesn’t solve the problem, but click on it and there is a heated discussion.
  • Os.path.isfile (path), os.access(), os.access(), etc.

4.Does Python have a ternary conditional operator?

Number of voters: 3,590 Number of spectators :1,209,596

If you are familiar with trinary arithmetic, do you have trinary arithmetic in Python? For example:

#way1
1 if a > b else -1
#way2
(falseValue, trueValue)[test == True]
#way3
a=1
b=2
print ({True: 100, False: 200}[a > b])
Copy the code

The above 3 kinds of trilliocular operation writing is not to let your imagination rich, but also so. Yes, especially when you’re doing a state machine, and there’s a history article called “Google Interview Algorithm, Two Buckets of water “that uses this trick.

5.What does __name__ == “__main__” : do?

Number of voters: 3,590 Number of spectators :1,209,596

  • All of you who are learning Python, when you first start learning Python, you’ll come across this line of code, but when I first started learning Python, I thought of it as an entry function to main.
  • __name__ refers to the name of a module reference. If the program is run inside the module, it is __main__, and if it is an external reference, it is the module name.

Calling an external command in Python?

Number of voters: 3,276 Number of spectators :2,324,850

Os.system () or os.popen(“ls -l”).read(). Or run some command with subprocess.popen ().

7.How to merge two dictionaries in a single expression?

Number of voters: 2,858 Number of spectators :1,024,003

The use of dictionaries and lists is one of the most subtle aspects of Python. Dictionary merge there are a variety of dictionary merge methods, very subtle, here are just a few of them:

x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
x.update(y)
print (x)
Copy the code
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z = {**x, **y}
print (z)
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z = dict(x, **y)
print (z)
Copy the code

8.How do I sort a dictionary by value?

Turnout: 2,650 Spectators :1,604,329

  • The dictionary sorting problem is a typical dictionary-related problem, and the dictionary sorting problem is also a typical dictionary-related problem, which is found on Stackoverflow.
  • There are many ways to sort the dictionary, as detailed in the article I wrote a year ago, there are several articles on this introduction.
  • You can generally use sorted, either by getting the items() of the dictionary for a sorted loop, or by most_common() in the Collections module.

9.How can I create a directory if it does not exist?

Number of voters: 2,643 Number of spectators :1,645,881

  • This problem is very coincidentally, I shared a very Pythonic use in the small circle a while back, not only does the path not exist, but the file does not exist!
  • Did not think of StackOveFlow this is the ninth hot issue! We commonly use if/else or if not to check whether os.path.exist() exists in a path or create a new path if not.

10.Does Python have a string ‘contains’ substring method?

Number of voters: 2,496 Number of spectators :2,226,231

  • There is always a string problem! I’ve been thinking about files, dictionaries, modules, generators, objects, how come there are no strings!
  • Ah ha found that the tenth question is about strings, this question actually involves a lot of string skills, dig out, can dig out a laundry list of string skills!
  • A small string can be found in pandas, anda large string can be searched by a regular string. A large string can be searched by the STR in pandas.

Of course, if it is just a simple analysis of hot issues, there is no need to in-depth climb, just need to point the ranking on the web page, I am to write an in-depth analysis report.

Well above is the top 10 questions, I read down all the way, found a lot of harvest! Also can detect oneself to learn to master a situation, sometimes answer is to help others not only, also be to improve oneself, review old and know new can be stronger! Small partners do not know that everyone has hit a few, welcome the number of messages reported!

Recommended reading:

12-12 come, I climb took all the down jacket on taobao | want to get the biggest discount

Python has 50 w highest-paid road across the country nearly 1600 | Python positions and data analysis

To learn Python salary how many | I climb took all the Python position

A guide to the Google Python style specification worth knowing

Nanjing housing fire? | 16000 sets of secondary data analysis

Analysis of 9000 movies | a simple movie recommendation system

Much!!!! Combat crawl cut in 5000 mobile phones | last

Python