#What are the basic knowledge of python language? Let me introduce them one by one below:
Data types
Commonly used data types: number, string, list (array), tuple (tuple), dict (dictionary)
Number (number)
The main numbers include: int (signed integer), long( Long integer type), float (floating point number), complex (plural number)
var1 = 1(int) var2 = 51924361L(long) var3 = 1.2(float) var4 = 3e+26J(complex)
String
Strings are identified by single quotes or double quotes. Numbers, letters, and underscores can be placed inside quotation marks or double quotation marks
Related recommendations: "python video tutorial"
str1 = "123456789" str2 = '我们都在学Python'
Arrays and tuples
List (list) and Tuple (tuple) are the most frequently used data types in Python.
Lists can complete the data structure implementation of most collection classes. It supports characters, numbers, strings and can even contain lists (i.e. nested).
Lists are marked with [ ] and are the most common composite data type in Python.
Tuples are marked with (), and internal elements are separated by commas. However, tuples cannot be assigned values twice and are equivalent to read-only lists.
The difference between List (list) and Tuple (tuple): List is a mutable sequence, and Tuple is an immutable sequence.
To put it bluntly, List can be added, deleted, modified, and checked, but tuple can only be viewed. (Immutable)
Dictionary
Dictionary is to find the value based on the key
Conditional judgment statement
Conditional judgment: Simply put, it is executed if the condition is met, and not executed if it is not satisfied. This is the same as our life. For example, when buying fruits, chestnuts, watermelons are 10 yuan each, and apples are 5 yuan each, and bananas 3 yuan each. If you bring 3 yuan, you can buy a banana, and if you bring 5 yuan, you can buy an apple.
A simple sentence: Execute from top to bottom. If you are satisfied, it will end. If you are not satisfied, go down.
Loop (iteration)
Loops are often used. The main reason for loops is efficiency. Think about it, if you want to get 1 to 10 It is impossible for us to output the numbers one by one. This would waste computer resources. So we directly throw a number to the computer, and it will automatically retrieve and output range (10): it is to generate an array, that is to say, as long as it is a sequence We can all iterate.
Function (function)
Scope:
Python uses the concept of namespace to store objects, this The namespace is the area in which objects operate, and different objects exist in different scopes. The following are the scoping rules for different objects:
Each module has its own global scope.
The object defined by the function belongs to the local scope and is only valid within the function and will not affect the objects in the global scope.
The assigned object is of local scope unless declared using the global keyword.
Parameter classification:
Default parameter: def function(ARG=VALUE)
Tuple parameter: def function( *ARG)
Dictionary parameters: def function (**ARG)
Rules:
The default value must be in non After the default parameters;
In a single function definition, only one tuple parameter (*ARG) and one dictionary parameter (**ARG) can be used.
tuple parameters must come after the connection parameters and default parameters.
Dictionary parameters must be defined last.
Summarize the function:
Points to note when defining:
[1]Indentation and colon
[2]If the defined function has not thought about what it wants, follow the grammatical rules and add a pass to avoid reporting errors
[3]Return represents The function has ended and the value returned to the caller
Parameters:
[1]Default parameters: Reduce the difficulty of using the function
[2]Can Variable parameters: to facilitate the improvement of our development efficiency
[3]Keyword parameters: mainly expand the function of the function
Recursive function:
Recursive It just calls itself continuously until the conditions cannot be met. The order of execution is to call multiple facts. When it is finally executed, the last fact is executed first, the result is returned (this result is used as the parameter of the second to last fact), and then the penultimate fact is executed. Two facts, and so on...
Module
The codes written by python all end with .py files, and this .py file It is the module. The purpose of this is to facilitate use
Usage rules:
[1]import our module name
[2] If we just want to use a specific function in the module, use from module name import module specific method name
[3] Sometimes the name of the module is very long and it is not very convenient for us to use it. Use Import module name as alias is to use as to give an alias to the module we use here. It is more convenient to use
[4] There is also the problem of module path: if it is not under the same path, we need to import it The full path of the module, not needed if the environment is configured
The above is the detailed content of What are the basics of python language?. For more information, please follow other related articles on the PHP Chinese website!