Still wondering why you can’t learn Python well? Why not take a look at my suggestions! !

Why you always can’t learn Python? 4 pitfalls in getting started with Python

How difficult is it to learn Python from scratch? How to get started? It is not difficult to learn Python from scratch, because Python is a programming language that is very suitable for beginners to get started. Python syntax is simple and clear, the code is highly readable, and it is easy to get started. However, Python has very strict requirements for coding, and it is more conducive for beginners to develop good coding habits.

It is very helpful for beginners to standardize their own learning, and it can also help beginners understand other people's code. Python has excellent syntax design and modern thinking. You can quickly understand some ideas of modern programming languages. The most important thing is that Python has a very good role in various fields. Python is a very suitable choice for learning a language as a tool. . How to learn Python?

Choose a learning direction. The main purpose of learning Python is to use the language to solve problems, not to understand the language. There are many application directions for Python. After learning the basic knowledge of Python, different application directions have different requirements. Although Python requires systematic learning, when learning Python, I want to tell you that you still need to determine the direction you are interested in in advance. Targeted learning is more important.

Plan a learning route. After determining your own development direction, the next step is to learn along the direction and establish your own learning route. There must be a systematic learning route, what goals need to be accomplished, what knowledge needs to be learned, and what knowledge needs to be understood, so that each time you learn a part, you can have actual results output, and the output of results can encourage the next step. study.

Python is known for its simple syntax and few keywords, so it is often deceived by major media as a programming language that is very easy to get started. His specific description is self-evident, but its ease of entry is controversial. Because everyone's foundation is different.

Python is known for its simple syntax and few keywords, so it is often deceived by major media as a programming language that is very easy to get started. His specific description is self-evident, but its ease of entry is controversial. Because everyone's foundation is different.

Most of the Python introductory books on the market have very long contents (after all, they will be criticized if they lack certain important knowledge points). But for beginners, do you really need to learn all the knowledge points?

For beginners, learning from these book catalogs often encounters various traps. Today I will share my personal views.

Too much grammar

Does Python have many grammatical rules? In fact, there are many. If you have studied some ancient programming languages ​​that are no longer updated, you will find that Python actually has a lot of grammatical rules.

Because Python needs to adapt to modern development requirements, it is "forced" to constantly add new grammatical features, such as "decorators", "walrus operators" and so on.

At this time, beginners will fall into the first trap - they cannot resist the temptation of the "table of contents" and feel that they will not be able to get started if they skip a certain knowledge point.

As a beginner, I don’t recommend learning these things (you will most likely not need them in the short term).

So, what kind of grammar do you need to learn?

  1. branch judgment
  2. cycle

To put it bluntly, it is if and for loops.

In fact, learning these grammars does not require you to memorize how to write. Most of the time these grammar statements do not require us to type out each letter by hand, because today's IDEs are very friendly and generally provide the function of generating code snippets. The following is vscode Demo:

Why you always can’t learn Python? 4 pitfalls in getting started with Python

Why you always can’t learn Python? 4 pitfalls in getting started with Python

I often see people suggesting that you have to enter each piece of code by hand when getting started. You must think twice. This can only improve your keyboard typing skills.

For a period of time, I often need to switch back and forth between multiple programming languages, but it does not test my syntax memory level. This is the benefit of code snippets.

Friend: "What about other grammatical rules, such as operator precedence? What should I do if I can't always remember them?"

In fact, I have never memorized operator precedence, because when multiple operators appear, I always use parentheses to clarify their precedence:

Friend: "It looks like it's easy to get started. Is the if and for syntax really so easy to learn?"

No, the focus of learning syntax like if is not how to write, but how to construct bool values, which requires you to further understand basic data types (str, int, bool, etc.).

Fortunately, the number of these knowledge points is very small, and for beginners, there is no need to understand the saving mechanism of each type in depth (such as how many bytes are needed)

