Python learning 20 - Common built-in functions

1. abs() takes absolute value

print(abs(-156))  # 156
print(abs(-15.2))  # 15.2

  2,all()   If all elements are true, the Boolean value true is returned. If the element is empty, it also returns true. Only one parameter can be entered, and two can not be placed. Generally, they are placed in the list, Yuanzu

Elements are True except for 0, empty, None and False, but even if only two '' are put here, something will output True

As long as there is a False element in the judged data, False is directly output

a = [0]
print(all(a))  # False
print(all('False'))  # True

3. If any() is put into the ancestor or all element bits in the list, False will be output

a = [0,'']
print(any(a))  # False

4. bin() converts decimal to binary

print(bin(10))  # 0b1010
print(bin(4))  # 0b100
print(bin(2))  # 0b10

5. bool() converts parameters to Boolean values, and outputs False if there are no parameters

print(bool(0))  # False
print(bool(1))  # True
print(bool())  # False

6. bytes() converts a string to byte form   Convenient network transmission

name = 'Xin Qiji'
print(bytes(name,encoding='utf-8'))  # b'\xe8\xbe\x9b\xe5\xbc\x83\xe7\x96\xbe'
print(bytes(name,encoding='utf-8').decode('utf-8'))  # Xin Qiji

Here encoding='utf-8 'is the defined encoding, the first line is the definition encoding with UTF-8, and the second line. decode is decoding with UTF-8

7. The return value of chr() is the ASCII character corresponding to the current integer

print(chr(47))  # /

8. dir print view list of used releases

print(dir(all))

9. Divide the number of parameters in front of divmod by the quotient of the following parameters to obtain the remainder

print(divmod(10,7))  # (1, 3)

Shang 1 Yu 3

10,eval()
1. Extract the data structure in the string
2. You can calculate the result of the mathematical operation formula inside the string

a = '1 + 3'
print(eval(a))  # 4

11. hash() hashes the parameters passed in

Data types that can be hashed are immutable data types
A non hashable data type is a variable data type

The output length of hash operation is the same
The result before hash operation cannot be known through reverse operation

a = '44aa5556545'
b = 'Flowers have a reopening day'
c = 'No one is young again'

print(hash(a))
print(hash(b))
print(hash(c))

The hash calculation results of each run are also different

12. The enumerate() function is used to combine a traversable data object (such as list, tuple or string) into an index sequence, and list data and data subscripts at the same time. It is generally used in the for loop. Define two variables at a time. The first is the index and the last is the data element

l = ('a','b','c','d','e','f','g','h','i','j','k','l')
for x,i in enumerate(l):
    print(x,i)
x = 0
l = ('a','b','c','d','e','f','g','h','i','j','k','l')
for i in l:
    x = x + 1
    print(x,i)

The output of the above two codes is the same

13. Help displays help information

print(help(all))

14. hex 10 to hex

print(hex(18))  # 0x12


15. oct 10 to octal

print(oct(18))  # 0o22

16. id() prints the memory address of a thing

a = 15
print(id(a))  # 2587034479344

17. isinstance() determines whether the former is something defined by the latter, and outputs a Boolean value

print(isinstance(123,int))  # True

18,globals()
Print out the current global variable

print(globals())

19,locals()
Print all local variables
If it is running directly in the global, the global variables will be displayed directly

20. zip() function the zip function arranges the elements in the incoming elements one by one, and discards them all individually

print(list(zip(('aa','bb','cc'),(3,4,5))))      #[('aa', 3), ('bb', 4), ('cc', 5)]
print(list(zip(('aa','bb','cc'),(3,4,5,6))))    #[('aa', 3), ('bb', 4), ('cc', 5)]
print(list(zip(('aa','bb','cc','dd'),(3,4,5)))) #[('aa', 3), ('bb', 4), ('cc', 5)]
ziDian = {'name':'adam', 'nianLing':25,'shenGao':'185cm' }
print(list(zip(ziDian.keys(),ziDian.values()))) #[('name', 'adam'), ('nianLing', 25), ('shenGao', '185cm')]
print(list(zip('1234567','abcdefg'))) 
#[('1', 'a'), ('2', 'b'), ('3', 'c'), ('4', 'd'), ('5', 'e'), ('6', 'f'), ('7', 'g')]

The zip function is mainly passed in   Sequence type on-line list primitive string

21, max and min

Take the maximum and minimum values
Need to pass in iteratable data
Each bit of the element is compared in turn. If the first bit has obtained the maximum or minimum value, the next bit is not compared
Different types of data cannot be compared, such as str and int
max and min can be followed by key, that is, you can define the value that needs cyclic comparison, and take the elements associated with this value as a whole

#Find the tallest of the three
ziDian1 = [
    {'name': 'Zhang San', 'shenGao': 136},
    {'name': 'Li Si', 'shenGao': 148},
    {'name': 'Wang Wu', 'shenGao': 176}
        ]


print(max(ziDian1,key =lambda a:a['shenGao']))  # {'name': 'Wang Wu', 'shenGao': 176}

22. ord() displays the value of the character in the asker code

print(ord('w'))  # 119

23. pow() is the power of the second parameter of the first parameter. If there is a third parameter, the remainder of the calculation result of the former is taken

print(pow(3,3)) #3**3
print(pow(3,3,2)) #3**3%2

24. Reversed

a = [1,2,3,4]
print(list(reversed(a)))  # [4, 3, 2, 1]

25. round

print(round(3.5))  # 4

26. set as dictionary

print(set('adam'))  # {'a', 'd', 'm'}

27. Slice defines slice width and step size

a = 'abcdefgh'
b = slice(3,6)
c = slice(3,6,2)
print(a[3:6])  # def
print(a[b])  # def
print(a[c])  # df

c here is to set the step size to 2, so the slice extraction is to extract from the third bit, that is, the letter d, and then empty a bit to f and then go in and extract

28. Sorted

a = [6,3,2,4,8,9]
print(sorted(a))
#Height ranking
ziDian1 = [
    {'name': 'Zhang San', 'shenGao': 136},
    {'name': 'Li Si', 'shenGao': 189},
    {'name': 'Wang Wu', 'shenGao': 176}
        ]
print(sorted(ziDian1,key =lambda a:a['shenGao']))

Followed by the parameter key, that is, the extraction sort is arranged according to the value corresponding to the shenGao key in the dictionary

Sequential arrangement

29. sum

l = [4,5,6]
print(sum(l))  # 15

30. View data type

a = 'qsx'
print(type(a),a)  # <class 'str'> qsx

31. When vars is inside the function, if no parameter is filled in, locals is the local variable
If the parameter is written, the method of using the function is listed and output in the form of a dictionary


32. The import module is other py files. You can write the file name of Py directly here without adding. Py
When called, write py file name directly followed by   . And the required function name
__ import__ () this py file name that can import string type is recommended because it is used at the bottom

Tags: Python Back-end

Posted on Mon, 08 Nov 2021 08:43:01 -0500 by wscreate