Beautiful and concise Python (3)

Let’s continue to share what a line of Python code can accomplish today! It is recommended to collect, it is really useful! ! !

One, Lambda expression
  • Lambda expression with 1 parameter
p = lambda x:x**2
print(p(3))
  • Lambda expression with 2 parameters
p = lambda x,y:x*(y**2)
print(p(3,4))
  • Lambda expression with multiple parameters
p = lambda x, y, z: x * y * z
print(p(1, 2, 3))
Second, check the existence of numbers in the list
  • Example
ls = [1,2,3,4,5,6]
num = int(input('请输入num:'))
if num in ls:
        print('存在')
Three, factorial operation
  • First we import the Python library math
import math
  • Example
def f1(n):
    return math.factorial(n)
n = int(input('请输入数字n:'))
result = f1(n)
print(result)

Guess you like

Origin blog.csdn.net/dongjinkun/article/details/114548972