---restore content starts---
Basic data type:
Set: is a non-repeating and unordered collection
Class set (object): "" " set (), new empty set object set (iterable), new set Object build an Unordered collection of unique elements. "" " def Add (Self, *args, * * Kwargs): # Real signature unknown "" " add an element to a set, adding elements this have no effect if the element is already present. "" " pass def Clear (Self, *args, **kwargs): # Real signature unknown ' "" Remove all Elements from the This set. Clear content "" " pass def copy (self, *args, **kwargs): # Real Signature unknown "" "Return a shallow copy of a set. Shallow copy "" " pass def Difference ( Self, *args, **kwargs): # Real signature unknown "" " return the difference of both or more sets as a new set. A exists, B does not exist (i.e. all elements, that is in this set and not the others.) "" " pass def difference_update (self, *args, **kwargs): # Real Signature unknown "" Remove all elements of another sets from this set. removes from the current collection and the same element in B "" " &nBsp; pass def Discard (self, *args, **kwargs): # Real Signature unknown& nbsp; "" " remove an Element from a set if it is a member. if the element is a member, does nothing. Removes the specified element without an error "" " pass def intersection (self, *args, **kwargs): # Real Signature unknown "" return the intersection of The sets as a new set. Intersection (i.e. All elements is in both sets.) "" " pass def intersection_update (self, *args, **kwargs): # Real Signature unknown "" "Update a set with the intersection of itself and another.& nbsp and update to a "" " pass def isdisjoint ( Self, *args, **kwargs): # Real signature unknown "" "Return True if both SE TS has a null intersection. returns True if there is no intersection, otherwise false "" " pass def Issubset (self, *args, **kwargs): # Real Signature unknown "" Report whether another set contains if this set. is a subsequence "" " pass def Issuperset (self, *args, **kwargs): # Real Signature unknown "" "whether this Set contains another set. is the parent sequence "" " pass def pop (self, *args, * *kwargs): # Real signature unknown "" " remove and return an arbitrary set element. Raises keyerror if the set is empty. remove element "" pass def Remove (self, *args, **kwargs): # Real Signature unknown "" " remove an element from a set; It must be a member. if the element is a member, raise a keyerror. Removes the specified element, no error "" " &nbSp; pass def symmetric_difference (self, *args, **kwargs): # Real Signature unknown& nbsp; "" " return the Symmetric difference of the sets as a new set. symmetric intersection & nbsp; (i.e. all elements, that is in exactly one of the sets.) "" " pass def symmetric_difference_update (self, *args, **kwargs): # Real Signature unknown & nbsp; "" "Update a set with the symmetric difference of itself and another. symmetric intersection, and update to a in the "" " pass def Union (self, *args, **kwargs): # Real signature unknown "" " &nbsP; return the union of sets as a new set. (i.e. all elements, that is in either set.) "" " pass def Update (self, *args, **kwargs): # Real Signature unknown "" "Update a set with the Union of itself and others. Update "" " pass
#书写格式re = value 1 if condition else value * If the condition is true Then ' value 1 ' is assigned to the RE variable, otherwise ' value 2 ' is assigned to the RE variable
Depth copy
One, number and string
Assignment, shallow copy, and deep copy are meaningless for ' numbers ' and ' strings ' because he always points to the same memory address.
Import copy# number and string n1 = 123#n1 = ' I am sunlieqi age ' Print (ID (n1)) ####### #赋值 ####### #n2 =n1print (ID (n2)) # # # # # # # # # # # # # # #n2 = Co Py.copy (n1) print (ID (n2)) # # # #深拷贝 # # #n3 = copy.deepcopy (n1) print (ID (n3))
Second, other basic data types
For dictionaries, tuples, and lists, the change in memory address is different when assigning, shallow copy, and deep copy.
1. Assign Value
Assign Value just create a variable that points to the original memory address, such as:
N1 = {"K1": "WU", "K2": 123, "K3": ["Alex", 456]} n2 = N1
2. Shallow copy
shallow copy, creating only the first layer of data in memory
Import Copy n1 = {"K1": "WU", "K2": 123, "K3": ["Alex", 456]} n3 = Copy.copy (N1)
Deep copy
deep Copy, rebuilds all data in memory (except for the last layer, i.e., Python's internal optimization of strings and numbers)
Import Copy n1 = {"K1": "WU", "K2": 123, "K3": ["Alex", 456]} N4 = Copy.deepcopy (n1)
Function
I. BACKGROUND
Before learning the function, always follow: process-oriented programming, that is, according to the business logic from the fall to implement the function, which often use a long code to achieve the specified function, the most common development process is to paste the copy, that is, the code block of the previous implementation to the current need to function, as follows:
While True:if CPU utilization > 90%: #发送邮件提醒 connect mailbox server send mail off connection if hard disk uses space > 90%: #发送邮件提醒 connect a mailbox server send mail close connection if memory consumption > 80%: Send mail #发送邮件提醒 connection Mailbox server Close connection
Look at the above code, if the contents of the statement can be extracted out of the loop to use, as follows
def send mail (content) #发送邮件提醒 Connect mailbox server send mail close connection while True:if CPU utilization > 90%: Send mail (' CPU alert ') I F HDD Use space > 90%: Send mail (' HDD alert ') if memory consumption > 80%:
The above two ways, the second time than the first reusability and readability is better, in fact, this is the function of the change and process-oriented programming differences:
- Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called
- Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."
Functional programming The most important thing is to enhance the reusability and readability of code, improve the reuse of code
Ii. Definition and use
def function name (parameter): ... function Body ... return value
Key to defining a function:
- def: A keyword that represents a function
- Function name: the names of functions, which are called later by function name
- Function Body: A series of logical calculations in a function, such as sending a message, calculating the largest number in a list, etc.
- Parameters: Providing data for the function body
- Return value: When the function is finished, you can undo the data for the caller.
1. Return value
A function is a function block that executes successfully or is required to inform the caller by the return value.
In the above points, it is more important to have parameters and return values:
def send SMS (): Send SMS code ... if send succeeded: return True Else:return False while true: # each time a send SMS function is sent, the return value is automatically assigned to result #, can be written according to result of the log, or re-send the operation result = Send text message () if result = = False: Log, SMS Send Send failed ...
2. Parameters
Why do we have parameters?
DEF CPU Alert mail () #发送邮件提醒 connect the mailbox server send mail off connect def hard drive Alert Mail () #发送邮件提醒 Connect mailbox server Send mail close connection def Memory Alert Mail () #发送邮件提醒 Connect mailbox server Send mail close connection while True: if CPU utilization > 90%: CPU Alert mail () if Hard disk Use space > 90%: hdd alert mail () if memory consumption > 80%: memory Alert Mail ()
Parameters are implemented
def send mail (message content) #发送邮件提醒 connect a mailbox server to send mail off connection while True: if CPU utilization > 90%: Send mail ("CPU alarm up.") if hard disk uses space > 90%: send mail ("hdd alarm up.") ") if memory consumption > 80%: Send mail (" Memory alarm up. ")
There are three different parameters to the function:
# ######### definition Function ######### # name is called function func formal parameter, abbreviation: Parameter def func (name): print name# ######### execute function ######### # ' Wup Eiqi ' is called the actual parameter of function func, abbreviation: argument func (' Wupeiqi ')
def func (name, age =): print "%s:%s"% (name,age) # Specify parameter func (' Wupeiqi ', 19) # Use default parameter func (' Alex ') Note: Default parameters need to be placed at the end of the parameter list
def func (*args): print args# execution mode one func (11,33,4,4454,5) # execution mode two li = [11,2,2,3,3,4,54]func (*li)----------------EF Func (**kwargs): print args# execution mode one func (name= ' Wupeiqi ', age=18) # execution mode Two li = {' name ': ' Wupeiqi ', age:18, ' gender ': ' Male '}func (**li)---------------------------------------------def func (*args, **kwargs): print args print Kwargs
e-mail instance:
Import smtplibfrom email.mime.text import mimetextfrom email.utils import formataddr msg = mimetext (' message content ', ' plain ', ' Utf-8 ') msg[' from '] = FORMATADDR (["Wu Jianzi", ' [e-mail protected] ') msg[' to '] = formataddr (["Leave", ' [email protected] ']) msg[' Subject '] = "subject" server = Smtplib. SMTP ("smtp.126.com") server.login ("[Email protected]", "email password") server.sendmail (' [email protected] ', [' [Email Protected] ',], msg.as_string ()) Server.quit ()
Built-in functions
Open function This function is used for file processing
Python basic ternary operations, etc.