Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the built-in functions in Python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what built-in functions Python has". The explanation in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn what built-in functions Python has.

1. It's related to numbers.

(1) data type

Bool: Boolean (True,False)

Int: integer (integer)

Float: floating point (decimal)

Complex: plural

(2) binary conversion

Bin () converts the given parameter to binary

Otc () converts the given parameter to octal

Hex () converts the given parameter to hexadecimal

Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print (bin (10)) # binary: 0b1010 ss= "> print (hex (10)) # hexadecimal: 0xa ss=" alt "> print (oct (10)) # Octal: 0o12

(3) Mathematical operation

Abs () returns an absolute value

Divmode () returns quotient and remainder

Round () is rounded

Pow (a, b) finds the b power of a, if there are three parameters. Then take the remainder of the third number after finding the power.

Sum () summation

Min () to find the minimum

Max () to find the maximum

Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print (abs (- 2)) # absolute value: 2 ss= "" > print (divmod (20pen3)) # Quotient and remainder: (6) ss= "alt" > print (round (4.50)) # rounded off: 4 ss= "> print (round (4.51)) # 5 ss=" alt "> print (pow (10Jing 2)) # if the third parameter is given. 1 ss= "> print (sum)) # summation: 55 ss=" alt "> print (5PM3 alt > min) # find the minimum: 2 ss=" > print (7 beers 3 minus 15 pencils 9 pencils 13) # find the maximum: 15

two。 Related to data structure

(1) sequence

a. Lists and tuples

List () converts an iterable object into a list

Tuple () converts an iterable object into a tuple

Hongmeng's official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print (1) Mei2, 3, 4, 5) # [1, 2, 3, 5, 5, 6] ss= "> tuple ([1, 2, 3, 4, 5, 5)) # (1)

b. Correlation built-in function

Reversed () flips a sequence and returns the iterator of the flipped sequence.

Slicing of slice () list

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > lst = ss= "attribute-value" > "Hello" ss= "> ss=" attribute "> it = ss=" attribute-value "> reversed (lst) # will not change the original list. Returns an iterator with a design rule ss= "alt" > print (list (it)) # ['Ah', 'good', 'you'] ss= "> ss=" attribute "> lst = [1,2print 3,4,5,6,7] ss=" alt "> print (LST [1: 3:1]) # [2print 3] ss=" > ss= "attribute" > s = ss= "attribute-value" > slice (1,3) 1) # ss= "alt" > print (LST [s]) # [2pr 3]

c. String

Str () converts data to a string

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print (str (123) + '456') # 123456 ss= "" > format () is related to specific data, used to calculate all kinds of decimals, actuaries, etc.

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > s = ss= "attribute-value" > "hello world!" Ss= "" > print (format (s, "^ 20")) # ss= "alt" > print (format (s, "ss=" tag "> 20")) # left align ss= "> print (format (s," ss= "tag" > > 20 ")) # right align ss=" alt "> # hello world! Ss= "> # hello world! Ss= "alt" > # hello world! Ss= "" > print (format (3,'b')) # binary: 11 ss= "alt" > print (format (97,'c')) # convert to unicode characters: a ss= "" > print (format (11,'d')) # binary: 11 ss= "alt" > print (format (11,'o')) # Octal: 13 ss= "> print (format (11) ) # hexadecimal (alphabetical): B ss= "alt" > print (format (11,'X')) # hexadecimal (uppercase): B ss= "" > print (format (11,'n')) # and d samples: 11 ss= "alt" > print (format (11)) # and d samples: 11 ss= "> ss=" alt "> print (format (123456789) 'e')) # Scientific counting. 6 decimal places are retained by default: 1.234568e+08 ss= "> print (format (123456789, '0.2e')) # Scientific counting. Keep 2 decimal places (lowercase): 1.23e+08 ss= "alt" > print (format (123456789, '0.2e')) # Scientific counting. Keep 2 decimal places (uppercase): 1.23E+08 ss= "" > print (format (1.23456789,'f')) # decimal point counting. Keep 6 decimal places: 1.234568 ss= "alt" > print (format (1.23456789, '0.2f')) # decimal point counting. Keep 2 decimal places: 1.23 ss= "> print (format (1.23456789, '0.10f')) # decimal point counting. Keep 10 decimal places: 1.2345678900 ss= "alt" > print (format (1.23456789e+3,'F')) # decimal point counting. Output INF:1234.567890 when it is very large

