Summary of common Python knowledge points and python Summary

Source: Internet
Author: User
Tags union of sets

Summary of common Python knowledge points and python Summary

1. Set Basic Data Type

A. set is an unordered and non-repeating element set.

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, add the element This has no effect if the element is already present. "pass def clear (self, * args, ** kwargs): # real signature unknown" Remove all elements from 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 two or more sets as a new set. A exists, B does not exist (I. e. all elements that are in this set but not the others .) "" pass def difference_update (self, * args, ** kwargs): # real signature unknown "Remove all elements of another set from this set. delete the same element "" pass def discard (self, * args, ** kwargs) from the current set as B ): # real signature unknown "Remove an element from a set if it is a member. if the element is not a member, do nothing. remove the specified element. The error "pass def intersection (self, * args, ** kwargs) does not exist ): # real signature unknown "" Return the intersection of two sets as a new set. intersection (I. e. all elements that are in both sets .) "" pass def intersection_update (self, * args, ** kwargs): # real signature unknown "Update a set with the intersection of itself and another. take the intersection and update it to "pass def isdisjoint (self, * args, ** kwargs ): # real signature unknown "" Return True if two sets have a null intersection. if no intersection exists, True is returned. Otherwise, False "pass def issubset (self, * args, ** kwargs) is returned ): # real signature unknown "Report whether another set contains this set. whether the subsequence "pass def issuperset (self, * args, ** kwargs): # real signature unknown" Report whether this set contains another set. whether 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 not a member, raise a KeyError. remove the specified element. The error "pass def into ric_difference (self, * args, ** kwargs) does not exist ): # real signature unknown "" Return the specified Ric difference of two sets as a new set. symmetric intersection (I. e. all elements that are in exactly one of the sets .) "pass def into ric_difference_update (self, * args, ** kwargs): # real signature unknown" Update a set with the specified Ric difference of itself and another. symmetric intersection, and updated to "pass def union (self, * args, ** kwargs) in ): # real signature unknown "" Return the union of sets as a new set. union (I. e. all elements that are in either set .) "pass def update (self, * args, ** kwargs): # real signature unknown" "Update a set with the union of itself and others. update "" pass

B. Data Type module example

Se = {11,22, 33,44, 55} be = {44,55, 66,77, 88} # se. add (66) # print (se) # add an element and cannot print it directly! #### Se. clear () # print (se) # clear all values in the se set, and cannot clear a single ### ce = be. difference (se) # se exists, be does not exist in the value, must be assigned to a new variable # print (ce) ### se. difference_update (be) # print (se) # Delete the same value as be in se. You cannot assign a value to a new variable. Input and convert the value first, and then print it. You cannot print it directly! # Se. discard (11) # print (se) # Remove a specified element. If it does not exist, no error is returned. # se. remove (11) # print (se) # remove the specified element. If the element does not exist, an error is returned. # se. pop () # print (se) # Remove random elements ### ret = se. pop () # print (ret) # remove an element and assign the removed element to another variable # ce = se. intersection (be) # print (ce) # Get the intersection of the two sets (same element) # se. intersection_update (be) # print (se) # extract the intersection of the two sets and update them to the se set # ret = se. isdisjoint (be) # print (ret) # No intersection exists between two sets. If an intersection exists, False is returned. True is not returned. # ret = se. issubset (be) # print (ret) # determine whether the se is a subsequence of the be set. If it returns True, it does not return Flase # ret = se. issuperset (be) # print (ret) # determine if se is the parent sequence of the be set. If True is returned, Flase # ret = se is not returned. into ric_difference (be) # print (ret) # symmetric intersection, except for different elements # se. into ric_difference_update (be) # print (se) # symmetric intersection, retrieve different elements and update them to the se set # ret = se. union (be) # print (ret) # union, merge two element sets into a new variable

2. Deep copy

A, number, and string

For numbers and strings, assigning values, copying values, and copying values are meaningless because they always point to the same memory address.

Import copy ########## numbers, strings ######### n1 = 123 # n1 = "I am alex age 10" print (id (n1 )) ### assignment ## n2 = n1print (id (n2) ### shallow copy ## n2 = copy. copy (n1) print (id (n2) ### deep copy ## n3 = copy. deepcopy (n1) print (id (n3 ))

B. Other Basic Data Types

When assigning values, copying values, and copying values to dictionaries, parent databases, and lists, the memory address varies.

1. Assignment

Assign a value, just create a variable that points to the original memory address, such:

n1 = {"k1": "zhangyanlin", "k2": 123, "k3": ["Aylin", 456]}n2 = n1

2. Shallow copy

Shortest copy: only the first layer of data is created in the memory.

import copyn1 = {"k1": "zhangyanlin", "k2": 123, "k3": ["aylin", 456]}n3 = copy.copy(n1)

3. Deep copy

Deep copy: Creates a new copy of all data in the memory (excluding the last layer, namely, optimizing strings and numbers in python)

3. Functions

Function: encapsulate a function code into a function. You do not need to write it again later. You only need to call the function.
Object-oriented: classifies and encapsulates functions to make development faster, better, and stronger...

The main points of function definition are as follows:

Def: indicates the function keyword.
Function Name: name of the function. The function will be called Based on the function name in the future.
Function body: perform a series of logical calculations in the function, such as sending emails and calculating the maximum number in [11, 22, 38,888, 2...
Parameter: provides data for the function body.
Return Value: After the function is executed, the caller can return data.

1. Return Value

A function is a function block. Whether the function is successfully executed or not, the caller must be informed of the returned value.

Parameters and return values are important in the preceding key points:

Def send SMS (): the code for sending the SMS... if the message is sent successfully: return True else: return False while True: # The returned value is automatically assigned to result # Each time the SMS sending function is executed, and logs can be written based on the result, or resend and other operations result = send SMS () if result = False: SMS sending failed...

The function has three different parameters:

Common Parameters

########## Define a function #########
 
# Name is a formal parameter of the function func.
Def func (name ):
Print name
 
######### Execution function #########
# 'Hangyanlin' is the actual parameter of the function func.
Func ('hangyanlin ')

Default parameters

Def func (name, age = 18 ):

Print "% s: % s" % (name, age)
 
# Specify parameters
Func ('hangyanlin', 19)
# Use default parameters
Func ('Nick ')

Note: The default parameters must be placed at the end of the parameter list.
  

Dynamic Parameters

Def func (* args): print args # execution method 1 func (11,33, 4454, 5) # execution method 2 li = [11,2, 54] func (* li)
Def func (** kwargs): print args # execution method 1 func (name = 'wupeiqi ', age = 18) # execution method 2 li = {'name ': 'wupeiqi ', age: 18, 'gender': 'male'} func (** li)
 def func(*args, **kwargs):   print args  print kwargs

Email instance:

Def email (p, j, k): import smtplib from email. mime. text import MIMEText from email. utils import formataddr set = True try: msg = MIMEText ('J', 'plain ', 'utf-8 ') # j mail content msg ['from'] = formataddr (["Wu peiqi", 'wptawy @ 126.com ']) msg ['to'] = formataddr (["leave", '1970 @ qq.com ']) msg ['subobject'] = "k" # k topic server = smtplib. SMTP ("smtp.126.com", 25) server. login ("wptawy@126.com", "WW.3945.59") server. sendmail ('wpt Awy@126.com ', [p], msg. as_string () server. quit () Recipient T: set = False return Trueformmail = input ("Enter the recipient's email address:") zhui= input ("Enter the subject :") neirong = input ("Enter the email content:") aa = email (formmail, neirong, zhti) if aa: print ("the email is sent successfully! ") Else: print (" failed to send email! ")

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.