美文网首页
Python 100 实例之(21 - 30)

Python 100 实例之(21 - 30)

作者: Kevin_小飞象 | 来源:发表于2020-05-12 17:43 被阅读0次

    example_021.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 猴子偷桃 '
    
    __author__ = 'Kevin Gong'
    
    peach = 1
    for i in range(9):
        peach = (peach + 1) * 2
    print(peach)
    

    example_022.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 比赛对手 '
    
    __author__ = 'Kevin Gong'
    
    a = set(['x', 'y', 'z'])
    b = set(['x', 'y', 'z'])
    c = set(['x', 'y', 'z'])
    c -= set(('x', 'z'))
    a -= set('x')
    
    for i in a:
        for j in b:
            for k in c:
                if len(set((i, j, k))) == 3:
                    print('a:%s,b:%s,c:%s'%(i, j, k))
    

    example_023.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 绘制菱形 '
    
    __author__ = 'Kevin Gong'
    
    def draw(num):
        a = '*' * (2 * (4 - num) + 1)
        print(a.center(9,' '))
        if num != 1:
            draw(num - 1)
            print(a.center(9, ' '))
    
    draw(4)
    

    example_024.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 斐波那契数列Ⅱ '
    
    __author__ = 'Kevin Gong'
    
    a = 2.0
    b = 1.0
    s = 0
    for n in range(1, 21):
        s += a / b
        a, b = a + b, a
    print(s)
    

    example_025.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 阶乘求和 '
    
    __author__ = 'Kevin Gong'
    
    res = 1
    for i in range(20, 1, -1):
        res = i * res + 1
    
    print(res)
    

    example_026.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 递归求阶乘 '
    
    __author__ = 'Kevin Gong'
    
    def factorial(n):
        if n > 1:
            return n * factorial(n-1)
        else:
            return 1
    
    print(factorial(5))
    
    

    example_027.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 递归输出 '
    
    __author__ = 'Kevin Gong'
    
    def rec(strs):
        if len(strs) != 1:
            rec(strs[1:])
        print(strs[0],end='')
    rec(input('string here: '))
    
    

    example_028.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 递归求等差数列 '
    
    __author__ = 'Kevin Gong'
    
    def age(n):
        if n == 1:
            return 10
        return 2 + age(n - 1)
    print(age(5))
    

    example_029.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 递归求等差数列 '
    
    __author__ = 'Kevin Gong'
    
    n = int(input('输入一个正整数:'))
    n = str(n)
    
    print('%d 位数' % len(n))
    print(n[::-1])
    

    example_030.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ' 回文数 '
    
    __author__ = 'Kevin Gong'
    
    n = input('随便输入:')
    a = 0
    b = len(n) - 1
    flag = True
    while a < b:
        if n[a] != n[b]:
            print('不是回文数')
            flag = False
            break
        a , b = a + 1, b - 1
        if flag:
            print('是回文数')
    

    相关文章

      网友评论

          本文标题:Python 100 实例之(21 - 30)

          本文链接:https://www.haomeiwen.com/subject/sbhinhtx.html