Why you always can't learn Python, 4 traps of getting started with Python


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

Most of the Python introductory books on the market have very long directories (after all, they lack an important knowledge point and will be approved), but as a starting point, do you really need to learn all the knowledge points?

For beginners, looking at these book catalogues and learning will often encounter various pitfalls. Today I will share my personal views.

Too much grammar

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

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-unable to withstand the temptation of the "directory", feeling 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 probably won't be able to use them in the short term).

So, what grammar do you need to learn?

  1. Branch judgment
  2. cycle

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

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

I often see someone suggesting to enter each piece of code by hand, you must think twice, this can only improve your keyboard typing skills

For a while, I often need to switch multiple programming languages ​​back and forth, but it doesn’t test my grammatical memory level. This is the benefit of code snippets.

Little friend: "What about other grammatical rules, such as operator precedence, I can't remember what to do?"

In fact, I never remembered the priority of operators, because when there are multiple operators, I always use parentheses to clarify their priority:

Little 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 if grammar is not how to write, but how to construct bool values, which requires you to further understand the basic data types (str, int, bool, etc.).

Fortunately, the number of these knowledge points is very small, and there is no need to understand each type of storage mechanism (such as how many bytes are required) for getting started.

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, the beginner will fall into another pit-there are many ways to remember the list!

Similarly, I don't recommend beginners to memorize these methods, just remember the most commonly used 1 or 2 operations. such as:

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

In the same way, there are many ways to deal with strings, usually we only need to learn a few methods.

Little friend: "?! That's it?"

In fact, you may rarely use the above two operations later. Because in Python, it is more inclined to construct new sequences, rather than operate on the sequence in situ.

When you learn the deduction later, you will find that the most used grammars are 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, a pile of files, each file will have a pile of data.

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

Should I learn the dictionary at this stage? I suggest that you try to understand it. If you find that you can't understand it, then skip it. Because there will always be his application scenarios in the later period, then it will be easier to combine the scenarios to learn

Learn to break down the problem of thinking

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

At this point you fall into another trap-write code while thinking about logic

Most introductory books will not teach you this, because this is not a feature of Python, but it is extremely important.

The essence of programming is to express the logic of reality in code

In reality, when we want to accomplish a more complicated thing, we first consider the overall process, divide into multiple sub-processes, and finally consider the details for each sub-process.

Cases are indispensable in my article.

Consider the following realistic scenario: You want to find a book by a certain author from the bookshelf in your home (there are more than 100 books).

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

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

  1. First of all, you will consider where to start, there is always a search direction. For example, sweep across each row of books from the upper left corner of the bookshelf
  2. Secondly, before you start looking, you decide to find a book and look at the name of the author on the cover
  3. Finally, if you meet the conditions, you need to take it out to distinguish it from the original book

Note that each of the above considerations is decided before you start the operation, this is the consideration from the whole to the details.

You don’t just pick up a book and then think about how to find the author’s name? If you find it, do you want to take it out? This is very counter-intuitive.

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

Now switch to Python questions.

There are many text files in a folder, and each file is equivalent to a book, which contains information such as the title of the book and the name of the author:

The following is a counter-intuitive way of writing. At the end of this article, we will give you the method of customizing functions. You can clearly feel the difference in thinking between the two ways of writing.

Step 1: How can we ensure that every file is taken out without omission or repeated removal?

After searching for "python folder file" on the Internet, there are many ways to find, I just use one of them at will:

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

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

Search for "python read file" on the Internet and find:

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

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

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

The third step: the file contents of the line is the "author:" prefix, to give you this line, how the inside of the proposed name of the author?

This is a normal string operation:

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

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

So, now the code is like this (take out the title of the book easily):

import os 
for file in os.listdir(r'目标文件夹路径'): 
    with open(file, 'r') as f: 
        lines = f.readlines() 
         
        # 第三步 
        book = lines[0].split(':')[1] 
        author = lines[1].split(':')[1] 

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

This uses if judgment and basic sequence operations:

import os 
 
# 第四步 
results = [] 
target = '小明' 
 
for file in os.listdir(r'目标文件夹路径'): 
    with open(file, 'r') as f: 
        lines = f.readlines() 
        book = lines[0].split(':')[1] 
        author = lines[1].split(':')[1] 
         
        # 第四步 
        if target==author : 
            results.append(book) 

Now, the results list is the result

The code seems simple, but if the book is no longer a text file, but an Excel, can you know where to modify it all at once?

Beginners are often frustrated in such details. Obviously I understand what other people write, but when I solve my own problems, I am confused

This is because there is a knowledge point in Python that can perfectly match the "whole to detail" process! 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 to solve problems.

Still solve the previous problem:

# 第一步:从书架上取出书 
def get_file_paths(folder): 
    pass 
 
# 第二步:看封面,得知书名与作者 
def get_book_message(file): 
    pass 
    return book,author 
 
# 第三步:看看是否符合 
def match(author): 
    return author=='小明' 

How do you feel that the last step is missing, "take out the qualified book"?

Look at the overall call:

results=[] 
 
for file in get_file_paths(r'目标文件夹路径'): 
    book,author = get_book_message(file) 
    if match(author): 
        results.append(book) 
  • The logic of "take out eligible books" is included in the overall process

The next step is to implement each custom function one by one. The solution is the same as the previous counter-intuition.

But how do you feel that the amount of code is now more than before?

This is true, but if the information is now saved in excel, you can immediately know 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 other functions of each step are 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

The above summary (for getting started):

  • Grammar learning is simple (if, for)
  • The basic sequence must be understood (list, tuple), but the object operation method does not need to be memorized specially
  • Learn to break down the problem of thinking
  • Learn to customize functions

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

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

For example, in the previous example, the overall process code still contains the logic of "getting out eligible books", which is actually not reasonable. Then you will learn new grammar knowledge points at this time, so that you can simplify the overall process code.

This may require you to learn:

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

Another example:

Step 2: Look at the cover and learn the title and author of the book

def get_book_message(file): 
    pass 
    return book,author 

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

At this point, you need to learn object-oriented knowledge: such as defining classes (its practical named tuples are also OK)

I think it’s best to learn everything based on your actual needs, because it’s the easiest to learn when there are usage scenarios.


Finally: a wave of software testing data sharing!

In the technology industry, you must improve your technical skills and enrich your practical experience in automation projects. This will be very helpful for your career planning in the next few years and the depth of your test technology mastery.

In the interview season of the Golden 9th and the Silver 10th, the season of job-hopping, organizing interview questions has become my habit for many years! The following is my collection and sorting in recent years, the whole is organized around [software testing], the main content includes: python automation test exclusive video, Python automation details, a full set of interview questions and other knowledge content.

May you and I meet and you will find something! If you want to exchange experience in software testing, interface testing, automated testing, and interviews. Follow WeChat public account:[Sad Spicy Strips]Receive a 216-page software test engineer interview book for free. And the corresponding video learning tutorials are free to share! Communication learning skirt:313782132

Recommend good articles:

Packaged as a test engineer with 1 year of work experience, my advice before the interview is as follows

What exactly should I learn in automated testing?

Why not consider Tencent for job-hopping? Talk about a little bit of the past between me and the goose factory

Which is more advanced, automated testing or manual testing?

Novice must see: How to write a qualified test case?

Python login interface test problem record and solution (dry goods)

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/113987759