Article Directory:
- Environmental configuration and basic operations
- Call various packages
- Judgment, loop
- functions
- data structure
- character string
- Back to the local
- Command line version 2048 mini game
This is called entry, and the standard is that you can use this language to implement a less complex function without considering efficiency.
Of course, this ignorance of efficiency is not without principle. For example, I can do it in 1 hour, and it takes 1 day or 2 days for a novice who is just getting started. He will need a lot of time to check the library and search functions, but it will never happen. Used last week and a half.
I have never learned python. I was fed up with Matlab and wanted to convert a class written in Matlab into other languages. After a week, I put a thousand dollars covering image processing, data fitting and other functions. The Matlab code was successfully converted to Python. A few years later, Matlab banned us. It can be seen that the choice back then was extremely wise.
So let me rewrite that class now, it probably only takes less than a day. This is the difference between novice and veteran, but it is only the difference between novice and veteran.
From entry to proficiency, what is needed is not the improvement of thinking, but just a word-use, or more intuitively, it is to look at the amount of code. In this sense, the faster you get started, the earlier you can accumulate the amount of code, and thus become proficient in using Python earlier, instead of step-by-step from getting started to giving up. The scariest thing about learning programming is to write Hello World every day and feel full of rewards.
Next, get started quickly with Python.
1.Environmental configuration and basic operations
Learning Python is a fast word. Although it is not troublesome to download and install Python, novices often encounter various puzzling problems during use. So, if you want to get started in 1 hour, I recommend this. Online Jupyter editing tool, after entering, click Try JupyterLab in the middle to enter the online Notebook interface.
Click Python3 to quickly enter the jupyter notebook environment. Then according to intuition, write some four arithmetic expressions, such as these:
Then press Ctrl+Enter at the same time to run the python code.
Then press alt+enter to open the next section of code.
If you use shift+enter, the above two functions will be integrated. This section takes 3 minutes.
If you feel that the online Jupyter tool is not easy to use, and there is this ideone, select python after entering, and select run after entering the code.
2.Call various packages
In python, the call of the package is completed through import. For example, I want to call a calendar package and then print the calendar.
Then shift+enter, you can see the calendar.
If you want to perform scientific calculations through python, the most used packages are numpy and pyplot in matplotlib. The former is used for calculations and the latter is used for plotting. Usually the two are renamed to np and plt. Next, let’s demonstrate the usage of the two.
Words to remember in this section: import, as, numpy, matplotlib.pyplot, random.rand, scatter, plt.show.
This section takes time: 5 minutes.
3. Judgment, loop
If you want to count how many of the newly created x and y are greater than 0.5, then the method is simple: create two variables to store the number of the two, and then run a loop to judge.
You can understand range(100) as a set of 100 numbers from 0 to 99. For i in range(100) means that i traverses this set. The traversal is to copy to i one by one, so the loop is completed.
In python, the judgment and loop structure are separated from their content, and the sub-code block needs to be marked by a space. If there is only one line of code in the program block, it can be written directly after :.
If you want to know, x is in (0, 0.3], (0.3, 0.6], (0.6, 1] (0,0.3],(0.3,0.6],(0.6,1)(0,0.3],(0.3, What are the numbers of 0.6],(0.6,1) in these three intervals, you need to use if..elif
The function of for..in is to traverse a certain collection, and x itself is also a collection, so traversal can also be accepted.
Words to remember in this section: if, else, elif, for, +=,, it takes 5 minutes.
4. Function
If you want to repeatedly count the distribution of a group of random numbers, obviously you can’t write the old code every time, but you should encapsulate the code in a function.
In Python, create a new function through def. The parentheses after the function name are the variables that the function needs to enter. Finally, a value is returned by return. If you follow the mathematical expression, the above code is almost similar to the feeling of xNum=statis(x).
After a function is built, it can be called. This section takes 5 minutes, and you need to remember def and return.
5. Data structure
We just said that x is a set, which is not accurate. Mathematical collection requires no repeated elements, but x is a randomly generated array, so this cannot be guaranteed.
In this section, we need to understand the 5 most commonly used data structures in python: tuples, lists, dictionaries, sets, and arrays. At the same time, we must master the python deduction, which is expected to take 15 minutes.
As you can see, even though we gave 1,2,3,3 when creating a new set, there are no duplicate elements in the set, so there is only one 3 left.
Distinguish these five types in one sentence:
Only the array np.array can be calculated
The dictionary is indexed by key-value pairs
The collection has no duplicate elements and is not indexable
Tuples can be used as dictionary keys, but lists cannot.
The detailed differences between these five data types are as follows (this is not practical to remember).
Among them, hashable can temporarily be understood as a key that can be used as a dictionary.
The so-called pointer type is a name I chose randomly, which means that the element can be taken out like a pointer. Among them, the set and dictionary have no changes because the elements cannot be repeated.
As for computability, it is easier to understand. For example, + test, dictionaries and collections simply report errors, and tuples and lists will be + overloaded as a combination, and only arr performs the addition operation.
In python, the length of these data structures can be obtained through len.
6.character string
We have actually used strings in dictionaries. In python, single quotation marks or double quotation marks are used to represent strings. The two are equivalent.
Because quotation marks are used to mark strings, if you want to enter quotation marks in a string, you need to escape, and the transfer symbol is \. And because \ is used for escaping, \ also needs to be escaped.
7 Back to the local
Download python, or download anaconda. After downloading, you can use win+r, enter cmd to enter the command line, and enter python to enter the python environment.
If you download python, install numpy through pip isntall numpy. In short, the command format is pip install XXX; if you download anacoda, you can also install it through conda install numpy-of course, the basic environment of anaconda has been installed Most commonly used packages.
If the python code is encapsulated into a file in .py format, then it can be called and executed through python XX.py.
8 Command line version 2048 mini game
It stands to reason to see here and write here, it should be considered as a completion of the entry, then the next step is to use the knowledge learned in the entry to write a command-line version of the 2048 mini game.
The logic of 2048 is nothing more than operating 4×4 squares. There is a number in each square. We can manipulate these numbers to move. If two identical numbers collide under our operation, then they can be merged.
And this 4×4 square is nothing more than a matrix. Our operation can be understood as inputting characters, using wsad to represent up, down, left, and right, y to confirm, and n to cancel.
Python’s function to receive characters is input, for example:
To create a matrix, you can use np.zeros([4,4]).astype(int), which means to create a 4×4 4\times44×4 all-zero matrix, and transform it into an integer.
There are only 16 elements in the matrix. Although the loop efficiency is low, it is enough to satisfy human operation speed.
If the reader has typed all the code from beginning to end, he must now be able to write such a program independently. So the specific code will be given in the next article.