python built-in methods

# Note: The built-in function id() can return the identity of an object as an integer. This integer usually corresponds to the location of the object in memory, but this is related to the specific implementation of python and should not be used as a definition of identity, that is, it is not accurate enough, and the most accurate one is based on the memory address.
# The is operator is used to compare the identity of two objects, the equal sign compares the values ​​of two objects, and the built-in function type() returns the type of an object

The following first lists the built-in functions that need to be mastered
print(abs(-1)) # 1 takes the absolute value

print(all([2, 3, 4, 'sss', 'True'])) # If the value inside is all True, return True, and return True if the value inside is empty
print(any('3' )) #As long as one of the values ​​inside is True, choose to return True, if the value inside is empty, return False

print(bytes('Hello, albert', encoding='utf-8')) # Convert the utf-8 encoded string to binary
print(callable('d'.strip)) # Determine whether the value inside is Iterable types can be followed by (), such as 'abc'.strip(), max(), etc.


print(bin(11)) #decimal to binary
print(oct(11)) #decimal to octal
print(hex(11)) #decimal to hexadecimal

print(bool(0)) #0, None, an empty boolean is false

res='Hello egon'.encode('utf-8') # unicode is encoded according to utf-8, and the result is bytes type
res=bytes('Hello egon',encoding='utf-8') # Same as above
print(res)

def func():
pass
print(callable('aaaa'.strip)) #Determine whether an object is callable, callable means that parentheses can be added to execute a function

print(chr(90)) #Convert decimal numbers into characters according to the ascii code table
print(ord('Z')) #Convert characters into decimal numbers according to the ascii code table


print(dir('abc')) # See which methods can be called by dots under an object to
output:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
print(divmod(1311,25)) # print a tuple containing the quotient and remainder (52,11)

eval focuses on introduction, can be used for file read and write operations
# Take out the expression in the character and run it, and get the execution result of the expression
res=eval('{"name":"egon","age":18 }')
print(res,type(res))
output: {'name': 'egon', 'age': 18} <class 'dict'>
# application of eval in file
with open('db.txt' ,'r',encoding='utf-8') as f:
s=f.read()
dic=eval(s)
print(dic,type(dic))
print(dic['egon'])
# eval can Let’s take out the string expression obtained by opening the default file and run it, and get the execution result of the expression, convert it into the original data type, and then make it easier to read and write operations.


fset=frozenset({1,2,3}) #Set is a mutable type, frozenset can create immutable sets, fset has no .add method.


print (len ({'x': 1, 'y': 2})) # {'x': 1, 'y': 2} .__ len __ ()

obj=iter('egon') #'egon'.__iter__()
print(next(obj)) #obj.__next__()
print(next(obj))#obj.__next__()
print(iter(obj))#obj.__next__()

Output result
e
g
<str_iterator object at 0x00000000021E7160>


# pow three parameters,
print(pow(2,3,3)) #2 ** 3 % 3 take the remainder and output 2


# reversed order is reversed, no size sorting
l=[ 1,4,3,5]
res=reversed(l)
print(list(res))
print(l)


# write a float type in round, and output after rounding
print(round(3.5)) # 4
print(round(3.4) ) # 3


# slice Take a slice
sc=slice(1,5,2) #1:5:2
l=['a','b','c','d','e','f' ]
# print(l[1:5:2])
print(l[sc]) # sc = slice(1,5,2) = [1:5:2]

t=(1,2,3,4,5,6,7,8)
# print(t[1:5:2])
print(t[sc])


# sum summation, only for summation, from functools import reduce 'reduce' has many functions, one of which can also be used for summation
sum
print(sum([1,2,3,4]))

vars 
vars ()

# zip zipper function, let the upper and lower data intersect one-to-one
left='hello'
right={'x':1,'y':2,'z':3}

res=zip(left,right)
print(list(res)) # [('h', 'x'), ('e', 'y'), ('l', 'z')]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324727089&siteId=291194637