"2" common built-in functions
#(1) ScopePrint(Globals ())#global scope, showing all functions and variable names globallyPrint(Locals ())#local scope, showing the function and variable name of the current scope#(2) input and outputA = input ('Please enter the content:')#Input User Interaction command, the value returned is a string typePrint(A,type (a))" "print (self, *args, sep= ", end= ' \ n ', File=none)" "Print('123','456','789','987','654', sep='**')#The Sep parameter is used to split each character in print, the delimiter customPrint('123', end="')#The end parameter sets the end action for each print, the default is the newline character, and if the setting is "the final effect will print on one line 123456Print('456') with open ('Test.txt','W', encoding='Utf-8') as F:Print('123456789', file=f)#The file parameter saves the content that you want to print to your files and no longer prints the output through the screen#(3) memory-relatedt = (A) L= [A]i= 123Print(hash (t))#after the hash function is called, the hash value of a variable is returned, and the hash can only be used to not become a data type, such as for variable data types will be errorPrint(hash (l))Print(hash (i))#(4) file OperationWith open ('Test.txt','R', encoding='Utf-8') as F:#Open opens a file, the operation is divided into ' r,rb,r+,w,wb,w+,a,ab,a+ ', return a file handle Print(F.readlines ())#(5) View all built-in methods for the type of the parameterPrint(dir (list))#View the list's built-in methods, and return a listing#(6) data type#bool int float for cast of type#(7) Abs seek absolute valueA =-3Print(ABS (a))#returns the absolute value of the variable a#(8) Divmod, respectively, to take the business and take the surplusA = 15b= 2Print(Divmod (A, B))#returns a tuple, the first element is the quotient, the second element is the remainder#(9) Max returns the maximum value in the collectionA = [1,2,-3,-4]Print(Max (a))#min Returns the minimum value in the collectionA = [1,2,-3,-4]Print(min (a))#sum sums the setA = [1,2,-3,-4]Print(sum (a))#(round) roundingA = 1.5556Print(Round (a))#POW returns the Y power of xx = 5y= 2Print(Pow (x, y))#(14) lists and tuples#list and tuple casts for typeLis = [1,2,3,4]Print(Tuple (LIS))#() Str converted to String typeA = 123Print(str (a), type (str (a)))#(bytes) Convert string to bytes byteA ='World'Print(Bytes (a,encoding='GBK'))Print(Bytes (a,encoding='Utf-8'))#(+) repr convert an object to printable formatA ='qwe'repr (a)#The contents of the REPR output will be quoted, but cannot be output on Pycharm#print (a)#(reversed) reverse, reverse objects = [1,-2,2,4,5]s1= Reversed (s)#returns an Iterator object in reverse order forIinchS1:#For loop out element Print(i)Print(s)#(19) data collection#Dict,set (), Frozenset (non-modifiable collection)#() Len returns the set lengthPrint(Len ('123ASD'))#(Enumerate)#returns an enumerable object that returns a tuple for the next () method of the object.Lis = ['a','b','C'] forKvinchEnumerate (LIS):Print(k,v)#zip to match objects individuallyA = [A.]b= ['a','b','C']z=Zip (A, b) forIinchZ:Print(i)#(+) filter#filter, constructs a sequence that is equivalent to [item for item ' in Iterables if function (item)], sets the filter condition in the function, iterates through the iterator#element that returns a value of True when the element is left, forming a filter type datadefcompace (x):returnX > 5result= Filter (compace,[1,2,3,4,5,6,7,8,9,10,11]) forIinchResult:Print(i)#Map maps the specified sequence based on the provided function.Li = [1,2,3,4,5,6]result= Map (LambdaX:X * 2, Li) forIinchResult:Print(i)#(sorted) sorting the collectionLi = [ -1,2,3,8,4,6]li2= [9.5.4]Print(Sorted (LI))
Python's built-in functions