Deep understanding of Python3 built-in functions and deep understanding of python3

Source: Internet
Author: User
Tags vars

Deep understanding of Python3 built-in functions and deep understanding of python3

This article mainly introduces Python3 built-in functions and shares them with you as follows:

Built-in functions

The following code uses Python3.6.1 as an example:

# Coding = UTF-8 # builtin_function.py built-in Function import osdef fun (): all ([True, False]) # all elements of the iterator (empty or) are true, returns true => False any ([True, False]) # returns true => true num = abs (-1.23) if any element of the iterator is True) # absolute value num = pow (5, 3) # Power x ** y => 125 num = pow (5, 3, 3) # x ** y) % z => 2 num = round (1.23) # returns the approximate value of the floating point number. By default, the value is 0, and num = round (1.23, 5) #5 indicates the number of digits after the decimal point to be retained num = sum ([1, 2, 3, 4, 5]) # sums the list numbers and strs = ascii (OS) # returns Ascii string format of the image strs = bin (123) # convert the integer to a binary string (if the converted object is not of the int type, it can be defined in _ index _) boolean = bool (0) # obtain true/False (False: None/False/0/null of any type ""() [] {}/_ bool _ return False/_ len _ return 0) bytes = bytearray ("You were, are and will be in my heart! "," UTF-8 ") # string (string, encoding): Convert to byte sequence/number (number) by encoding: generate an empty array of the corresponding size/No parameter (): 0 array bytes = bytes ("You were, are and will be in my heart! "," UTF-8 ") # Same as bytearray (), but not variable strs = chr (123) # convert the integer (Unicode) to the char type (range: [0, 1 114]) num = ord ("{") # convert char type to INTEGER (Unicode) num = complex ("1 + 2j") # convert string or number to plural (cannot contain spaces) num = float ('1. 1 ') # build floating point strs = format (123, 'F') # format (type: x, B, f... /displacement:> (> 10), <, ^/length: 5/E) strs = hex (123) # convert the integer into a hexadecimal string strs = oct (123) # convert from decimal to an octal string num = int (123.1) # convert it to an integer strs = str (123) # convert the object to a string strs = str (B "123", "utf -8 ") elem = max ([1, 3, 4], [2, 4, 6]) # One iterable or multiple elements can be received => [2, 4, 6] elem = max (,) # => 4 elem = max ([, 6], key = lambda x: x = 2) # => 2 elem = min (1, 2, 3, 4) # contrary to max exe = compile ("print ('O _ o')", filename = 'strs ', mode = 'exec ') # compile as code, mode (compilation mode: executable statement 'exec', single statement 'eval', interactive statement 'single ') exec (exe) # Run the compiled code exec ("print ('O _ o')") # Run the String uncompiled code eval (compile ("print ('O _ o ')", filename = 'Strs', mode = 'eval') # The string type delattr (Clazz ("Hello! ")," Name ") # delete an object attribute = del clazz. name For details, see the reflection code block (http://www.bkjia.com/article/128897.htm) setattr (Clazz (" Hello! ")," Name "," World! ") # Assign strs = getattr (Clazz (" Hello! ")," Name ") # obtain the attribute value strs = getattr (Clazz (" Hello! ")," Name ",-1) #-1 returns boolean = hasattr (Clazz (" Hello! ")," Name ") # whether the object has this attribute lists = dir (Clazz (" Hello! ") # View the function clazz = type (OS) # obtain the type dicts = globals () # obtain the current global function and the object dicts = locals () # obtain the current local function and the object dicts = vars () # Same as locals () (_ dict _) dicts = vars (Clazz ("Hello! ") Num = hash (Clazz (" Hello! ") # Obtain the hash value of an object help (OS) # obtain the help document num = id (OS) of an object) # obtain the id boolean = isinstance (Clazz ("Hello! "), Clazz) # whether the object is an instance of this class boolean = issubclass (Clazz, Clazz) # Whether the class (Before) is a subclass of this class (after) (including itself) strs = repr (OS) # convert the object into a string expression #-super () # proxy parent class object, see the class article (http://www.bkjia.com/article/88315.htm) #-memoryview (obj) for details) # Memory view, detailed description of the data structure article (http://www.bkjia.com/article/128892.htm) dics = dict () # create a dictionary tups = divmod (10, 5) # Return tuples, (vendor (10/5 ), remainder (10% 5) lists = enumerate (['A', 'B', 'C']) # returns the enumerated object lists = filter (lambda x: true if (ord (x)> 66) else False, ['A', 'B', 'C']) # function is reserved for Frue, false remove sets = frozenset ([1, 2, 3]) # Return the new frozenset object (SET) num = len ([1, 2, 3]) # length lists = list (1, 'A') # convert to list type tups = tuple ([1, 2, 3]) # convert to tuple tuples type ran = range (5) # immutable sequence ran = range () ran = range (, 2) # (START, end, increase) sets = set ([1, 2, 3]) # returns the set maps = map (lambda x, y: x * y, [1, 2, 3], [65, 66, 67, 68]) # returns an iterator. elements are filtered by custom functions and multiple iterable parameters can be received: [65,132,201] iters = zip (["", "B", "C", "D", "E", "F"], [1, 2, 3]) # create a new iterator, aggregate each iterator element => [('A', 1), ('B', 2), ('C', 3)] iters = iter ([1, 2, 3, 4, 5]) # Return an iterator object elem = next (iters) # obtain the next element from the iterator; for the implementation principle, see the built-in function Article lis block code (http://www.bkjia.com/article/128890.htm) iters = reversed ([, 3]) # returns the reverse traversal Server => [3, 2, 1] lists = [1, 2, 4, 5] [slice (3)] # slice => [1, 2, 3] lists = [1, 2, 3, 4, 5] [slice (1, 3)] # [slice (3) = slice (None, 3, None)/slice (1, 3) = slice (1, 3, None) /slice (, 1) = slice (1, 3, 1) lists = sorted ([, 4]) # Sort => [1, 2, 3, 4, 5] lists = sorted (['A', 'B', ';', 't', 'D', '1'], key = lambda x: ord (x), reverse = True) # key: Comparison key function. Does reverse traverse strs = input ("Please input data :") # input data f = open ("temp.txt", "r +") # open the file. For details, see the operating system Article print ("string % d" % 123) # print character => string 123 print ("Word", "character", "string", sep = "-") # sep is separator => character-string print ("Word", "character", "string", sep = "-", end = "\ r \ n ") # end: end => character-string/r/n print ("Word", "character", "string", sep = "-", end = "\ r \ n", file = open ("temp.txt", "w +") # print to the file class Clazz: def _ init _ (self, name): self. name = name; @ classmethod # wrap the function into the class method def setName_cls (cls, name): pass @ staticmethod # package the function into the static method def setName_sta (name ): pass def getname (self): return self. name def setname (self, value): self. name = value def delname (self): del self. name # property (fget = None, fset = None, fdel = None, doc = None) # returns a property attribute # property is an attribute method, which can be implemented in two ways, for details, see the attribute method code block of the class article (http://www.bkjia.com/article/68235.htm) x = property (getname, setname, delname) if _ name _ = "_ main _": fun () # use c = Clazz ("Liu Yan") print (c. x) # => Liu Yan c. x = 'tang wei' print (c. getname () # => Tang Wei del c. x

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.