python学习日记-实战(2)

    • 问题1
    • 问题2
    • 问题3

问题1

定义一个函数,接收3个参数,返回一元二次方程:

ax2+bx+c=0

的两个解。

def solve_quadratic(a, b, c):
    if b * b - 4 * a * c < 0:
        raise ValueError
    a, b = (-b + sqrt(b * b - 4 * a * c)) / (2 * a), (-b - sqrt(b * b - 4 * a * c)) / (2 * a)
    return a, b

问题2

输入

L = ["Hello", "World", 18, "Apple", None]

输出为各个英语单词的小写

[s.lower() for s in L1 if isinstance(s, str)]

问题3

杨辉三角的生成器

def triangles():
    a = [1]
    while True:
        yield a
        a = [a[i-1] + a[i] for i in range(1, int(len(a)))]
        a.insert(0, 1)
        a.append(1)

你可能感兴趣的:(我的学习日记,python)