Hello Python:)
Supplemental data type: Set (simple memory: Duplicate list not allowed)
s = set () #创建空集合
S.add (11)
S.add (11)
For S1 in S:
Print (S1) #仅仅输出一个11
Details:
When you create a set collection, you can also accept tuples, strings, lists, and so on to iterate over objects.
Method:
Clear (): Empty
Difference ():
S1 = {11,22,33}
S2 = {22,44}
Print (s1.difference (S2))
#{11,33}
Print (S2.difference (S1))
#{44}
Difference_update ():
Print (S1.difference_updata (S2))
#None so no return value
Print (S1)
#{33,11} So this method is to find S1 exist and S2 not exist, update themselves.
Discard (): Removes the specified element, no error is present
Remove (): Removes the specified element, no error is present
Intersection (): takes the intersection of two several sets
Intersection_update (): Take intersection and update yourself
Isdisjoint (): Determine if there is an intersection
Issubset (): is a subsequence
Issuperset (): Is the parent sequence
Pop (): Removes an element and returns
Symmetric_difference ():
S1 = {11,22,33}
S2 = {22,44}
Print (s1.symmetric_difference (S2))
#输出 {33,44}
Symmetric_difference_updata (): ...
Union (): Set
Update (): Updated
----------------------set end, not very important ... ----------------
Ternary operations
Example: Name = "Kepler" If condition else "Stark"
If the condition is True,name = "Kepler"
If the condition is False,name = "Stark"
How different data types are stored in memory:
In the case of the C language, the list in Python is like a chain table.
STR is like an array, so it cannot be changed and can only be redistributed
Depth copy:
Import Copy
N1 = 123
N2 = Copy.copy (n1)
Print (ID (N1))
Print (ID (n2))
#输出的id号一样, this is a shallow copy.
N2 = Copy.deepcopy (n1)
Print (ID (N1))
Print (ID (n2))
#输出的id号一样, this is a deep copy.
#综上, the ID is the same for both numbers and STR, regardless of the depth of the copy, because Python itself optimizes
For lists and so on, the situation is different.
N1 = {one: "Kepler", "Air"}
N2 = Copy.copy (n1)
Print (ID (N1))
Print (ID (n2))
#id相同
Print (ID (n1[11)))
Print (ID (n2[11)))
#id不同
#综上, a shallow copy only copies the first layer, and the follow-up still points to the original address
N2 = Copy.deepcopy (n1)
#这时候, it will be copied to the number and STR layer, that is, how many layers will be copied until STR and digits (i.e. the last layer)
Function Preliminary:
Q: How do I define a function?
def first ():
Something ...
Q: How do I call a function?
First ()
Q: How does the function return?
Return Something
Q: What are the parameters of the function?
def first (args):
Something ...
# args is called the formal parameter, the notation is arbitrary, when called, First (123), 123 is called the actual parameters, this and Java, C and some other differences. It is important to note that when a function has multiple parameters, it can also be out of order, as long as the compiler is notified.
Default value:
def first (ARGS,ARGS1 = 123):
Something ...
#这个first函数, you can call it this way: first (110), firstly (110,220), where ARGS1 is not specified, the default value is 123, and the second is ARGS1 = 220 specified. It is important to note that to use the default parameters, you must put them behind or you can have more than one default parameter.
Dynamic Parameters:
As the name implies, the number of parameters can be passed.
A) def first (*args):
Something ...
#这样, the first () function can accept any number of arguments. Internal principle: The nature of the dynamic parameters is to pass in any number of parameters into the form of tuples, of course, can be nested and so on ...
b) def first (**args):
Something ...
#这种类型的动态参数要这样调用: First (k1=123,ke=234). The intrinsic essence is the conversion of incoming parameters into a dictionary. So when you pass in, you follow the definition of the dictionary, which is kv.
c) The two dynamic parameters above can be mixed, but also can be mixed with ordinary form parameters, but note a) in front of B).
Details:
def first (*args):
Print (args)
First ([11,22,33,44])
First (*[11,22,33,44])
#第一个输出: ([11,22,33,44],)
#第二个输出: (11,22,33,44)
def first (**args):
Print (args)
DIC = {"K1": One, "K2": 22}
First (DIC)
FIRSST (**dic)
#第一个会报错
#第二个才是正确的传入动态参数字典的方法, of course the KV form is certainly possible
Global variables and local variables:
Communicates with advanced programming languages such as Java.
If you want to modify global variables within a function (local variables), use the Global keyword.
Example:
p = "Kepler"
Def fun1 ():
p = "Air"
Def fun2 ():
Global P
p = "Stark"
Def fun3 ():
Print (P)
FUN1 ()
FUN3 ()
#输出Kepler
Fun2 ()
FUN3 ()
#输出Stark
Got it?
Pythonman (Fri)