Bytes () converts a string to a bytes type

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > bs = ss= "attribute-value" > bytes ("have you eaten today?" Ss= "attribute" > encoding=ss= "attribute-value" > "utf-8") ss= "> print (bs) # b'\ xe4\ xbb\ X8a\ xe5\ xa4\ xa9\ xe5\ x83\ xe9\ xa5\ xad\ xe4\ xba\ x86\ xe5\ x90\ x97' ss=" alt "> bytearray () returns a new byte array. The element of this number is variable, and the value range of each element is [0256) ss= "> ss=" alt "> ss=" attribute "> ret = ss=" attribute-value "> bytearray (" alex "). Ss= "attribute" > encoding = ss= "attribute-value" > 'utf-8') ss= "" > print (ret [0]) # 97 ss= "alt" > print (ret) # bytearray (bounded exe) ss= "" > ret [0] = 65 # assign position An of 65 to ret [0] ss= "alt" > print (str (ret)) # bytearray (bounded Alex')

Ord () input characters to find the position with character encoding

Chr () input the position number to find the corresponding character

Ascii () is the ascii code that returns the value or u

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print (ord ('a')) # letter a code point in the coding table: 97 ss= "> print (ord ('middle')) # 'position in the coding table: 20013 ss=" alt "> ss=" > print (chr (65)) # known code point What are the characters: a ss= "alt" > print (chr (19999)) # characters ss= "> ss=" alt "> for i in range (65536): # print the characters ss=" > print (chr (I), ss= "attribute" > end=ss= "attribute-value" > ") ss=" alt "> ss=" > print (ascii ("@")) #'@'

Repr () returns the string form of an object

Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > s = ss= "attribute-value" > "Today\ nate% s meal\ t meal"% 3 ss= "> print (s) # Today # ate 3 meals ss=" alt "> print (repr (s)) # output as is Filter out escape characters\ n\ t\ r regardless of% ss= "> # 'Today\ nhad 3\ t meals'

(2) data set

Dictionary: dict creates a dictionary

Collections: set creates a collection

Frozenset () creates a frozen collection that cannot be added or deleted.

(3) related built-in functions

Len () returns the number of elements in an object

Sorted () sorts iterable objects (lamda)

a. Syntax: sorted (Iterable, key= function (collation), reverse=False)

Iterable: iterable object

Key: collation (collation function). Within sorted, each element in the iterable object is passed to the parameter of this function. Sort according to the results of the function operation

Reverse: whether it is a flashback. True: flashback, False: positive order

Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > lst = [5pje 7pint 6pint 12jingle 12jingle 12jingle 12pint 9pint 18pint 5] ss= "> lst.sort () # sort is a method in list ss=" alt "> print (lst) # [1,5,5,6,7,9,12,13,18] ss=" > ss= "alt" > ss= "attribute" > ll = ss= "attribute-value" > sorted (lst) # built-in function. The new list is sorted ss= "> print (ll) # [1, 5, 5, 6, 7, 9, 12, 13, 18] ss=" alt "> ss=" > ss= "attribute" > L2 = ss= "attribute-value" > sorted (lst,ss= "attribute" > reverse=ss= "attribute-value" > True) # reverse ss= "alt" > print (L2) # [18, 13, 12, 9, 7, 6, 5, 5 1] Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > # sort the list ss= "" > ss= "attribute" > lst = ['one',' two', 'three',' four', 'five',' six'] ss= "alt" > def f (s): ss= "> return len (s) ss=" alt "> ss=" attribute "> L1 = ss=" attribute-value "> sorted (lst) Ss= "attribute" > key=ss= "attribute-value" > f,) ss= "> print (L1) # ['one',' two', 'six',' four', 'five',' three']

Enumerate () gets the enumerated object of the collection

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > lst = ['one','two','three','four','five'] ss= "> for index, el in enumerate (lst,1): # get the index and elements together. The index starts from 0 by default. You can change ss= "alt" > print (index) ss= "" > print (el) ss= "alt" > # 1 ss= "" > # one ss= "alt" > # 2 ss= "> # two ss=" alt "> # 3 ss=" > # three ss= "alt" > # 4 ss= "> # four ss=" alt "> # 5 ss=" > # five

The all () iterable object is all True, and the result is True.

One of the any () iterable objects is True, and the result is True.

Hongmeng's official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print (all ([1 magnificent hellographies)) # True ss= "> print ([0meme 0meme 1 Falsedale]) # True