The above knowledge points may only be the content of 1 to 3 chapters in an introductory book.

Sequence processing is very important

After you have a basic understanding of the use of if and for, the next step is to understand the data structure of the sequence (list, tuple).

At this point, beginners will fall into another pit - the list has many methods to remember!

Similarly, I do not recommend beginners to memorize these methods. They only need to remember the most commonly used 1 or 2 operations. for example:

  1. Add element: append
  2. Remove elements: remove

In the same way, there are many methods for processing strings. Usually we only need to learn a few methods.

Friend: "?!, that's it?"

In fact, you may rarely even use the above two operations later. Because in Python we prefer to construct new sequences rather than operate on sequences in place.

When you learn derivation later, you will find that the most commonly used syntax is if and for

You may be wondering, why is sequence processing important?

No matter what programming language you learn, whether you are application development or ordinary office automation learning, most of the truly complex logic comes from sequence processing.

For example, there are a bunch of files, and each file has a bunch of data.

As for simple single data, the operation is often very straightforward and simple.

Do you need to learn the dictionary at this stage? My suggestion is that you can try to understand it. If you find that you can't understand it, then skip it. Because there will always be its application scenarios later, it will be easier to learn based on the scenarios at that time.

Learn how to break down problems

After the above two stages of learning, you will find that you can only solve simple problems of addition, subtraction, multiplication and division in elementary school at best, and you will have no clue about slightly more complicated problems.

At this point you fall into another trap - thinking about logic and writing code at the same time

Most introductory books won't teach you this because it's not a Python feature, but it's extremely important.

The essence of programming is to express realistic logic in code

In reality, when we want to complete a relatively complex thing, we first consider the overall process, divide it into multiple sub-processes, and finally consider the details of each sub-process.

My articles are indispensable without cases.

Consider the following real-life scenario: You want to find a book by a certain author on your bookshelf at home (there are more than 100 books).

You may think that this matter is very simple. Don't you just start from the beginning, look at the author's name in each book, and take it out if it matches?

This thinking process is actually a process from the whole to the details:

  1. First, you will consider where to start looking. There is always a direction to search. For example, scan each row of books horizontally from the upper left corner of the bookshelf
  2. Secondly, after you decide to find a book before you start looking, look at the author’s name on the cover.
  3. Finally, if it meets the conditions, it must be taken out and distinguished from the original book.

Note that the considerations for each of the above points are decided before you start the operation. This is the consideration of the whole to the details.

You wouldn't pick up a book and then think about how to find the author's name? Should you take it out if you find it? This is a very counter-intuitive approach.

However, Python beginners often use this kind of counter-intuitive programming-wherever they write, they think of it.

Now switching to Python question.

There are many text files in a folder. Each file is equivalent to a book, which contains the book title, author name and other information:

Why you always can’t learn Python? 4 pitfalls in getting started with Python

Here is a counter-intuitive way of writing it. At the end of this article, we will give you how to define a custom function. You can clearly feel the difference in thinking between the two writing methods.

Step one: How can we ensure that every file is taken out without omission or duplication?

After searching for "python folder file" online, I can find many ways, and I just used one of them:



1.  import os 

3.  for file in os.listdir(r'目标文件夹路径'): 
4.      # file 就是每个文件的路径 
5.      pass 




Step 2: There is a file path, how to read the contents?

Search online for "python reading files" and find:



1.  with open('文件路径(记得带后缀)', 'r') as f: 
2.      lines = f.readlines() 
3.      # lines 是一个列表,每个元素就是文件中的一行内容 


This step is actually the follow-up operation in the first step, so:



1.  import os 

3.  for file in os.listdir(r'目标文件夹路径'): 
4.      # file 就是每个文件的路径 
5.      with open(file, 'r') as f: 
6.          lines = f.readlines() 


Step 3: The content of the author line in the file is prefixed with "Author:". Given this line, how do you find the author's name in it?

This is normal string manipulation:



