Built-in functions OOP +

Built-in functions

grasp

1. bytes()
2. chr()/ord()
3. divmod()
4. enumerate()
5. eval()
6. hash()
  1. bytes () character decoding, converting characters to binary
res = 'hello 世界'.encode('utf-8')
print(res)
b'hello \xe4\xb8\x96\xe7\x95\x8c'
res = bytes('hello 世界',encoding='utf-8')
print(res)
b'hello \xe4\xb8\x96\xe7\x95\x8c'
  1. chr () / word ()

    chr (): integer [0-256] is converted to the ASCII code corresponding to characters
    ord (): converting the character into a corresponding digital

print(chr(97))
print(ord('a'))
a
97
  1. divmod () columns
print(divmod(17,3))   # 返回(17//3,17%3)
(5, 2)
  1. enumerate()

    Returns an iterator object in the form of tuples and corresponding values ​​of the index

s = 'hello'
for i in enumerate(s):
    print(i)
(0, 'h')
(1, 'e')
(2, 'l')
(3, 'l')
(4, 'o')
  1. eval()

    Converting a string to other data types

s = '2'
lis = "[1,2,'3']"
print(eval(s),type(eval(s)))
print(eval(lis),type(eval(lis)))
2 <class 'int'>
[1, 2, '3'] <class 'list'>
  1. hash()

    It can determine whether hash, hash variable data type is not type, data type to be immutable hash type

print(hash(2))
2

To understanding

1. abs()
2. all()
3. any()
4. bin()/oct()/hex()
5. dir()
6. frozenset()
7. globals()/loacals()
8. pow()
9. round()
10. slice()
11. sum()
12. import()
  1. abs()

    Absolute value

print(abs(-12))
12
  1. all()

    Inner iterations may object elements are all true returns True, otherwise False

print(all([1,'2',3,0]))
print(all([]))
False
True
  1. any()

    Iterables there is an element of truth Returns True

print(any([0,[],None,{},'',False]))
False
  1. bin()/oct()/hex()

    bin (): converted into a binary number

    oct (): converted to octal

    hex (): converted to hexadecimal

print(bin(20))
print(oct(20))
print(hex(20))
0b10100
0o24
0x14
  1. to you()

    Modules include all the features

import time

print(dir(time))
['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname']
  1. frozenset()

    Immutable collection

s = frozenset({1,2,'a','c'})
print(s)
s[1] = 3    # 报错,不可改变
frozenset({1, 2, 'c', 'a'})



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-33-ef2c6686afe7> in <module>
      1 s = frozenset({1,2,'a','c'})
      2 print(s)
----> 3 s[1] = 3    # 报错,不可改变


TypeError: 'frozenset' object does not support item assignment
  1. Global () / local ()

    View global variables / Check local variables

def f1(num):
    x = 10
    print(locals())
    print(num)
    
num = 20
f1(num)   
# print(globals())
{'x': 10, 'num': 20}
20
  1. pow()

    pow (x, y [, z]) is returnedx**y%z

print(pow(2,10,3),2**10%3)
print(pow(2,5),2**5)
1 1
32 32
  1. round()

    rounding

print(round(3.4),round(3.5))
3 4
  1. slice()

    Achieve slice slice (start, stop [, step])

lis = [1,2,3,4,5,6,7]
s1 = slice(0,3)
s2 = slice(3,6,2)
print(lis[s1])
print(lis[s2])
[1, 2, 3]
[4, 6]
  1. sum()

    Summing

lis = [1,2,3,4,5,6,7]
print(sum(lis))
28
  1. __import__()

    By introducing the string module

time = __import__('time')
print(time.time())
1560430597.1571605

Object-Oriented Programming

Object-oriented programming is to program functions into one package, and then call the execution order, the return value of a function of the parameters as a function of the incoming

Like the same plant, a step-by-step process, the next process to be based on a process to do the process, a process if there is a problem, the next process will be affected

Advantages: simplify complex problems, process-oriented

Disadvantages: poor scalability

Guess you like

Origin www.cnblogs.com/Hades123/p/11019552.html