Set
1.set is used to store a set of distinct key values, but does not store its value.
There are two ways to create 2.set:
- With the set function, the parameter is a list collection, for example: S=set ([1,2,3,4,5]);
- The set literal wrapped in curly braces, for example: s={1,3}.
3.set Common operations
- The Add (key) method adds an element to the set and does not take effect if the element already exists
- The Remove (key) method can delete an element.
- | Operator can take set merge set
- & operator Fetch set intersection
- -Find the difference of two sets
- ^ Ask for an XOR of two sets.
# Create Seta_set = {1, 2} # Single value set does not need to add comma A_set = set (a_list) # Create Empty Seta_set = Set () # Create empty dictionary A_set = {} #由于从 Python 2 inherited the bizarre rules of history that cannot Use two curly braces to create an empty collection # Add a value A_set.add (4) # accepts a single parameter a_set.update ({2, 4, 6}) A_set.update ([Ten, 3, +]) a_set.update ({6,, 9}, {1, 2, 3, 5, 8, 13}) # Delete value A_set.discard (10) # If the element does not exist then do nothing A_set.remove (21) # If the element does not exist then the error a_set.pop () # Random Delete value (set unordered), if the set is empty then the error a_set.c Lear () # Empty set, equivalent to A_set = set () # detection value of A_set # merge Seta_set.union (B_set) # return a new Seta = a | B # equivalent to the previous one, the same as # two set intersection a_set.intersection (b_set) # returns a new Seta = A & b# culling seta_set.difference (b_set) # return A_set does not contain b_s The new seta of the ET value = a-b# non-intersection a_set.symmetric_difference (b_set) # returns the new Seta = a ^ b# symmetry that appears only in one of the set values, and the result is the true# set is unordered. Any set of two containing all the same values (without one omission) can be considered equal b_set.symmetric_difference (a_set) = = A_set.symmetric_difference (b_set) b_set.union (A_ Set) = = A_set.union (b_set) b_set.intersection (a_set) = = A_set.intersection (b_set) # Detect subset A_set.issubset (B_set) # Whether the former is a subset of the latter B_set.issuperset (A_set) # The former is the parent set of the latterTrinocular operator
A=1b=2if a<b: #一般条件语句的写法 k=aelse: k=b c=a if a<b else b
Deep copy Shallow copy 1, number and string
For numbers and strings, deep copy, shallow copy is no different, because for numeric numbers and strings once created can not be modified, if the string substitution operation, only in memory to re-produce a string, and for the original string, there is no change, based on this, Deep and shallow copies are no different for numbers and strings.
2. Data structures such as dictionaries and lists
Shallow copy
Deep copy
function 1, definition
Define a function to use the def statement, write down the function name, parentheses, the arguments in parentheses, and the colon : , and then write the function body in the indent block, returning the function's return value with a return statement.
The format of the definition function is:
def function name (parameter 1, Parameter 2, ..., parameter n): function Body (statement block)
2. Parameters
General parameters
The normal parameter is the parameter that the user fills in the calling function, and the parameter position must be consistent with the parameter.
def foo (p1,p2,p3,...)
Specifying parameters
Specifying a parameter is not required when the user invokes the function, as specified by the parameter in the function, which is the value that the parameter corresponds to.
def foo (p1=value1,p2=value2,...)
Default parameters
The default parameter can be written after the definition parameter, if the user calls the function is not set parameters, then the default parameters will be used, if the user specified parameters, then the user-specified parameters will replace the default parameters.
def foo (p1,p2=value2,...)
Dynamic parameters
Dynamic parameters can accept any parameters entered by the user, including dictionaries, lists, tuples, and other data types.
Dynamic Parameters--transport tuples
def foo (*args)
Dynamic Parameters--Transfer dictionary
def foo (**args)
def foo (X,y=2,*args,**args)
Python Learning Notes (iii)