Usage of python built-in functions and anonymous functions

1. Built-in functions

1. output print

print(1,2,3,4,5,sep=';',end='| ') ##sep是以;分开每个数字,end是以|结尾的

2. View built-in properties

#dir查看某对象的属性及其方法

import os

print(dir(os)) 

3. Mathematics-related operations

1 abs calculates the absolute value
print(abs(-9))
2 divmod can return quotient and remainder
print(divmod(9,4))
3 round decimal precision
print(round(3.1415926,3)) #逗号后面可以指定保留几位小数,并且会四舍五入
4 pow exponentiation
print(pow(2,3,5)) #(2**3)%3 就是2的三次方,对5取余
5 sum sum
print(sum ([1,2,3,4]))

print(sum(range(1,10)))

print(sum([1,2,3,4,5],10))  ##1到5求完和之后,然后再加10,不加默认是0
6 max Find the maximum value (important)
print(max(1,2,3,6))

print(max(range(1,10)))
7 min Find the minimum value (important)
print(min(-1,0,3,4))

print(min(-1,0,3,4,key=abs)) ##算完绝对值之后,在返回最小值

def func(num):

return num%2

print(min(-2,3,-4,key=func))
8 reverse order
#reverse  #直接返回结果

ret = [1,2,3,4,5]

ret.reverse()

print(ret)


#reversed  #返回一个可迭代对象
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078

ret1 = reversed(ret)

ret2 = reversed((1,2,3,4,5))

print(ret)

print(list(ret1))

4. String related

eval and exec

eval('print(123)')

exec('print(123)')

print(eval('1+2-3*20/(2+3)'))#有返回值

print(exec('1+2-3*20/(2+3)')) #执行了但没有返回值

5.format formatted display

The parameters that can be provided by the string specify the alignment, < is left alignment, > is right alignment, ^ is center alignment

print(format('test', '<20'))

print(format('test', '>20'))

print(format('test', '^20'))

6.ord string converts numbers according to unicode

print(ord('a'))

7. Chr numbers are converted to characters according to unicode

print(chr(97))

8.repr is used for %r formatted output

print(repr(1))

print(repr('1'))

9. enumerate enumeration

first way of writing

ll=['a','b','c']

for i in ll:

	print(ll.index(i),i)

second way of writing

for i,v in enumerate(ll):

	print(i,v)

10. zip returns an iterator, and the zipper is to make them correspond one by one in order

ret = zip([1, 2, 3, 4, 5], ('a', 'b', 'c', 'd'), (4, 5,0))  # 拉链方法

print(ret)

for i in ret:
    print(i)

11. filter is mainly used for filtering, such as numbers greater than a few, or even numbers, odd numbers, etc.

first way of writing

lst = [1, 4, 6, 7, 9, 12, 17]
def func(num):
    if num % 2 == 0: return True

filter(func, lst)  #分别把lst里面的值传给num,然后取出除2等于0的数字

for i in filter(func, lst):

    print(i)

The second method

g = (i for i in lst if i%2 == 0)
for i in g:
    print(i)

12. Map Finding the square of 1 to 10 is important mainly for calculation

def func(num):

    return num**2
for i in map(func,range(10)):

    print(i)

13 Sort

sort Sort (this is just a method, not a function)

l = [1,-4,-2,3,-5,6,5]

l.sort(key=abs) #按照绝对值来排序的

print(l)
#打印结果是
[1, -2, 3, -4, -5, 5, 6]

sorted sort (important)

l = [1,-4,-2,3,-5,6,5]

new_l = sorted(l,key=abs,reverse=True) #按照绝对值,并且反序来排序的

print(new_l)
#打印结果是
[6, -5, 5, -4, 3, -2, 1]

2. Anonymous function (lambda)

Anonymous function: a one-sentence function designed to solve the needs of those functions that are very simple

#普通函数写法:
def calc(n):
    return n**n
print(calc(2))


#换成匿名函数
calc = lambda n:n**n
print(calc(2))

#打印结果都是4

anonymous function format

函数名 = lambda 参数 :返回值

There can be multiple parameters, separated by commas.
No matter how complex the logic is, the anonymous function can only write one line, and the content after the logic execution is completed is the return value. The return value
can be of any data type like a normal function.

example

#第一种普通函数写法:
def func(num):
	return num ** 2

for i in map(func,range(10)):

	print(i)

#小编创建了一个Python学习交流群:725638078
#第二种写法:匿名函数

for i in map(lambda num : num ** 2 ,range(10)):
	print (i)


#打印结果是0-9,9个数字的平方

example

There are two tuples (('a'), ('b')), (('c'), ('d')), please use the anonymous function in python to generate a list [{'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))

Guess you like

Origin blog.csdn.net/Python_222/article/details/129751847