python base (15): built-in function

Keywords: Python encoding ascii

1. Built-in functions

What are built-in functions? They are functions that Python gives you, such as print, input, etc. There are 68 built-in functions available up to Python version 3.6.2.They were provided directly to us by python, and
Today we know the built-in function of python.
Domain correlation:
locals(): Returns the name in the current domain
globals(): Returns the name in the global domain
Iterator correlation:
range(): into data
next(): the iterator holds down times, the internal reality makes the next ()method return the next items of the iterator
iter(): Gets the iterator, and internally actually makes the u iter_ ()method to get the iterator
String type code implementation:
eval(): Enforce string type code. and return the final result
print(eval("2+2")) # 4
n = 8
print(eval("2+n")) # 10
def func():
  print(666)
eval("func()") # 666
exec(): code that implements string type
exec("""
for i in range(10):
   print(i)
""")
exec("""
def func():
   print("I'm Jay Chou")
func()
""")
compile(): Varies the code of a string type so that a code object can be evaluated by an exec statement or eval()
'''
//Parameter description:
 1. resource Code to enforce_, Dynamic Code Segment
 2. Part Name, Name of the file where the code is stored, When the first parameter is passed, This parameter is empty
 3. Pattern, There are three values,
 1. exec: When putting some flow statements in place
 2. eval: resource Store only 12 evaluation expressions.
 3. single: resource When stored code interacts. mode Should be single
'''
code1 = "for i in range(10): print(i)"
c1 = compile(code1, "", mode="exec")
exec(c1)

code2
= "1+2+3" c2 = compile(code2, "", mode="eval") a = eval(c2) print(a)
code3
= "name = input('Please enter your name:')" c3 = compile(code3, "", mode="single") exec(c3) print(name)
Code_eval() as a string with a return value, code exec() as a string without a return value, and to compile() rarely.
Input and output are related:
input(): Get user input
print(): printout
Memory correlation:
hash(): Gets the hash value of the object (int, str, bool, tuple)
id(): Gets the memory address of the object
Operation related:
open(): Open pieces in to create handle
Module related:
_u import_u():In dynamically loading classes and functions
Help:
help(): Function Detailed description of how to view a function or module
Ticking correlation:
callable(): In checking whether objects are adjustable, if True is returned, the object may fail to adjust, but if False is returned, the tune will never succeed
View built-in properties:
dir(): View the object's built-in properties, method, access the u dir_u()method in the object
Base data types are relevant:
Number correlation:
bool(): Converts the given data to a bool value and returns False if no value is given
int(): Converts the given data to an int value and returns 0 if no value is given
flfloat(): Converts a given data to an flfloat value, that is, the number
complex(): create the complex number, the real part of the parameter, the imaginary part of the parameter, or the direct string of the parameter to describe the complex number
Binary conversion:
bin(): Converts the given parameter to skeleton
otc(): Converts the given parameter to octal
hex(): converts the given parameter to hexadecimal
Mathematical operations:
abs(): Returns the absolute value
divmode(): returns quotient and remainder
round(): rounding
pow(a, b): Find the b-power of A. If there are three parameters, balance the third number after finding the power
sum(): sum
min(): Find the maximum value
max(): find the maximum value
Related to data structure:
Lists and tuples:
list(): Convert 120 iterative objects into a list
tuple(): converts a tuple of iterative objects into tuples
reversed(): flips a sequence to return the iterator of the flipped sequence
slice(): slice of list
st = "Home, I am Mahjong"
s = slice(1, 5, 2)
print(st[s])
String correlation:
str(): Convert data to string
 format(): related to specific data, in calculating various numbers, actuarial, etc.
# Character string
print(format('test', '<20')) # Left Pair Mushroom
print(format('test', '>20')) # Right Pair Mushroom
print(format('test', '^20')) # Centered
# numerical value
print(format(3, 'b')) # Quantitative
print(format(97, 'c')) # convert to unicode character
print(format(11, 'd')) # Musk base
print(format(11, 'o')) # _Binary
print(format(11, 'x')) # Mushroom Hexadecimal(_Writing_)
print(format(11, 'X')) # Mushroom Hexadecimal(Writing)
print(format(11, 'n')) # and d Mushroom Sample
print(format(11)) # and d Mushroom Sample
# Floating point number
print(format(123456789, 'e')) # Scientific Counting. Default Reserve 6 Bits
print(format(123456789, '0.2e')) # Scientific Counting. Keep 2-digit Number(Writing)
print(format(123456789, '0.2E')) # Scientific Counting. Keep 2-digit Number(Writing)
print(format(1.23456789, 'f')) # _Count of points. Keep 6-digit Number
print(format(1.23456789, '0.2f')) # _Count of points. Keep 2-digit Number
print(format(1.23456789, '0.10f')) # _Count of points. Keep 10 bits
print(format(1.23456789e+10000, 'F')) # _Count of points.
bytes(): Converts a string to a bytes type
s = "Hello"
bs = s.encode("UTF-8")
print(bs)
s1 = bs.decode("UTF-8")
print(s1)
bs = bytes(s, encoding="utf-8") # Encoding strings as UTF-8
print(bs)
bytearray(): Returns an array of new bytes.The elements of this number_are variable, and the range of values for each element is [0,256]
ret = bytearray('alex',encoding='utf-8')
print(ret[0])
print(ret)
memoryview(): View bytes in memory
# See bytes Bytes in Memory
s = memoryview("Spatholobus sinensis".encode("utf-8"))
print(s)
ord(): Enter a character to find a character encoding location
chr(): Enter a position number to find the corresponding character
ascii(): is the ascii code to return the value is not to return \u...
# Find the encoding position for the corresponding character
print(ord('a'))
print(ord('in'))
# Find the character corresponding to the encoding position
print(chr(97))
print(chr(20013))
# stay ascii Return this value in. Return if not\u...
print(ascii('a'))
print(ascii('good'))
repr(): Returns the string form of objects
# repr Is the output unchanged, Neither quotation marks nor escape characters work
print(repr('Home,\n \t My name is Jay Chou'))
print('Hello, my name is Jay Chou')

# %r Write it as it is name = 'taibai' print('My name is%r' % name)
Data collection:
dict(): Create a dictionary
set(): Create sets
frozenset(): Create frozen collections, frozen collections cannot be added and deleted
Other relevant:
len(): Returns the number of elements in a set of objects
sorted(): advance sort on iterative objects (after lamda)
enumerate(): Gets the enumeration object of the collection
lst = ["alex", "wusir", "taibai"]
for index, el in enumerate(lst):
  print(str(index)+"==>"+el)
all(): All of the iterative objects are True, and the result is True
any(): Of the Iterable objects, there are True, and the result is True
print(all([1,2,True,0]))
print(any([1,'',0]))
zip(): function packages the corresponding elements in the object into tuples as parameters, then returns the open table composed of these tuples. If the number of elements in each iterator is not the same, the list is returned with the same degree as the shortest object
l1 = [1,2,3,]
l2 = ['a','b','c',5]
l3 = ('*','**',(1,2,3))
for i in zip(l1,l2,l3):
  print(i)
fifilter(): filter (finished lamda)
map(): the specified sequence is mapped (lamda) based on the function provided

Posted by r.smitherton on Thu, 07 Nov 2019 10:47:16 -0800