02, built-in function
input () user interaction.
Len () iterates over the length of an object.
sum () sum.
open () opens the file.
print () prints the results.
ID () to see the memory address.
Forzenset () freezes.
Int ()
Range () customizes a list of numeric ranges.
str ()
dict ()
dir () View all methods of an object
list ()
Type () view Types
Globals () Place all global variables in a dictionary
locals () The local variable at the current position.
bool ()
tuple ()
set ()
Next ()
isinstance ()
Enumerate ()
scope-dependent
**globals () returns a global variable in the form of a dictionary
**locals () a local variable that returns the current position in the form of a dictionary
q=666
def wrapper (argv):
a=3
print (Locals ())
def Inner (argv1):
b=4
c=5
print (Locals ())
Inner (6)
Wrapper (2)
Print (Globals ())
1.2.1 Execution of String type code Eval,exec,complie
***eval Remove quotation marks from a string, returning the corresponding internal value
s=str ({' A ': 1, ' B ': 3})
Dic=eval (s)
Print (Dic,type (DIC))
Print (eval (' + +) ' )
Print (eval (' Print (666))) # None can't read it inside.
* * * EXEC executes string internal code code after execution will return none if it can't be a letter will be an error
t=exec (' a ') #错误例子
print (t)
ret= ' #$$$$$$$$
name=input (' Please enter name: ') strip ()
if name = = ' Alex ':
print (666)
" "
EXEC (ret)
Compile:pass
1.2.2 Input/output related Input,print
name = input (' Please%s enter name: '% ' Alex ')
print (name)
Print (1,2,3,sep= ' * ') #s设定打印元素的连接方式
print (' Alex ', end= ') #end control is line break
f1=open (' Log ', encoding= ' utf-8 ', mode= ' W ')
print (' 666 ', FILE=F1) #file manipulate the file handle and write to the file.
f1.close ()
1.2.6 Call Related
* * * callable: function is used to check whether an object (variable) is callable.
if return true,object still may fail, but if return false, calling object Ojbect will never succeed.
def func1 ():
print (666)
age=16
Print (Callable (' name '))
Print (callable (age))
Print (callable (func1))
1.2.7 View built-in properties
* * dir: Returns a list of variables, methods, and definitions in the current scope when the function has no arguments;
with parameters, returns the properties of the parameter, a list of methods. If the parameter contains method __dir__ (), the method is called. If the parameter does not contain __dir__ (), the method will collect the parameter information to the fullest extent.
Print (dir (str))
1.3 Iterator Generator related
* * * range iter () next ()
Print (len (range ))
l=[1,2,3,4,5]
l_obj=l.__iter__ ()
L_obj=iter (l) $$$$$$$$$$$
print (l_obj)
print (l_obj.__next__ ())
print (Next (l_obj))
1.4.1 Digital Correlation (+)
data Type (4):
bool: Used to convert the given parameter to a Boolean type, or False if there are no arguments.
int: function is used to convert a string or number to an integral type.
Print (int (' 123 '))
Print (int (1.234))
Print (int (1.768))
Mathematical Operations (7):
**abs: The function returns the absolute value of the number.
Print (ABS ( -5))
***divmod: Calculates the result of divisor and divisor, returns a tuple containing quotient and remainder (A//b, a% b).
Print (Divmod (6,3))
**round: Preserves the number of decimal digits for floating-point numbers, and preserves integers by default.
Print (Round (1.251,3)) #后面的3代表保留几位小数 $$$$$$$$$$
*pow: Seek x**y power. (Three parameters for the result of X**y to Z-redundancy)
Print (POW (2,3)) #2的三次幂
Print (POW (2,3,3)) #2的三次幂对3取余数
***sum: Computes the sum of an iterative object (which can be set to an initial value).
Print (sum ([1,2,3,4]))
Print (sum ([1,2,3,4],100)) #100是初始值
* * min: Returns the minimum value of an iterative object (Key,key is the function name, and the minimum value is returned by the rule of the function).
* * min: Returns the minimum value of an iterative object (Key,key is the function name, and the minimum value is returned by the rule of the function)
Print (min ([1,3,5,7,-4]))
Print (min ([1,3,5,7,-9],key=abs))
1.4.2 and data structure correlation (in)
lists and Ganso (2)
List : Converts an Iterative object to a list (in the case of a dictionary, by default the key is the element of the list).
tuple: Converts an iterative object into Cheng Yuanju (in the case of a dictionary, the key is the Ganso element by default).
tu= (a)
Print (List (TU))
l1=list ({' name ': ' Alex ', ' age ': +})
print (L1)
related built-in functions (2)
* * * Reversed: Flips a sequence and returns an iterator for this flip sequence.
l1=[11,22,33,44,77,66]
l_obj=reversed (L1)
print (l_obj) #直接打印迭代器是一个地址 to be used for loop .
For i in L_obj:
print (i)
* * * bytes:str---> bytes
S1 = ' Alex '
b1=s1.encode (' utf-8 ')
Print (B1)
b2=bytes (s1,encoding= ' utf-8 ')
print (B2)
* * * repr: Returns the string form of an object (the True colors).
msg= ' fractional%f '% (1.237)
Print (msg)
msg= ' name:%r '% (' Alex ')
Print (msg)
Print (repr (' {"name": "Alex"} ')) # $$$$$$$$$$
print (' {' name ': ' Alex '} ')
* * * len: Returns the number of elements in an object.
* * * sorted: Sorts all objects that can be iterated. Returned is the list
print (sorted ([1,2,3,4,5,-6]))
print (sorted ([1,2,3,4,5,-6],key=abs))
l=[(' A ', 1), (' C ', 2), (' d ', 4), (' B ', 3),]
print (sorted (L))
def func1 (x): #$$$$$$$$$
return x[1]
l=[(' A ', 1), (' C ', 2), (' d ', 4), (' B ', 3),]
print (sorted (l,key=func1))
*all: Can iterate over objects, all true is true
*any: Can iterate over objects, with a true is true
Print (All ([1, ' Alex ', True, (+)] ))
Print (All ([0, ' Alex ', True, (+)] ))
print (Any ([0, ', False, (+)] ))
print (Any ([0, ', False, ()]))
***zip Zipper Method The return is an iterator
l1=[1,2,3,4]
tul= (' Old boy ', ' Alex ', ' Wusir ')
l2=[' * ', ' * * ', ' * * * ', ' * * * '
obj=zip (L1,TUL,L2)
For i in obj:
print (i)
***map: Cycle mode
def func2 (x):
return x**2
Obj=map (func2,[1,2,3,4])
For i in obj:
print (i) #for遍历一个迭代器
print ([i**2 for I in [1,2,3,4]]) #列表推导式
def func2 (x, y):
return x+y
Obj1=map (func2,[1,2,3,4,6], (2,3,4,5))
For i in obj1:
print (i)
***filter Filter Mode
def func (x):
return x% 2 ==0
Ret=filter (func,[1,2,3,4,5,6,7])
print (ret)
For i in RET:
print (i)
print ([I for I in [1, 2, 3, 4, 5, 6, 7] if I% 2 = = 0])
***lambda anonymous function one sentence function
def func (x): return x% 2==0
def func1 (x, y):
return x+y
ret =lambda x,y:x+y
Print (ret (2,3))
def func1 (x, y):
return x if x>y else y
Ret=lambda x,y:x If x>y else y
Print (ret (2,3))
def func (x):
return x% 2 = = 0
ret = filter (lambda x:x% 2 = = 0, [1, 2, 3, 4, 5, 6, 7])
print (ret)
For i in RET:
print (i)
# [1, 2, 3, 4, 5, 6, 7] [1,4,9,16 ...] /Map Lambda
Ret2 = map (lambda x:x**2,[1, 2, 3, 4, 5, 6, 7])
For i in Ret2:
print (i)
def func1 (x):
return x[1]
L = [(' A ', 1), (' C ', 2), (' d ', 4), (' B ', 3),]
print (sorted (L, key=func1))
students = [(' John ', ' A ', '), (' Jane ', ' C ', '), (' Dave ', ' B ', ten)] $$$$$$$$$$$$$$$$$$
L = sorted (students, key= Lambda x:x[2],reverse=true)
print (L)
sorted Max min Map filter
Python------Built-in functions