table of Contents
Built-in functions
grasp
1. bytes()
2. chr()/ord()
3. divmod()
4. enumerate()
5. eval()
6. hash()
- 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'
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
- divmod () columns
print(divmod(17,3)) # 返回(17//3,17%3)
(5, 2)
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')
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'>
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()
abs()
Absolute value
print(abs(-12))
12
all()
Inner iterations may object elements are all true returns True, otherwise False
print(all([1,'2',3,0]))
print(all([]))
False
True
any()
Iterables there is an element of truth Returns True
print(any([0,[],None,{},'',False]))
False
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
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']
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
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
pow()
pow (x, y [, z]) is returned
x**y%z
print(pow(2,10,3),2**10%3)
print(pow(2,5),2**5)
1 1
32 32
round()
rounding
print(round(3.4),round(3.5))
3 4
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]
sum()
Summing
lis = [1,2,3,4,5,6,7]
print(sum(lis))
28
__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