1. Mathematical operation function
- Introduction to built-in functions:
Built in functions, which are built-in functions after Python installation
All built-in function usage websites officially provided by Python:
https://docs.python.org/zh-cn/3/library/functions.html
- Common mathematical operation functions:
1.abs() - find the absolute value
Usage:
abs(numerical value)
Example:
print(abs(-25)) # The result is print 25 print(abs(35)) # The result is print 35 print(abs(-2.56)) # The result is 2.56
2.round() - approximate value of floating point number
Usage:
round(numerical value,numerical value) # The following values mean to retain several decimal places and can be omitted
Example:
print(round(2.57)) # The result is print 3 print(round(2.57,1)) # The result is print 2.6
3.pow() - find the index
Usage:
pow(x,y,z) # x. Both y and z represent values # The return value of this function is the Y power of x, which is equivalent to x**y # If z exists, take the remainder of the calculated value, which is equivalent to (x**y)%z
Example:
print(pow(2,4)) # The result is print 16 print(pow(2,4,3)) print((2**4)%3) # The results are all printed 1
4.divmod() - find quotient and remainder
Usage:
divmod(x,y) # Returns a tuple containing quotient and remainder (x//y,x%y)
Example:
print(divmod(8,2)) # The result is print (4, 0) print(divmod(8,3)) # The result is printed (2, 2)
5.max() - find the maximum value
Usage:
max(x,y...)
Example:
print(max(1,500,100)) # The result is print 500
min() - find the minimum value
Same as max()
6.sum() - sum
Usage:
sum(iterable,start) # iterable represents iteratable objects (list, tuple, set, etc.), and start adds and sums the calculated values of iterable again
Example:
print(sum([1,2,3])) # The result is print 6 print(sum([1,2,3],2)) # The result is print 8
7.eval() - execute expression
Usage:
eval(expression[, globals[, locals]]) # Expression -- expression # globals -- variable scope and global namespace. If provided, it must be a dictionary object # locals -- variable scope and local namespace. If provided, it can be any mapping object
Example:
print(eval('1+2')) # The result is print 3 a=5 b=3 print(eval('a+b')) # The result is print 8 print(eval('a+b',{'a':2,'b':3})) # The result is print 5
2. Type conversion function
- Common type conversion function
1. (Num) numeric type conversion
- int() - convert to integer
Usage:
int(x,base=Hexadecimal number) # x stands for string or number, and base is an omitted parameter
Example:
print(int(2.5)) # The result is print 2 print(int('a',base=16)) # The result is 10, because the decimal value of a in hexadecimal is 10 print(int('0101',base=2)) # The result is print 5, because the decimal value of 0101 in binary is 5
- float() - convert to floating point number
Usage:
float(x) # x stands for string or integer
Example:
print(float(2)) # The result is print 2.0
- str() - convert to string
Usage:
str(object='') # Object represents an object
Example:
print(type(str(1))) # The result is print < class' STR '>
2.ASCII conversion
- ord() - character to number (ASCII code)
Usage:
ord('character') # Returns the ACSII code value corresponding to the character
Example:
print(ord('a')) # The result is print 97
- chr() - numeric to character (ASCII code)
Usage:
chr(number) # Returns the character corresponding to the ASCII code value
Example:
print(chr(66)) # The result is print B
3.bool() - convert to Boolean
Usage:
bool(x) # x is the object to convert
Example:
print(bool(0)) # The result is print False print(bool(1)) # The result is print True
4. Binary conversion
- bin() - decimal to binary
Usage:
bin(x) # x is an integer
Example:
print(bin(20)) # The result is 0b10100 a=20 print(bin(a)) # The result is 0b10100
- hex() - decimal to hexadecimal
Usage:
hex(x) # x is a decimal integer
Example:
print(hex(15)) # The result is 0xf
- oct() - decimal to octal
Usage:
oct(x) # x is an integer
Example:
print(oct(10)) # The result is printed 0o12
5. Type conversion
- list() - Convert tuples to lists
Usage:
list(Tuple) # Tuple is the tuple to be converted to a list
Example:
print(list((1,2,3))) # The result is printed [1, 2, 3]
- tuple() - Convert list to tuple
tuple(sequence) # Sequence is the sequence to be converted
Example:
print(tuple([1,2,3])) # The result is printed (1, 2, 3) print(tuple({'a':1,"b":2})) # The result is print ('a ',' B ')
- dict() - create dictionary
Usage:
dict(**kwarg) dict(mapping, **kwarg) dict(iterable, **kwarg) # **kwargs -- Keyword # mapping -- container for elements # Iteratable -- iteratable object
Example:
print(dict(a=15,b='xiaoli',c=[99,97,100])) # The result is print {'a': 15, 'B': Xiaoli ',' C ': [99, 97, 100]}
- bytes() - convert to byte array
Returns a new (byte array) bytes object, which is an immutable sequence containing integers in the range of 0 < = x < 256
Usage:
bytes([source[, encoding[, errors]]]) # If source is an integer, an initialization array with a length of source is returned; # If source is a string, convert the string into a byte sequence according to the specified encoding; # If source is an iteratable type, the element must be an integer in [0, 255]; # If source is an object consistent with the buffer interface, this object can also be used to initialize bytes # If no parameters are entered, the default is to initialize the array to 0 elements
Example:
print(bytes('good',encoding='utf-8')) # The result is b'\xe5\xa5\xbd' print(bytes('good'.encode('utf-8'))) # The result is b'\xe5\xa5\xbd' # Both printing results are consistent and can be used print(bytes('good'.encode('gbk'))) # The result is print b'\xba\xc3'
3. Sequence operation function
- Sequence operation function
1. Judgment
- all() - correct judgment
Usage:
all(iterable) # iterable stands for tuples or lists # The function is used to judge whether all elements in a given iteratable parameter iterable are True. If so, it returns True; otherwise, it returns False # All elements are True except 0, empty and False
Example:
print(all([1,2,3])) # The result is print True print(all((1,2,0,23))) # The result is print False
- any() - wrong judgment
Usage:
any(iterable) # iterable stands for tuples or lists # The function is used to judge whether the given iteratable parameters iterable are all False, and returns False. If one of them is True, # Returns True The opposite of the all() function
Example:
print(any((0,False,1))) # The result is True
2. Sorting
- sorted() - the function sorts all iteratable objects
Usage:
sorted(iterable, *, key=None, reverse=False) # iterable represents an iteratable object # key represents the element mainly used for comparison. There is only one parameter. The parameters of the specific function are taken from the iteratable object, # Specifies an element in the iteratable object to sort # Reverse stands for collation, reverse = True descending, reverse = False ascending (default ascending)
Example:
print(sorted([12,231,31],key=int,reverse=True)) # The result is printed [231, 31, 12]
Difference between sort and sorted:
Sort is a method applied to the list. sorted can sort all iteratable objects,
The sort method of list returns the operation on the existing list, while the built-in function sorted method returns a new list instead of the operation on the original basis
- reverse() - used to reverse the elements in the list
Usage:
list.reverse() # Note: this function has no parameters and no return value, but it will sort the list elements in reverse
Example:
a=[1,20,17] a.reverse() print(a) # The result is printed [17, 20, 1]
- range() - create a list of integers
Usage:
range(start, stop[, step]) # Start stands for the start of counting. It starts from 0 by default # stop stands for the end of counting #Step stands for step size, which is 1 by default
Example:
for Test in range(0,5): print(Test,end=' ') # The result is print 0 1 2 3 4 # end = '' means no line breaks, separated by '
3. Packing
- zip() - package the elements in the object into tuples
Usage:
zip([iterable, ...]) # iterable represents one or more iterators # Return tuple list
Example:
a=zip(['Age','Name'],[15,'xiaoli']) print(a) # The result is print < zip object at 0x000002272ab0f2c0 > print(list(a)) # View packaged files # The result is printed [('Age', 15), ('Name', 'xiaoli')]
4. Traversal
- enumerate() - combine a traversable data object into an index sequence
Usage:
enumerate(sequence, [start=0]) # Sequence represents a sequence or other object that supports iteration # start represents the starting position of the subscript # The function is used to combine a traversable data object into an index sequence, # Lists data and data subscripts at the same time, which is generally used in the for loop
Example:
for Test in enumerate(['Name','Age']): print(Test,end=' ') # The result is print (0, 'Name') (1, 'Age')
4. Set operation function
- Set set
Set (set) is a data type in Python. It is an unordered and non repeated set of elements. If repetition occurs, it will be automatically overwritten
Example:
a={1,2,3,1} print(a) # The result is print {1, 2, 3}
- Set operation function
1. Add, empty
- add() - add element
Usage:
aggregate.add(element) # Note: only one element can be added at a time
Example:
a={1,2,3} a.add(4) print(a) # The result is print {1, 2, 3, 4}
- clear() - clear elements
Usage:
aggregate.clear() # Empty all
Example:
a={1,2,3} a.clear() print(a) # The result is print set()
2. Collection operation
- difference() - find the difference set
Usage:
a.difference(b) # Both a and b represent sets, and the difference set is the element that exists in a and does not exist in b
Example:
a={1,21,15} b={1,20,11} print(a.difference(b)) # The result is printed {21, 15}
- intersection() - find intersection
Usage:
a.intersection(b) # Both a and b represent sets. Find the intersection, that is, the elements that exist in a and b
Example:
a={12,13,14} b={12,13,10} print(a.intersection(b)) # The result is print {12, 13}
- union() - Union
Usage:
a.union(b) # Both a and b represent sets, union sets, that is, elements existing in a and elements existing in b
Example:
a={5,6,7} b={12,4,2} print(a.union(b)) # The result is printed {2, 4, 5, 6, 7, 12}
3. Remove, update
- pop() - randomly remove an element
Usage:
aggregate.pop() # Randomly remove an element and get that element without parameters
Example:
a={1,25,13} print(a.pop()) # The result is print 25 (may be different)
- discard - removes the specified element
Usage:
a={12,13,14} a.discard(13) print(a) # The result is printed {12, 14}
- update() - update the collection
Usage:
a={1,12,8} b={2,9,5} a.update(b) print(a) # The result is printed {1, 2, 5, 8, 9, 12}