In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the traps that need to be paid attention to in the introduction of Python knowledge". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. below, please follow the editor's way of thinking to study and learn "what are the traps that need to be paid attention to in the introduction to Python knowledge"?
Python is known for its simple syntax and few keywords, so it is often tricked by the major media that it is a very easy programming language to get started. His specific description is self-evident, but whether it is easy to get started or not is controversial. Because everyone's foundation is different.
There is too much grammar.
Are there many grammar rules in Python? In fact, a lot, if you have learned some ancient programming languages that are no longer updated, you will find that there is actually a lot of Python syntax.
Because Python needs to adapt to modern development requirements, he "forced to" constantly add new syntax features, such as "decorator", "walrus operator" and so on.
At this time, beginners will fall into the first trap-can not resist the temptation of the "directory", feeling that they will not be able to get started if they have skipped a certain knowledge point.
As a beginner, I don't recommend learning these things (chances are you won't need it in the short term).
So, what kind of grammar do you need to learn?
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Branch judgment
Cycle
To put it bluntly, it is the if and for cycle.
In fact, learning these grammars does not require you to remember how to write them. Most of the time, we do not need to type out every letter of these grammar sentences, because today's ide is very friendly and generally provides the function of generating code snippets. The following is a demonstration of vscode:
I often see some people suggest that you should enter each piece of code by hand. You must think twice. This can only improve your keyboard typing level.
There is a time when I often need to switch back and forth between multiple programming languages, but it does not test my grammar memory, which is the advantage of code snippets.
Buddy: "what about other grammatical rules, such as operator precedence, that I can't always remember?"
In fact, I have never remembered what operator priority, because when there are multiple operators, I always use parentheses to specify their priority:
Buddy: "it looks like it's easy to get started. Is if and for grammar really that easy to learn?"
Not that the focus of learning syntax like if 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 for starters, there is no need to have an in-depth understanding of each type of preservation mechanism (such as how many bytes are required).
The above knowledge points may be just one or three chapters in an introductory book.
The processing of sequences 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 (lists, tuples).
At this point, beginners will fall into another pit-the list has many ways to remember!
Similarly, I do not recommend beginners to memorize these methods, only need to remember the most commonly used 1 or 2 operations. For example:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Add element: append
Remove element: remove
Similarly, there are many ways to deal with strings, and usually we only need to learn a few methods.
Buddy: "?! that's it?"
In fact, you may not even use the above two operations later. Because in Python it is more likely to construct a new sequence than to manipulate the sequence in place.
When you learn the deduction later, you will find that the most frequently used grammars are if and for
You may wonder why the processing of sequences is so important.
No matter what programming language you learn, whether you are learning application development or ordinary office automation, most of the really complex logic comes from sequence processing.
For example, there is a pile of files, and there will be a pile of data in each file.
And simple single data, often the operation is very straightforward and simple.
Do you want to learn the dictionary at this stage? I suggest you try to understand, and if you find that you don't understand, skip it. Because there will always be his application scenario in the later stage, it will be easier to learn by combining the scene at that time.
Learn the thinking of decomposing problems
After the above two stages of study, you will find that at best you can only solve the simple problem of addition, subtraction, multiplication and division in primary school, and you have no clue about a slightly more complicated problem.
At this point, you fall into another trap-- thinking about logic while writing code.
Most introductory books won't teach you this because it's not a feature of Python, but it's extremely important.
The essence of programming is to express realistic logic in code.
In reality, when we want to complete a more complex thing, we first consider the overall process, divided into multiple sub-processes, and finally consider the details for each sub-process.
There are some cases in my article.
Consider the following realistic scenario: you want to find an author's book from your home shelf (there are more than 100).
You may think this matter is very simple, don't you just start from the beginning, look at the author's name for each book, and take it out if you match it?
This thinking process is actually a process from the whole to the details:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
First of all, you will consider where to start looking, there is always a search direction. For example, books that sweep across each line from the upper left corner of the shelf
Second, before you start looking, you decide to find out the name of the author on the cover of a book.
Finally, if you meet the requirements, you should take it out and distinguish it from the original book.
Note that the thinking of each of the above points is decided before you start to operate, and this is the consideration from the whole to the details.
You don't pick up a book and think about how to find the author's name. Do you want to take it out if you find it? This is very counterintuitive.
On the other hand, Python beginners often use this kind of counter-intuitive programming-- where to write and where to think.
Now switch to the Python problem.
There are many text files under a folder, each of which is equivalent to a book with information such as the title of the book, the name of the author, and so on:
Here is a counterintuitive way to write it. At the end of this article, I will give the method of custom function, you can obviously feel the difference between the two ways of writing.
Step 1: how can you ensure that every file is taken out without omission and will not be repeated?
After searching for "python folder files" on the Internet, I can find a variety of ways, and I use one of them casually:
Import os for file in os.listdir (r 'destination folder path'): # file is the path pass of each file
Step 2: there is a file path, how to read the contents of it?
Search for "python read File" on the Internet and find:
With open ('file path (remember with suffix)','r') as f: lines = f.readlines () # lines is a list, and each element is a line of content in the file
This step is actually the follow-up to the first step, so:
Import os for file in os.listdir (r 'destination folder path'): # file is the path of each file with open (file,'r') as f: lines = f.readlines ()
Step 3: the content of the author line in the document is prefixed with "author:". If I give you this line, how to put forward the author name inside?
This is a normal string operation:
Author: Xiao Ming. Split (':) [1]
This should be the method you must learn to get started, of course, you can also search for "python string segmentation" on the Internet.
So now the code looks like this (easily take out the title of the book, too):
Import os for file in os.listdir (r 'destination folder path'): with open (file,'r') as f: lines = f.readlines () # third step book = lines [0] .split (':') [1] author = lines [1] .split (':') [1]
Step 4: judge whether the title of the book is what we are looking for and take it out in accordance with it.
This uses if judgment and basic sequence operations:
Import os # step 4 results = [] target = 'Xiaoming' for file in os.listdir (r 'destination folder path'): with open (file 'r') as f: lines = f.readlines () book = lines [0] .split (':') [1] author = lines [1] .split (':') [1] # step 4 if target==author: results.append (book)
Now, the results list is the result.
The code seems simple, but if the book is no longer stored in a text file, but instead of an Excel, can you know where to change it at once?
Beginners are often frustrated by such details. I clearly understand what others have written, but I am confused when I solve my own problems.
This is because there is a knowledge point in Python that perfectly matches the "whole to detail" process! But beginners generally don't know how to use it.
Be sure to learn custom functions
Why do programming languages basically have the characteristics of custom functions? Because it is in line with the logic of our problem-solving thinking.
Still solve the previous problem:
# step 1: take the book from the shelf def get_file_paths (folder): pass # step 2: look at the cover and get the title and author def get_book_message (file): pass return book,author # step 3: see if it conforms to def match (author): return author==' Xiaoming'
How do you feel like you're missing the last step, "take out a book that meets the criteria"?
Take a look at the overall call:
Results= [] for file in get_file_paths (r 'destination folder path'): book,author = get_book_message (file) if match (author): results.append (book)
The logic of "taking out qualified books" is included in the whole process.
Next, you can implement each custom function one by one. The solution is the same as before counterintuitive.
But how does it feel that there is more code now than before?
That's true, but if the information is now stored in excel, you can immediately know which function is modified, and the burden of modification is much less.
Why?
Because the function definition is constrained, take a look at the function definition of get_book_message above, you must pass in a file path, and you must return a tuple (book title, author).
And the whole process and the function of each step is 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
The summary above (for getting started):
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Grammar learning is simple (if, for)
The basic sequence needs to be understood (list, tuple), but its object operation method does not need to be memorized.
Learn to think about breaking down problems
Learn to customize functions
In fact, point 3 is the most important, and the other points are just for him.
Therefore, the advance of Python still revolves around point 3.
For example, in the previous example, the overall process code still contains the logic of "fetching qualified books", which is not very reasonable. Then you will learn new grammar 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, which actually pass logic to function parameters)
Another example is:
# step 2: look at the cover and get the title and author def get_book_message (file): pass return book,author
This function only returns the title of the book and the name of the author, and if there is any other information, the code of the overall process is also troublesome.
At this point, you need to learn object-oriented knowledge: such as defining classes (which can also be used to name tuples)
Thank you for your reading, the above is the content of "what are the traps that need to be paid attention to in the introduction of Python knowledge". After the study of this article, I believe you have a deeper understanding of the traps that you need to pay attention to in the introduction of Python knowledge, and the specific use needs to be verified in 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.