The zip () function is used to take iterable objects as parameters, package the corresponding elements in the object into a tuple, and then return a list of these tuples. If the number of elements of each iterator is not the same, the length of the return list is the same as the shortest object.

Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > lst1 = [1,2,3,4,5,6] ss= "> ss=" attribute "> lst2 = ['drunken folk songs', 'donkeys get water', 'spring of cattle herding class', 'beautiful life', 'defender', 'life of abandoned pine nuts'] ss=" alt "> ss=" attribute "> lst3 = ['USA', 'China' 'France', 'Italy', 'Korea', 'Japan'] ss= "> print (zip (lst1, lst1, lst3)) # ss=" tag "> zip object at 0x00000256CA6C7A88ss=" tag "> ss=" alt "> for el in zip (lst1, lst2, lst3): ss=" > print (el) ss= "alt" > # (1, 'drunken Folk', 'USA') ss= "> # (2, 'Donkey gets water' 'China') ss= "alt" > # (3, 'Spring of cattle herding Class', 'France') ss= "> # (4, 'Beautiful Life', 'Italy') ss=" alt "> # (5, 'defender', 'Korea') ss=" > # (6, 'Life of abandoned Pine Nut', 'Japan')

Fiter () filtering (lamda)

b. Syntax: fiter (function. Iterable)

Function: the function used to filter. Elements in iterable are automatically passed to function in iterable lter. Then judge whether to retain this data according to the True or False returned by function. Iterable: iterable object.

Hongmeng's official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > def func (I): # judge odd ss= "" > return I% ss= "attribute" > 2 = = 1 ss= "alt" > ss= "attribute" > lst = [1pd2 alt > ss= "attribute" > lst =] ss= "> ss=" attribute "> L1 = ss=" attribute-value "> filter (func Lst) # L1 is the iterator ss= "alt" > print (L1) # ss= "tag" > filter object at 0x000001CE3CA98AC8ss= "tag" > > ss= "> print (list (L1)) # [1,3,5,7,9]

Map () maps the specified sequence column (lamda) according to the function provided.

c. Syntax: map (function, iterable)

Each element in an iterable object can be mapped. To execute function separately

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > def f (I): return I ss= "> ss=" attribute "> lst = [1 ss=" alt "> ss=" attribute "> it = ss=" attribute-value "> map (f, lst) # pass each element of the iterable object to the previous function for processing. The result of the processing will be returned as an iterator print (list (it)) # [1, 2, 3, 4, 5, 6, 7]

3. Related to scope

Locals () returns the name in the current scope

Globals () returns the name in the global scope

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > def func (): ss= "" > ss= "attribute" > a = ss= "attribute-value" > 10 ss= "alt" > print (locals ()) # content in current scope ss= "> print (globals ()) # content in global scope ss=" alt "> print (" a lot of content today ") ss=" > func () Ss= "alt" > # {'name__':: 10} ss= "" > # {' _ _ name__':'_ _ main__' '_ doc__': None,' _ package__': None,'_ loader__': ss= "alt" > # ss= "tag" > _ frozen_importlib_external.SourceFileLoader object at 0x0000026F8D566080ss= "tag" > >, ss= "> #'_ spec__': None,'_ annotations__': {},'_ builtins__': ss=" tag > module 'builtins' ss= "alt" > # (built-in) ss= "tag" > > '_ _ file__':' D:/pycharm/ exercise / week03/new14.py','_ cached__': None, ss= "> # 'func': ss=" tag "> function func at 0x0000026F8D6B97B8ss=" tag "> >} ss=" alt "> # there are a lot of content today

4. Related to iterator / generator

Range () generates data

The next () iterator executes down once, which internally actually causes the _ _ next__ () method to return the iterator's next item.

Iter () gets the iterator, and the _ _ iter__ () iteration method is actually used internally to get the iterator

Hongmeng's official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > for i in range: ss= "" > print (I) ss= "alt" > # 15 ss= "> # 10 ss=" alt "> # 5 ss=" > # 0 ss= "alt" > ss= "attribute" > lst = [1 dint 2 dome 3 ss= 4 5] ss= "> ss=" attribute "> it = ss=" attribute-value "> iter (lst) # _ iter__ () get iterator ss=" alt "> print (it.__next__ ()) # 1 ss="> print (next (it)) # 2 _ next__ () ss=" alt "> print (next (it)) # 3 ss=" > print (next (it)) # 4

5. Execution of string type code

Eval () executes code of type string. And return the final result

Exec () executes code of type string

