In the process of learning Python, especially in the introduction stage of Xiaobai, you will encounter many bright labels of "others' home". Today, let's look at two common labels.
Zero basis
There are many cases of "zero foundation". Originally, Python programming is well mastered. When it comes to zero foundation, it immediately attracts a lot of white eyes, especially with the background of career change and interdisciplinary.
You know, other people's zero foundation may simply have not been exposed to python, but other programming languages are involved. You can't see the efforts he has made behind the scenes, even if he really starts from the bottom up. Therefore, we should face up to the label of "zero basis", which neither shows that Python is easy to learn, nor can it prove who is strong or weak.
For the zero foundation, first of all, it is clear that every new Python learner starts with zero foundation. It is meaningless to say these. Learning Python well does not lie in your basic starting point, but in the reserve of knowledge, the application of methods and the accumulation of practice.
quick get start
Generally, the label of "quick start" is closely followed by "zero foundation". With Python becoming more and more popular in recent years, copywriting for training and promoting Python learning can be seen everywhere. With the starting point of zero foundation and the final quick start, the effect of promotion and salary increase is realized. Isn't this the best advertisement of Python training institutions?
Python's simplicity is compared with other programming languages, not through learning time. Senior programmers can get started with Python for weeks or even days, while pure white may be confused for months or even years. Horizontal comparison at different levels is meaningless.
Quick start can be used to urge yourself vertically: if you can do it, don't be intoxicated, continue to deepen your learning; If you find it difficult to get started, you should put your mind right and fight steadily. After you have a general grasp of your foundation and ability, make reasonable planning and efficient practice to improve the quality of your learning.
02
Combined with my own experience in practicing Python, I recommend a learning method to Xiaobai friends: when there is an idea and demand, search the ready-made code to digest, and carry out integration and transformation by myself. It sounds like absorbing other people's skills for yourself, so I call myself "star sucking Dharma".
Star sucking method
Each script has its own premise, such as "if you want to practice this skill, you must * * first". We are not so strict, but we must learn to search efficiently.
Many, many Xiaobai will not search for questions. They would rather spend a lot of time in groups and forums to ask questions than directly find answers through search engines. This is a big taboo. At the same time, because it's really Xiaobai, you can't grasp the key points of the problem when asking questions. After talking for a long time, others don't understand what you really want. A lot of time and energy are wasted.
Find the appropriate reference materials, study the code in combination with others' explanations, and integrate and transform yourself after understanding it. First try to imitate learning, digest a general idea, and then take the initiative to implement it with code.
After the whole process, we search and filter the results by ourselves, study the complete code of others, imitate, modify and integrate to form our own new code, and the star attraction method will be practiced.
Case drill
Just a few days ago, a friend asked about using Python to implement a calculator. We took this question to practice the star sucking method.

The first is the search. In fact, the demand is to write a calculator with a graphical interface in Python. When searching, it should reflect:
- The first few search results may be advertisements, which are directly ignored;
- When selecting the posts to refer to, first give priority to the recently released ones with the best source code and instructions according to the posting time. After entering the post, take a look at the feedback and error in the comment area. If so, don't waste time to read them;
- When several articles of good quality enter the field of vision, choose the code as concise and detailed as possible for reference.
In addition, the selection of resources should be combined with personal needs, because I tried tkinter to write graphical interface before. This time, I intend to take this opportunity to learn Qt graphical interface programming, so I chose an article on PyQt5 implementation of calculator to learn.
https://blog.csdn.net/LindaMan_/article/details/81777543