1.  '作者:小明'.split(':')\[1\] 


This should be a must-learn method for getting started. Of course, you can also search for "python string splitting" online.

So, the code now looks like this (I also took out the title of the book):



1.  import os 
2.  for file in os.listdir(r'目标文件夹路径'): 
3.      with open(file, 'r') as f: 
4.          lines = f.readlines() 

6.          # 第三步 
7.          book = lines\[0\].split(':')\[1\] 
8.          author = lines\[1\].split(':')\[1\] 


Step 4: Determine whether the book title is what we are looking for and take it out if it matches.

This uses if judgments and basic sequence operations:



1.  import os 

3.  # 第四步 
4.  results = \[\] 
5.  target = '小明' 

7.  for file in os.listdir(r'目标文件夹路径'): 
8.      with open(file, 'r') as f: 
9.          lines = f.readlines() 
10.          book = lines\[0\].split(':')\[1\] 
11.          author = lines\[1\].split(':')\[1\] 

13.          # 第四步 
14.          if target==author : 
15.              results.append(book) 


Now, the results list is the results.

The code seems simple, but if the book is now stored in an Excel file instead of a text file, can you immediately know where to modify it?

Beginners are often frustrated by details like this. I obviously understand what others have written, but I am confused when it comes to solving my own problems.

This is because there is a knowledge point in Python that perfectly matches the process of "overall to details"! But beginners generally don't use it very much.

Be sure to learn custom functions

Why do programming languages ​​basically have the characteristics of custom functions? Because this is in line with our thinking logic for solving problems.

Still solving the previous problem:



1.  # 第一步:从书架上取出书 
2.  def get\_file\_paths(folder): 
3.      pass 

5.  # 第二步:看封面,得知书名与作者 
6.  def get\_book\_message(file): 
7.      pass 
8.      return book,author 

10.  # 第三步:看看是否符合 
11.  def match(author): 
12.      return author=='小明' 


Why do you feel that the last step is missing, "removing the eligible books"?

Take a look at the overall call:



1.  results=\[\] 

3.  for file in get\_file\_paths(r'目标文件夹路径'): 
4.      book,author = get\_book\_message(file) 
5.      if match(author): 
6.          results.append(book) 


  • The logic of "taking out books that meet the criteria" is included in the overall process

Next, just implement each custom function one by one. The solution is the same counter-intuitive idea as before.

But why does it feel like there is more code now than before?

This is true, but if the information is now saved in excel, you will know immediately which function to modify, and the burden of modification will be much less

Why?

Because the function definition has constraints, look at the function definition of get_book_message above. A file path must be passed in and a tuple (book title, author) must be returned.

The overall process and the functions of each other step are that no matter how you get this tuple from a file path, the process is not important, the result is the most important.

How to advance

Summary of the above (for entry-level):

  1. Focus on simple grammar learning (if, for)
  2. You need to understand basic sequences (lists, tuples), but you don’t need to memorize their object operation methods.
  3. Learn how to break down problems
  4. Learn to customize functions

In fact, point 3 is the most important, the other points just serve him

Therefore, the advancement of Python still revolves around point 3.

For example, in the previous example, the overall process code still contains the logic of "removing books that meet the conditions", which is actually unreasonable. Then at this time you will learn new syntax knowledge points, allowing you to simplify the overall process code.

This may require you to learn:

  • lambda
  • The definition of higher-order functions (proper nouns are scary, but in fact they can pass logic to function parameters)

Another example:



1.  # 第二步:看封面,得知书名与作者 
2.  def get\_book\_message(file): 
3.      pass 
4.      return book,author 


This function only returns the book title and author name. If there is other information, the code of the overall process will be very troublesome.

At this time, you need to learn object-oriented knowledge: such as defining classes (in fact, named tuples can also be used)

Guess you like

Origin blog.csdn.net/2301_78096295/article/details/130685423
Recommended