#定义一个函数与变量的定义非常相似, for a well-known function, you must access it through the variable name
def func (x, y, z = 1):
return x + y + Z
#匿名函数: 1. No name, 2. Function body comes with return
#匿名函数的应用场景, Temporary use
Lambda x, z = 1:x + y + Z
Print (lambda x, z = 1:x + y + z)
f = lambda x, y, z = 1:x + y + Z
Print (f)
F (a)
#内置函数
#1. ABS ()
Print (All ([up to, ' a ', None])) #表示可迭代对象取出的每一个值的布尔值都为真
Print (All ([])) #可迭代对象为空的列表, expressed as true
Print (Any ([])) #表示为False
Print (Any ([', None,false])) #表示为True
Print (Any ([', None,false])) #表示为False
#bin, Oct,hex
Print (BIN) #十进制转二进制
Print (Oct) #十进制转八进制
Print (hex) #十进制转十六进制
#bytes
#字符串本身就是unicode
#unicode------->encode------>bytes
Print (' Hello '. Encode (' Utf-8 '))
Print (bytes (' hello ', encoding = ' utf-8 '))
#callable是否可以被调用
Print (callable (bytes)) #True
Print (callable (ABS)) #True
#chr, Ord.
Print (CHR) #A
Print (CHR) #表示的是 #
Print (Ord (' # ')) #表示的是35
S1 = Frozenset ({1,2,3,4}) #表示的是不可变集合
Import Sys
Sys.path # #path表示来自于sys名称空间的名字
Print (dir (sys)) #查看sys有哪些属性
#divmod
Print (Divmod (10,3)) #结果表示的是除数和商
#enumerate
L = [' A ', ' B ', ' C ']
res = enumerate (l)
For Index,item in Res:
Print (Index,item)
#hash (), which means to examine a string and get a hash value
#help () View Help information
Print (Help ()) #在函数中写注释信息, you can view the
#id: A function implemented by the Python interpreter, not a real memory address
#isinstance
x = 1
Print (Isinstance (x,int)) #判断x是否是int的一个实例
#pow
Print (POW (10,2)) #表示的是10 **2
Print (POW (3,2,2)) #表示的是3 **2%2
#repr: Turning an object into a string
Print (Type (repr (1))) #解释器内部调用
#reversed
L = [1, ' A ', 2, ' C ']
Print (Reversed (l))
#round
Print (Round (3.567,2)) #表示的保留两位小数
#slice
L = [1,2,3,4,5,6]
Print (L[0:4:2])
s = Slice (0,4,2)
Print (L[s]) #可以给多个对象使用
#sorted
L = [1,10,4,3,-1]
Print (sorted (l,reverse = True))
#zip: Zipper (Find two one by one corresponding parts)
s = ' Hello '
L = [1,2,3,4,5]
Print (Zip (s,l))
res = Zip (s,l) #为迭代器
Print (list (res))
#__import__
Import Sys
M_name = input (' Module>>: ')
if m_name = = ' sys ':
m = __import__ (m_name)
Print (m)
Print (M.path)
Salaries = {
' Egon ': 3000,
' Alex ': 100000,
' Wupeiqi ': 1000,
' Yuanhao ': 2000
}
# Print (List (Zip (Salaries.values (), Salaries.keys ()))
# Print (max (list (Zip (Salaries.values (), Salaries.keys ())))
# Print (max (Salaries,key = lambda Name:salaries[name]))
# #filter, Map,reduce
names = [' Alex ', ' Wupeiqi ', ' Yuanhao ', ' Egon ']
# res = map (lambda x:x + ' _sb ', names)
# Print (list (res))
#从functools中导入reduce模块
From Functools import reduce
# Print (Reduce (lambda x,y:x + y,range (101)))
def my_map (seq):
For item in SEQ:
Item = Item + ' _SB '
Yield item
Res1 = My_map (names)
# print (Next (res1))
def my_map (FUNC,SEQ):
For item in SEQ:
Yield func (item)
# res1 = My_map (lambda x:x + ' _sb ', names)
# print (Next (res1))
# #filter函数
names = [' Alex_sb ', ' Wupeiqi ', ' Yuanhao ', ' Egon ']
# Print (List (filter (Lambda name:name.endswith (' SB '), names)))
# #eval与exec
# cmd = ' print (x) '
# x = 1
# eval (cmd)
# eval (cmd,{},{}) #第一个大括号表示的是全局作用域, the second curly brace represents a local scope
# eval (cmd,{' x ': 0},{' y ': 10000})
s = ' For I in Range ':p rint (i) '
Code = Compile (S, ' ', ' exec ')
EXEC (code)
PYTHON__ built-in functions