The Difference between python Complex Implementation (-2) 0.5 and Open Root Srt (-2)**
(-2)**0.5 and sqrt(-2) are different, the former is plural and the latter is wrong.
print((-2)**0.5) #Output: (8.659560562354934e-17+1.4142135623730951j) import math math.sqrt(-2)#Report errors ValueError: math domain error
An array of functions constructed by Python in a loop that returns the same value when the function inside the array is run
functions = [] for i in range(5): def f(x): return x + i functions.append(f) for f in functions: print(f(7))
The output of the above program is:
11 11 11 11 11
Why does Ming f(x) return x+i, and I changes from 0 to 4?Reasonably, the return value after executing f(x) should also change, so why do we have five identical values after executing f(x).
A: This is because the external variables stored by the functions in python are addresses.That is, I in x+i is the address, and the loop is finished.IThe value of this address becomes 4. So the last execution of f(7) is always 11==7+4.
Don't believe you can output id(i) to try.
Note: Python takes the address of a variable by ID (variable name).
Differences between extend() and append() in Python array list
Code examples
x = [1,2,3] print(x.extend([2,3,4])) """ Output: [1,2,3,2,3,4] """ print(x.append([2,3,4])) """ Output: [1,2,3,[2,3,4]] """
You can see:
- extend() is to merge parameters into the original list.(Fusion)
- append() adds a parameter to the original list as an element.(annexation)
Python class variable, the difference between class name variable (className.x) and self.x?
- Class name variables and self variables have exactly the same address for their initial values.(which means the same value)
- Using class names for variables does not affect the address of their variables.(in order to take variables by class name and modify variables so that the values of both ways of taking variables change)
- But modifying variables by self makes their addresses different.(This means that modifying the variables self takes will only affect the value of self in the way it takes variables, not the value of the way class names take variables)
PS: No one answered the question?Need Python learning materials?You can get it yourself by clicking on the link below
note.youdao.com/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76
See the code to understand these three sentences:
class Car: color = 'gray' def describe_car(self): return Car.color def describe_self(self): return self.color test = Car() # The class names Car.color and self.color are the same initial values, because their addresses are exactly the same at this time. print(test.describe_car()) #Output: gray print(test.describe_self()) # Output: gray print('Carcolor Address:',id(Car.color),'self.color Address:',id(test.color))#You can see if the addresses are the same # Modifying Car.color first affects self.color.Because at this point their addresses are exactly the same Car.color = 'red' print(test.describe_car()) #Output:red print(test.describe_self()) # Output:red print('Carcolor Address:',id(Car.color),'self.color Address:',id(test.color))#You can see if the addresses are the same # Modifying self.color does not affect Car.color because their addresses are different at this time. test.color = 'blue' print(test.describe_car()) #Output:red print(test.describe_self()) # Output:blue print('Carcolor Address:',id(Car.color),'self.color Address:',id(test.color))#You can see if the addresses are the same # After the last step they had different addresses.Modifying Car.color does not affect self.color.Because their addresses are different at this time Car.color = 'black' print(test.describe_car()) #Output:black print(test.describe_self()) # Output:blue print('Carcolor Address:',id(Car.color),'self.color Address:',id(test.color))#You can see if the addresses are the same
Tuple tuples cannot modify elements, which is one of the differences between tuple and List lists
x = (0,1,2) x[0]=-1#This is an error and will result in an error TypeError: 'tuple' object does not support item assignment
Python magic method magic method, the order of variables
_u mul_ (self, other), calling this magic method is self*other, which actually does this: self. u mul (other).