Compile () encodes the code of type string. Code objects can be executed through exec statements or evaluated by eval ()

Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > S1 = ss= "attribute-value" > input ("Please enter aquib:") # input: 8: 9 ss= "> print (eval (S1)) # 17 can dynamically execute the code. The code must return a value of ss= "alt" > ss= "attribute" > S2 = ss= "attribute-value" > "for i in range (5): print (I)" ss= "> ss=" attribute "> a = ss=" attribute-value "> exec (S2) # exec execution code does not return anything ss=" alt "> ss=" > # 0 ss= "alt" > # 1 ss= "> # 2 ss=" alt "> # 3 ss=" > # 4 ss= "alt" > print (a) # None Ss= "" > ss= "alt" > # dynamic execution Code ss= "" > exec ("" ss= "alt" > def func (): ss= "> print (" I am Jay Chou ") ss=" alt ">") ss=" > func () # I was co-founded by Jay Chou's official strategic cooperation-- HarmonyOS Technology Community ss= "dp-xml" > ss= "alt" > ss= "attribute" > code1 = ss= "attribute-value" > "for i in range (3). ): print (I) "ss=" > ss= "attribute" > com = ss= "attribute-value" > compile (code1 Ss= "attribute" > mode=ss= "attribute-value" > "exec") # compile will not execute your code. Just compile ss= "alt" > exec (com) # and execute the compiled result ss= "" > # 0 ss= "alt" > # 1 ss= "" > # 2 ss= "alt" > ss= "> ss=" attribute "> code2 = ss=" attribute-value ">" 5 minutes 7 "ss=" alt "> ss=" attribute "> com2 = ss=" attribute-value "> compile (code2,") Ss= "attribute" > mode=ss= "attribute-value" > "eval") ss= "> print (eval (com2)) # 18 ss=" alt "> ss=" > ss= "attribute" > code3 = ss= "attribute-value" > "name = input ('Please enter your name:')" # enter: hello ss= "alt" > ss= "attribute" > com3 = ss= "attribute-value" > compile (code3, "", ss= "attribute" > mode=ss= "attribute-value" > "single") ss= "" > exec (com3) ss= "ss=" > ss= (alt) # alt

6. Input and output

Print (): printout

Input (): get the content output by the user

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print ("hello", "world", ss= "attribute" > sep=ss= "attribute-value" > "*", ss= "attribute" > end=ss= "attribute-value" > "@") # sep: what connection to use for printed content, end: what to end with ss= "" > # hello*world@

7. Memory dependent

Hash (): gets the hash value of the object (int, str, bool, tuple). Hash algorithm: (1) the purpose is unique. (2) dict search efficiency is very high, hash table. Trading space for time consumes more memory.

Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > s = ss= "attribute-value" > 'alex' ss= "> print (hash (s)) #-1683248450430382 ss=" alt "> ss=" > ss= "alt" > ss= "attribute" > lst = [1,2,3,4,5] ss= "> print (hash (lst)) # error report The list is ss= "alt" > id (): get the memory address of the object ss= "" > ss= "alt" > ss= "> ss=" attribute "> s = ss=" attribute-value "> 'alex' ss=" alt "> print (id (s)) # 2278345368944

8. File operation related

Open (): used to open a file and create a file handle

Hongmeng official strategic cooperation and co-construction-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > f = ss= "attribute-value" > open ('file',ss= "attribute" > mode=ss= "attribute-value" >' ritual force "ss=" attribute "> encoding=ss=" attribute-value "> 'utf-8') ss=" > f.read () ss= "alt" > f.close ()

9. Module correlation

_ _ import__ (): used to dynamically load classes and functions

Hongmeng official strategic cooperation-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > # allows users to enter a module to import ss= "" > import os ss= "alt" > ss= "attribute" > name = ss= "attribute-value" > input ("Please enter the module you want to import:") ss= "> _ import__ (name) # can import the module dynamically

10. Help

Help (): a function is used to view a detailed description of the purpose of a function or module

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print (help (str)) # View the use of strings

11. Call related

Callable (): used to check whether an object is callable. If True is returned, the call to object may fail, but if False. The call will never succeed.

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > ss= "attribute" > a = ss= "attribute-value" > 10 ss= "" > print (callable (a)) # False variable a cannot be called ss= "alt" > # ss= "> def f (): ss=" alt "> print (" hello ") ss=" > print (callable (f)) # True function can be called

twelve。 View built-in properties

Dir (): view the built-in properties of the object, accessing the _ _ dir__ () method in the object

Hongmeng official strategic cooperation to build-- HarmonyOS technology community ss= "dp-xml" > ss= "alt" > print (dir (tuple)) # the method of viewing tuples thank you for reading, the above is the content of "what built-in functions does Python have". After the study of this article, I believe you have a deeper understanding of what built-in functions Python has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report