a built-in function
1 output print
print(1,2,3,4,5,sep=';',end='| ') ##sep separates each number with ; and end ends with |
2 View built-in properties
##dir View the properties and methods of an object
# import the
# print(dir(s))
3 Mathematically related operations
3.1 abs to calculate the absolute value
# print(abs(-9))
3.2 divmod can return quotient and remainder
# print(divmod(9,4))
3.3 round decimal precision
# print(round(3.1415926,3)) #You can specify a few decimal places after the comma, and it will be rounded up
3.4 pow exponentiation
# print(pow(2,3,5)) #(2**3)%3 is the third power of 2, and the remainder is 5
3.5 sum summation
# print(sum ([1,2,3,4]))
# print(sum(range(1,10)))
#print(sum([1,2,3,4,5],10)) ##After 1 to 5 are summed, then add 10, the default is 0 if not added
3.6 max find the maximum value (important)
print(max(1,2,3,6))
print(max(range(1,10)))
3.7min Find the minimum value (important)
1 # print(min(-1,0,3,4)) 2 3 # print(min(-1,0,3,4,key=abs)) ##After calculating the absolute value, return the minimum value 4 5 # def func(num): 6 7 # return num%2 8 9 # print(min(-2,3,-4,key=func))
3.8 Reverse order
1 # #reverse ##Returns the result directly 2 3 # ret = [1,2,3,4,5] 4 5 # ret.reverse() 6 7 # print(ret) 8 9 10 # reversed ##Returns a reversible Iterate object 11 12 # ret1 = reversed(ret) 13 14 # ret2 = reversed((1,2,3,4,5)) 15 16 # print(ret) 17 18 # print(list(ret1))
4 String related
1 # eval and exec 2 3 # eval('print(123)') 4 5 # exec('print(123)') 6 7 # print(eval('1+2-3*20/(2+3) '))##with return value 8 9 # print(exec('1+2-3*20/(2+3)')) ##executed but without return value
5 format formatted display
#String can provide parameters, specify the alignment, < is left-aligned, > is right-aligned, ^ is center-aligned
# print(format('test', '<20'))
# # print(format('test', '>20'))
# print(format('test', '^20'))
6##ord string converts numbers according to unicode
# print (word ('a'))
7#chr numbers are converted to characters according to unicode
# print(chr(97))
8##repr for %r formatted output
# print(repr(1))
# print(repr('1'))
9 enumerate enumeration
1 # ll=['a','b','c'] 2 3 # for i in ll: 4 5 # print(ll.index(i),i) 6 7 第二种写法 8 9 # for i,v in enumerate(ll): 10 11 # print(i,v)
10 zip returns an iterator, zip is to make them correspond one by one in order
1 # ret = zip([1,2,3,4,5],('a','b','c','d'),(4,5)) #zip method 2 3 # print( ret) 4 5 # for i in ret: 6 7 # print(i)
11filter is important for filtering, such as numbers greater than a few, or even numbers, odd numbers, etc.
1 # lst = [1, 4, 6, 7, 9, 12, 17] 2 3 # def func(num): 4 5 # if num % 2 == 0:return True 6 7 # filter(func,lst) ##分别把lst里面的值传给num,然后取出除2等于0的数字 8 9 # for i in filter(func,lst): 10 11 # print(i)
##第二种方法 13 # g = (i for i in lst if i%2 == 0)
12map 求1到10的平方 重要 主要用于计算
1 # def func(num): 2 3 # return num**2 7 # for i in map(func,range(10)): 8 9 # print(i)
13 排序
##sort 排序(这个只是个方法,不是函数)
1 # l = [1,-4,-2,3,-5,6,5] 2 3 # l.sort(key=abs) ##按照绝对值来排序的 4 5 # print(l)
打印结果是
[1, -2, 3, -4, -5, 5, 6]
##sorted排序(重要)
1 # l = [1,-4,-2,3,-5,6,5] 2 3 # new_l = sorted(l,key=abs,reverse=True) ##按照绝对值,并且反序来排序的 4 5 # print(new_l)
打印结果是
[6, -5, 5, -4, 3, -2, 1]
二 匿名函数(lambda)
匿名函数:为了解决那些功能很简单的需求而设计的一句话函数
1 #普通函数写法: 2 def calc(n): 3 return n**n 4 print(calc(2)) 5 6 7 #换成匿名函数 8 calc = lambda n:n**n 9 print(calc(2)) 10 11 打印结果都是4
匿名函数格式
函数名 = lambda 参数 :返回值 #参数可以有多个,用逗号隔开 #匿名函数不管逻辑多复杂,只能写一行,且逻辑执行结束后的内容就是返回值 #返回值和正常的函数一样可以是任意数据类型
2.1例子
1 ##第一种普通函数写法: 2 # def func(num): 3 # return num ** 2 4 # 5 # for i in map(func,range(10)):print(i) 6 7 8 ##第二种写法:匿名函数 9 10 # for i in map(lambda num : num ** 2 ,range(10)):print i 11 12 13 ##打印结果是0-9,9个数字的平方
#例子
现有两元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}]
第一种写法:普通函数 # def func(t): # return {t[0]:t[1]} # ret = map(func,zip((('a'),('b')),(('c'),('d')))) # print(list(ret)) ##第二种写法:匿名函数 # ret = map(lambda t:{t[0]:t[1]},zip((('a'),('b')),(('c'),('d')))) # print(list(ret))