Basic data types
Integer int immutable
string str immutable
list list ordered, variable
tuple ordered, immutable
Dictionary dict: key is the only unordered, variable
set set unordered, variable
Boolean: bool
single equal sign Is an assignment, the two equal signs are comparison values, and is is the id of the two variables compared
Basic operator
/ The result retains the fractional part, the normal operation result /// Modulus, only retain the integer part % Take the remainder
Variable and immutable types
Variable: list, set, dictionary
Immutable: integer, floating point, string, tuple
Variable type: when the value changes, the id does not change, indicating that you cannot modify it based on the original value Hash
immutable type: when the value changes, the id changes, indicating that a new value can be hashed
1. The difference between lists and tuples
1 . Similarities list container object and the type of the ancestral sequences are, can store any type of data 2 . Differs addition difference literally (brackets and brackets), a list of variable data type, tuples are immutable Data type Yuanzu is used to store heterogeneous data, that is, some unrelated data, such as using Yuanzu to record a person's height, weight, and age. Lists are generally used to store isomorphic data, that is, some data that has the same meaning, for example, they are all string types.
2. Add elements to the list? The result of using "+" sign in the two lists is different from append?
a = [1,2,3 ] b = [4,5,6 ] print (a + b) # [1, 2, 3, 4, 5, 6] a.append (b) print (a) # [ 1, 2, 3, [4, 5, 6]]
Use the "+" sign to open each list element, as an element,
use append to add the added list as an element at the end
3.
The difference between global variables and local variables lies in scope. Global variables are declared in the entire py file and can be used in the global scope;
local variables are declared inside a function and can only be used inside the function. External), you will get an error
__name__ is a built-in variable
1. If you add global before the variable in the function, it is clear that the variable is a global variable (global is locally modified into global immutable data)
2. If a variable in the function has the same name as the global variable, but the function does Assign a value to this variable, then the variable is a global variable
3. If a variable in the function has the same name as the global variable, but the function assigns a value to the variable, the variable is a local variable
4. If the variable in the function does not have the same name Global variable, then the variable is obviously a local variable
4.Python garbage collection mechanism
1. Reference counting: When the data in the memory is not bound to any variable name, it will be automatically recovered 2. Mark clear: When the memory is about to be filled by an application, it will automatically trigger the clear 3. Generation recycling: According to the different survival time of the value, it is divided into different levels. The higher the level, the lower the probability of scanning by the garbage collection mechanism.
5. Operator overloading (in simple terms, it is a custom class, rewriting the magic method to implement the operator)
https://blog.csdn.net/zhangshuaijun123/article/details/82149056
What is operator overloading: Allow objects (instances) generated by a custom class to be operated using operators Operator overloading of arithmetic operators: (mainly using magic methods to implement operations) Method names Operators and expressions Description __add__ (self, RHS) Self + RHS adder __sub__ (Self, RHS) Self - RHS subtraction __mul__ (Self, RHS) * Self RHS multiplication __truediv__ (Self, RHS) Self / RHS division __floordiv__ (Self, RHS) // Self RHS floor except __mod__ ( self, rhs) self% rhs modulo (remainder) __pow__ (self, rhs) self ** rhs power operation
Examples
class Mynumber: def __init__ (self, v): self.data = v def __add__ (self, other): # other is an object x = self.data + other.data return x def __sub__ (self, other): x = self .data- other.data return x n1 = Mynumber (100 ) n2 = Mynumber (200 ) n3 = n1 + n2 # n3 = n1 .__ add __ (n2) Plus sign trigger __add__Magic method print (n3) # 300 n4 = n2-n1 #n4 = n2 .__ sub __ (n1) Minus sign triggers __sub__ magic method print (n4) # 100
6. F query and Q query in Django ORM
F query: The essence is to get the value of a field directly from the database
Query database for books that are larger than sold
from django.db.models import F res = models.Book.objects.filter (kucun__gt = F ( ' maichu ' )) # kucun and maichu are both fields of the Book table
Increase book prices by 10
model.Book.objects.update (price = F ( ' price ' ) +10) #All book prices increase by 10
(Note that the field of the modified char type cannot follow the above method, you need to use Concat, plus the splicing value)
from django.db.models.functions import Concat from django.db.models import Value ret3 = models.Book.objects.update(title=Concat(F('title'), Value('新款'))) models.Book.objects.update(title = F('title')+'新款') # 不能这么写
Q query (filter query conditions are AND, Q supports NOR)
Check if the book name is Romance of the Three Kingdoms or the price is 444
from django.db.models import Q res = models.Book.objects.filter (title = ' Three Kingdoms ' , price = 444.44) # filter only supports and relationship res1 = models.Book.objects.filter (Q (title = ' Three Kingdoms ' ), Q (. price = 444)) # If you use a comma or relationship and RES2 = models.Book.objects.filter (Q (title = ' Three Kingdoms ' ) | Q (. price = 444)) # or relationship = models.Book.objects.filter RES3 (~ Q (title = ' Three Kingdoms ' ) | Q (. price = 444)) # query title is in addition to the Three Kingdoms, or the price of a book 444
7. Exception handling: try ... except ... else ..., try ... finally ... the process of execution
When the code block in the try does not detect an exception, it will go to the else code, finally whether or not there will be an error will eventually go.
Common error types
NAMERROR Name error
SyntaxError Syntax error
KeyError Key does not exist
ValueError Value error
IndexError Index error
the try : need to check the code for the except Exception: # anomalies all exception types are captured Universal Print ( ' I invincible ' ) the else : Print ( ' code is detected without any exception will go the else ' ) a finally : Print ( ' No matter whether the detected code has an exception or not, it will be executed after the code is completed ' ))
8. File operation under Linux:
View the last 50 lines of the file: tail -n 50 filename vim operation commands: three common modes 1. Normal mode (this mode is the first time you open the document) 2. Edit mode (press i, o, a and other keys, it will switch from normal mode To edit mode, you can write to the document) 3. Command line mode (press ESC key in edit mode to enter the normal mode, enter: q to exit,: wq to save and exit, and: q! To force to exit)
9. Code Demo
1. Print False print (bool (0)) print (bool ( '' )) in Python 2. List operation result [ 1,2] * 2 # [1, 2, 1, 2] 3. == and is The difference == is the size of the comparison value is is the comparison of whether the id value is consistent 4. Variable assignment reference problem a = 2 b = a a + = 2 print (a, b) # 4, 2 5. Function passing parameters, decompress def func (a, b, c = 3, d = 4 ): print (a, b, c, d) func ( 3, * (3, 4)) # 3,3,4,4 func(3, *(3, 4), d=7) #3,3,,4,7
Function parameter
1. Position parameter: position actual parameter, position formal parameter 2. Keyword parameter: assign the value to the variable name in the stage of function call 3. Default value parameter: assign the value to the variable name during the function definition stage 4. Indefinite length parameter: * And ** From the perspective of formal parameters * : Receive redundant position actual parameters, put them in tuples uniformly, and then pass them to the variable names behind. From the perspective of actual parameters * : View tuples, lists, and strings Wait for the data to be scattered, and then pass it to the formal parameters one by one . From the perspective of the formal parameters ** : Receive the redundant keyword parameters, put them in the dictionary, and then pass them to the variable names behind. From the perspective of the actual parameters * *: Break the dictionary data into key = value form, and then pass it to the formal parameter