Simple use and introduction of Python built-in functions

Source: Internet
Author: User
Tags decimal to binary ord pow

"""
Simple use and introduction of built-in functions
Reference Link: https://docs.python.org/3/library/functions.html


"""
1.
ABS () # Absolute Value
"""
n = ABS ( -10) print (n) # 10



"""
2.
All () # is true, output ture, output flase
Any () # As long as there is true, output ture, then output flase

0,none, "", [], (), {} # False
"""
N1 = ALL ([1,2,3,[],none]) print (n1) # Falsen2 = Any ([1,0, "", []]) print (n2) # True

  


"""
3.
ASCII ()
__repr__ method for automating object execution
"""
Class Foo:    def __repr__ (self):        return "444" n = ASCII (Foo ()) print (n) # 444

  



"""
4.
Bin () # Convert decimal to binary 0b to indicate binary
Oct () # Convert decimal to octal 0o for octal
Hex () # Convert decimal to hexadecimal 0x for hexadecimal
"""
Print (Bin (5)) Print (Oct (9)) print (hex) # 0b101# 0o11# 0x1b

  



"""
5.
BOOL () Boolean value
0,none, "", [], (), {} = False
"""


"""
6.
Bytes ()
UTF8 encoding, 3 bytes of a kanji
GBK encoding, 2 bytes of a kanji
One byte = = 8 bits

STR ()
Bytes into a string
"""
# Converts a string to a byte type, with a representation of 16 in the system # Bytes (string, encoded format) s = "China" n3 = bytes (s,encoding= "Utf-8") print (n3) # B ' \xe4\xb8\xad\xe5\x9b\ Xbd ' N4 = bytes (s,encoding= "GBK") print (N4) # b ' \xd6\xd0\xb9\xfa ' N5 = str (b ' \xd6\xd0\xb9\xfa ', encoding= "GBK") print (N5) # China N6 = str (b ' \xe4\xb8\xad\xe5\x9b\xbd ', encoding= "Utf-8") print (N6) # China

  




"""
7.
Callable ()
Detects whether the passed value can be called
"""
Def f1 ():    passf1 () F2 = 123print (callable (F1)) # Trueprint (callable (F2)) # False

  



"""
8.
Chr ()
Ord ()
Output ASCII correspondence, Chr () output decimal position character, Ord () output character in ASCII table position
"""
Print (CHR) # Aprint (Ord ("A")) # 65# instance 1: Generate 6-digit random password, plain letter # import random# li = []# for I in range (6): #     temp = chr (rand Om.randrange (65,91) #     Li.append (temp) # print (LI) # result = "". Join (LI) # print (Result) # instance 2: Generate 8-digit random password with a number,  The letter Import Randomli = []for i in range (8):    r = Random.randrange (0,6)   # Let's generate the position of the number random    if r = = 2 or r = = 5:        num = Random.randrange (0,10)        li.append (num)    else:        temp = Random.randrange (65,91)        c = chr (temp)        Li.append (c) result = "". Join (LI) print (result) # 3e4jvhf8

  



"""
9.
Compile () # Compile, compile the string into Python code
eval () # executes an expression and gets the result
EXEC () # Execute Python code