The article is like a screenshot. The code is posted on the csdn blog with a more detailed description, and the GitHub code link is also posted. Here, Xiaobai should pay attention. If you have the opportunity, learn how to view and download the code on GitHub, and try to upload your code to GitHub for sharing to practice. The code of the reference article has more than 200 lines, and the first pile of import is completely incomprehensible, which is particularly frightening. Don't worry, let's take a look and try to understand absorption.
The first is the import area at the beginning:
import sys from PyQt5.QtWidgets import (QMainWindow, QWidget, QApplication, QLineEdit, QPushButton, QGridLayout) from PyQt5.QtGui import QRegExpValidator from PyQt5.QtCore import Qt, QRegExp
The first import sys. If you haven't touched it before, you can find that the sys module contains functions related to the Python interpreter and its environment. The next three PyQt5 related from import s are obviously the modules we want to use to construct the graphical interface. We won't delve into the details first and continue to look at the code.
class Calculator(QWidget): def __init__(self): def ui(self): def clear_line_edit(self): def deal_num_btn(self, sender_text): def deal_operator_btn(self, sender_text): def deal_point_btn(self): def deal_equal_btn(self): def show_msg(self): def auxiliary_calculate(self, first_num, second_num, operator: str): def calculate(self, operator='='):
Next, nearly 200 lines of code can sort out the above structure. In fact, it defines a class and declares various functions. We can also think that the code will instantiate this class and run a calculator object to realize various functions of the calculator.
The last four lines of code are the commands actually executed by the code:
if __name__ == '__main__': # All PyQt5 applications must create an Application object. The sys.argv parameter is a list of parameters from the command line app = QApplication(sys.argv) # cal is the Calculator we want to build. It is an instance of the Calculator class cal = Calculator() # Calculator exit related sys.exit(app.exec_())
To tell you the truth, I didn't know when I went through the basics__ name__ In this usage, the code to be executed is directly written and run. This usage is mainly used to allow the script module to be imported into other modules and executed by itself. That is, when this py is imported by other modules__ name__ It's not "_main_", Subsequent code will not be executed; When the PY is executed, the condition will be triggered to run its code directly.
The code actually executed is very complex at first glance. You can see the general information by analyzing and searching. Since PyQt5 is to be used as a graphical interface application, its style and function should be established according to its format. As we expected, the Calculator class will be instantiated, and the functions of the Calculator are ready in the definition of a long series of classes above.
# QWidget is the parent class of Cal class Calculator(QWidget): """ Basic interface of basic page of calculator, Complete basic calculations """ # __ init__ Refer to the link for the parameters passed in when creating an instance https://www.cnblogs.com/illusion1010/p/9527034.html def __init__(self): #The super() function is a method used to call a parent class (superclass) #Reference link https://www.runoob.com/python/python-func-super.html super(Calculator, self).__init__() #First define the ui() function, which will be defined in detail below self.ui() self.char_stack = [] # Stack of operands self.num_stack = [] # Stack of operands self.nums = [chr(i) for i in range(48, 58)] # It is used to judge whether the value of the button is the number chr(48)-chr(57) corresponding to the number 0-9 self.operators = ['+', '-', '*', '/','x^y'] # Used to determine whether the value of the button is an operator self.empty_flag = True # The meaning of this flag is to judge whether the calculator is started for the first time and there is no data in the display screen self.after_operator = False # After looking at the calculation of the calculator, for example, 1 + 2 is still displayed on the screen after + is input. After 2 is input, 1 is replaced. The function of this flag is like this self.char_top = '' # Keep the operation symbol at the top of the stack self.num_top = 0 # Keep the value at the top of the stack self.res = 0 # Keep the calculation results. After the calculator calculates once, continue to press the equal sign, and repeat the latest calculation of 1 + 2. After getting 3, press the equal sign to get 3 + 2, and so on # >Calculate first. Why is the same symbol changed to calculate later in order to facilitate an operation, # After you calculate an expression, you continue to press and hold the equal sign and perform the last symbolic operation self.priority_map = { '++': '>', '+-': '>', '-+': '>', '--': '>', '+*': '<', '+/': '<', '-*': '<', '-/': '<', '**': '>', '//': '>', '*+': '>', '/+': '>', '*-': '>', '/-': '>', '*/': '>', '/*': '>' }
There are some difficulties in this paragraph. For example, class Calculator(QWidget) inherits the form of parent class__ init__ Call of super() in. This paragraph is a unified definition and initial assignment for the variables and functions required by the calculator.
Then, the ui() related code defines the style and layout of the calculator graphical interface in detail, and binds the click triggered event to the button in the interface. That is, when we click the calculator number button or operator, the number or operator will be displayed in the display box; When you click "=", a series of operations will be performed and the results will be output.
Code download
Go through the source code in this order, and you will have a certain grasp of the overall idea and structure of the code. If we want to add functions to it, we only need to make relevant modifications in the function of adding buttons or changing layout, and then properly handle the bound click events, and a calculator optimized by us is born.