Note: Eval has a return value, exec does not return a value
Python hello.py process:
1. read file contents open,str to memory
2.python, String----Compile special code
3. Execute code
"""
s = "Print (' hello,python~ ')" R = Compile (s, "<string>", "exec") exec (r) # hello,python~ss = "8*8+5" Print (eval (ss)) # 69

  


"""
10.
Dir ()
Help ()
Quick access to module, object-provided functionality
"""
Print (dir (tuple)) print (Help (tuple))

  




"""
11.
Divmod ()
Get quotient and remainder
"""
N1,N2 = Divmod (96,10) print (N1,N2) # 9 6



"""
12.
Isinstance ()
Used to determine whether an object is an instance of a class
"""
S1 = "Hello" r = isinstance (s1,str) print (r) # Truer1 = isinstance (s1,list) print (R1) # Flase

  


"""
13.
The filter () # function returns a value of ture, adding the element to the result
# filter Loop second parameter, let each loop element execute function, if function return value is ture, the function is valid
Map () # Adds a function return value to the result
# (Functions, objects that can be iterated (for loops))
"""
# example 1def F1 (args): result = [] for item in ARGS:IF item > 22:result.append (item) return RE        Sultli = [11,22,33,44,55,66,77,88]ret = f1 (LI) print (ret) # [33, 44, 55, 66, 77, 88]# optimization Example 1def F2 (a): If a > 22:  return TRUELS1 = [11,22,33,44,55,66,77]res1 = Filter (F2,LS1) print (list (res1)) # [33, 44, 55, 66, 77]# knowledge extension, lambda function res2 = Filter (lambda x:x > 22,ls1) print (RES2) # Returns a filter object# <filter object at 0x000000e275771748>print (list ( RES2) # [33, 44, 55, 66, 77]# example 2def F1 (args): result = [] for i in args:result.append (i +) return re    Sultlst1 = [11,22,33,44,55,66]rest = f1 (lst1) print (list (rest) # [111, 122, 133, 144, 155, 166]# optimization example 2,,,map function def f2 (a): Return a + 100lst2 = [11,22,33,44,55,66]RESULT1 = map (f2,lst2) Print (list (RESULT1)) # [111, 122, 133, 144, 155, 166]# optimization shown Example 2,map function +lambda function lst3 = [11,22,33,44,55,66]RESULT2 = map (lambda a:a+100,lst3) print (list (RESULT2)) # [111, 122, 133, 144, 155, 166]
 


"" "
.
Globals () # All global Variables
Locals () # All local variables
""
name = "Python" def Show (): a = 123 b = 456 print (locals ()) Print (Globals ()) show () # {' A ': 123, ' B ': 456}# {' r Esult ': ' 3vz8b1v0 ', ' res1 ': <filter object at 0x000000ecfe24b5c0>, ' lst3 ': [One, one, one, one, one, one, one], ' __package__ ': None, ' F2 ': <function F2 at 0x000000ecffcb9268>, ' ss ': ' 8*8+5 ', ' N2 ': 6, ' n3 ': B ' \xe4\xb8\xad\xe5\x9b\xbd ', ' temp ':  The ' s1 ': ' Hello ', ' n ': ' 444 ', ' name ': ' Python ', ' I ': 7, ' rest ': [111, 122, 133, 144, 155, 166], ' __spec__ ': None, ' lst1 ': [One, one, one, one, one, one, and a, ' R1 ': False, ' num ': 0, ' __builtins__ ': <module ' builtins ' (built-in), ' N6 ': ' China ', ' Rando M ': <module ' random ' from ' c:\\users\\xieshengsen\\appdata\\local\\programs\\python\\python35\\lib\\random.py ' , ' s ': ' Print (' hello,python~ ') ', ' __file__ ': ' d:/pycharmprojects/Advanced Automation/Module learning/built-in function _v1.py ', ' F1 ': <function F1 at 0x000000ecffcbf510>, ' result2 ': <map object at 0x000000ecfe25ec88>, ' Show ': <function show at 0x000000ecffcbf2f0>, ' ret ': [33, 44, 55, 66, 77, 88], ' __cached__ ': None, ' li ': [One, one, one, one, one, one, one, one], ' __doc__ ': ' \ n simple use and introduction of built-in functions \ n Reference Links: HTTPS://DOCS.PYTHON.ORG/3 /library/functions.html\n ', ' C ': ' V ', ' result1 ': <map object at 0x000000ecfe260d30>, ' Foo ': <class ' __main__. Foo ';, ' N1 ': 9, ' N5 ': ' China ', ' r ': True, ' __loader__ ': <_frozen_importlib_external. Sourcefileloader object at 0x000000ecfdfea898>, ' LS1 ': [One, one, one, one, one, one, 22], ' lst2 ': [11, 33, 44, 55, 66], ' Res2 ': <filter object at 0x000000ecfe24b4a8>, ' N4 ': B ' \xd6\xd0\xb9\xfa ', ' __name__ ': ' __main__ '}


"""
15.
Hash ()
Generate a hash value (string)
"""
has = "python" Print (a hash (has)) # 839578881833832098

  



"""
16.
Len ()
The length of the output object
"""
Print (len ("Python")) # 6print (Len ("Great Wall")) # Python3,  python2 output length is 6 (python3 by character, Python2 by Byte) # 2

  



"""
17.
Max () # max value
Min () # Minimum value
Sun () # summation
"""
lit = [11,22,33,44,55]print (max (lit)) print (min (lit)) print (sum (lit)) # 55# 11# 165

  



"""
18.
POW ()
Seeking Index
"""
Print (POW (2,10)) # 1024

  



"""
19.
Reverse ()
Reverse
"""
Lit1 = [11,22,33,44,55,66]print (list (Reversed (LIT1)) # [66, 55, 44, 33, 22, 11]

  



"""
20.
Round ()
Rounding evaluation
"""
Print (round (1.4)) Print (round (1.8)) # 2

  



"""
21st.
Sorted ()
Sort that is equivalent to a list
"""
Lit2 = [12,32,1,3,4,34,11,5]print (list (sorted (lit2))) # [1, 3, 4, 5, one, a, 1, 34]lit2.sort () print (LIT2) # [3, 4, 5,, 11, 12, 32, 34]



"""
22.
Zip ()
"""
L1 = ["Hello", 11,22,33]l2 = ["World", 44,55,66]l3 = ["Python", 77,88,99]l4 = Zip (l1,l2,l3) # Print (List (L4)) # # [(' Hello ', '  World ', ' Python '), (one, one, one, one, one, one), (+, (),]TEMP1 = List (l4) [0]print (temp1[0]) Ret1 = "". Join (TEMP1) print (ret1) # Hello World python

  

Simple use and introduction of Python built-in functions

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.