zoukankan      html  css  js  c++  java
  • Python3 From Zero——{最初的意识:008~初级实例演练}

    一、构显国际橡棋8x8棋盘

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    color_0="33[41m 33[00m"
    color_1="33[46m 33[00m"
    def line(a, b):
        for i in range(0,48):
            if ((i // 8)  % 2) == 0:
                print(a, end='')
            else:
                print(b, end='')
    for x in range(0, 24):
        if ((x // 4) % 2) != 0:
            line(color_0, color_1)
        else:
            line(color_1, color_0)
        print('
    ', end='')

    二、求正整数平方根

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    #Author: fanhui
    num = input("Please input a number: ")
    num_sqrt = round(float(num)**0.5, 4)
    print(num, "'s sqrt is ", num_sqrt, sep='')
    f@z ~ $ python3 num_sqrt.py 
    Please input a number: 10
    10's sqrt is 3.1623

    三、求负整数平方根

    #!/usr/bin/python3
    #-*- coding: utf-8 -*-
    import cmath
    num = input("Please input a negative num: ")
    negative_sqrt = cmath.sqrt(float(num))
    print(negative_sqrt)
    f@z ~ $ python3 negative_sqrt.py 
    Please input a negative num: -4
    2j

    四、三角形面积——海轮公式:√s(s-a)(s-b)(s-c),s=(a+b+c)*1/2,a、b、c分别为三角形三边长度

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    #Author: fanhui
    a = float(input("Please input the first length: "))
    b = float(input("Please input the second length: "))
    c = float(input("Please input the third length: "))
    s = (a + b + c) / 2
    area = (s * (s - a) * (s -b) * (s - c)) ** 0.5
    print("The triangle's area is: {:,.4f}".format(area))
    f@z ~ $ python3 triangle_area.py 
    Please input the first length: 444
    Please input the second length: 444
    Please input the third length: 444
    The triangle's area is: 85,362.3920

    五、摄时温度转华氏温度:(X * 1.8) + 32

    f@z ~ $ cat temperature.py 
    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    temperature = float(input("Please input a Celsius: "))
    F = temperature * 1.8 + 32
    print("F is: {:.2f}".format(F))
    
    f@z ~ $ python3 temperature.py 
    Please input a Celsius: 22.4
    F is: 72.32

    六、判断年份是否为闰年:被4整除且不被100整除,或能被400整除

    #!/usr/bin/python3
    #-*- coding: utf-8 -*-
    Year = int(input("Please input a Year number: "))
    if (Year % 4 == 0 and Year % 100 != 0) or (Year % 400 == 0):
        print("Yes! It is a leap year.")
    else:
        print("No! It is not a leap year.")
    f@z ~ $ python3 leap_year.py 
    Please input a Year number: 2100
    No! It is not a leap year.
    f@z ~ $ python3 leap_year.py 
    Please input a Year number: 2000
    Yes! It is a leap year.
    f@z ~ $ python3 leap_year.py 
    Please input a Year number: 2016
    Yes! It is a leap year.

    七、判断素数(即:质数)

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    num = int(input("Please input a Integer:"))
    if num in [1, 2]:
        print("Not prime number!")
    else:
        for x in range(2, num):
            if num % x == 0:
                print("Not prime number!")
                break
        else:
            print("Prime number!")
    f@z ~/py_script $ python3 prime_number.py 
    Please input a Integer:1
    Not prime number!
    f@z ~/py_script $ python3 prime_number.py 
    Please input a Integer:2
    Not prime number!
    f@z ~/py_script $ python3 prime_number.py 
    Please input a Integer:3
    Prime number!
    f@z ~/py_script $ python3 prime_number.py 
    Please input a Integer:4
    Not prime number!
    f@z ~/py_script $ python3 prime_number.py 
    Please input a Integer:5
    Prime number!
    f@z ~/py_script $ python3 prime_number.py 
    Please input a Integer:6
    Not prime number!
    Prime Number

    八、整数阶乘:正整数的阶乘等于小于或等于其自身的所有正整数的乘积;0的阶乘为1

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    num = int(input("Please input a non-negative integer: "))
    if num < 0:
        print("Sorry! Negative integer has no factorial.")
    elif num == 0:
        print("0's factorial is 1")
    else:
        factorial = 1
        for x in range(1, num + 1):
            factorial *= x
        print("{}'s factorial is {}".format(num, factorial))
    f@z ~/py_script $ a factorial.py 
    Please input a non-negative integer: 0
    0's factorial is 1
    f@z ~/py_script $ a factorial.py 
    Please input a non-negative integer: -111
    Sorry! Negative integer has no factorial.
    f@z ~/py_script $ a factorial.py 
    Please input a non-negative integer: 3
    3's factorial is 6
    Factorial

    九、九九乘法表

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    for m in range(1, 10):
        print('|', end='')
        for n in range(1, m + 1):
            print('{}*{}={:2d}|'.format(n, m, n * m), end='')
        print('
    ', end='')
    f@z ~ $ python3 99.py 
    |1*1= 1|
    |1*2= 2|2*2= 4|
    |1*3= 3|2*3= 6|3*3= 9|
    |1*4= 4|2*4= 8|3*4=12|4*4=16|
    |1*5= 5|2*5=10|3*5=15|4*5=20|5*5=25|
    |1*6= 6|2*6=12|3*6=18|4*6=24|5*6=30|6*6=36|
    |1*7= 7|2*7=14|3*7=21|4*7=28|5*7=35|6*7=42|7*7=49|
    |1*8= 8|2*8=16|3*8=24|4*8=32|5*8=40|6*8=48|7*8=56|8*8=64|
    |1*9= 9|2*9=18|3*9=27|4*9=36|5*9=45|6*9=54|7*9=63|8*9=72|9*9=81|

    十、斐波那契数列Fibonacci Number:第0项是0,第1项是第一个1,从第三项开始,每一项都等于前两项之和 

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    num = int(input('Please input a positive integer: '))
    a = 0
    b = 1
    while b <= num:
        print(b, end=' ')
        a, b=b, a+b
    print('
    ', end='')
    f@z ~ $ python3 fibonacci.py 
    Please input a positive integer: 40
    1 1 2 3 5 8 13 21 34 

    十一、阿姆斯特朗数Armstrong:如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数

    #!/usr/bin/python3
    #-*- coding: utf-8 -*-
    num = input('Please input a positive integer: ')
    temp = 0
    for x in range(0, len(num)):
        temp += int(num[x]) ** len(num)
    if int(num) == temp:
        print('Your number is an Armstorng Number!')
    else:
        print('Your number is not an Armstrong Number!')
    f@z ~/py_script $ python3 armstrong.py 
    Please input a positive integer: 407
    Your number is an Armstorng Number!
    f@z ~/py_script $ python3 armstrong.py 
    Please input a positive integer: 100
    Your number is not an Armstrong Number!
    Armstrong Number

    十二、获取指定期间内的阿姆斯特朗数

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    def arms(num):
        temp = 0
        for j in range(0, len(num)):
            temp += int(num[j]) ** len(num)
            if temp == int(num):
                print(num)
    
    n1 = int(input('Please input a positive integer: '))
    n2 = int(input('Please input another positive integer: '))
    if n1 > n2:
        n1, n2 = n2, n1
    print('=' * 40, '
    ', 'Armstrong number between {} and {}: '.format(n1, n2))
    for i in range(n1, n2 + 1):
        arms(str(i))
    f@z ~ $ a armstrong2.py 
    Please input a positive integer: 500
    Please input another positive integer: 100
    ======================================== 
     Armstrong number between 100 and 500: 
    153
    370
    370
    371
    407

    十三、构显汉诺塔(hanoi)移动轨迹 

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    def hanoi(n, x='A', y='B', z='C'):
        if n == 1:
            print(x, '~>', z)
        else:
            hanoi(n - 1, x, z, y)
            hanoi(1, x, y, z)
            hanoi(n - 1, y, x, z)
    
    num = int(input('Please input a positive integer: '))
    hanoi(num)
    递归法
    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    import random
    num = int(input('Please input a positive integer: '))
    A = list(range(num - 1, -1, -1))
    B = []
    C = []
    TEMP = [['A', 'B', 'C'], [A, B, C]]
    while len(C) != num:
        x = random.choice(TEMP[1])
        X = TEMP[0][TEMP[1].index(x)]
        y = random.choice(TEMP[1])
        Y = TEMP[0][TEMP[1].index(y)]
        if X != Y:
            if (x[-1:] > y[-1:] or x == []) and y != []:
                x.append(y.pop())
                print(Y, '~>', X, end=' ')
            elif (y[-1:] > x[-1:] or y == []) and x != []:
                y.append(x.pop())
                print(X, '~>', Y, end=' ')
    print(C)
    穷举法

    十四、最大公约数算法

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    def divisor(i, j):
        if i > j:
            i, j = j, i
        for k in range(i, 0, -1):
            if i % k == 0 and j % k == 0:
                print("=" * 44, "
    ", 'The GCD between {} and {} is: {}'.format(i, j, k))
                break
    
    n_0 = int(input('Please input a positive integer: '))
    n_1 = int(input('Please input another positive integer: '))
    
    divisor(n_0, n_1)
    f@z ~ $ python3 GCD.py 
    Please input a positive integer: 24
    Please input another positive integer: 54
    ============================================ 
     The GCD between 24 and 54 is: 6

    十五、显示日历

    >>> import calendar
    >>> print(calendar.month(2016, 9))
       September 2016
    Mo Tu We Th Fr Sa Su
              1  2  3  4
     5  6  7  8  9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30

    十六、递归斐波那契数列

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    def fibo(i):
        if i < 2:
            return 1
        else:
            return  (fibo(i - 1) + fibo(i - 2))
    
    num = int(input('A positive integer: '))
    for j in range(0, num + 1):
        print(fibo(j))
    f@z ~ $ python3 fibo_recursion.py 
    A positive integer: 10
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
  • 相关阅读:
    Navicat Premium 15 永久激活版安装教程
    win10安装redis
    Linux下安装Go环境
    IoT platforms that deserves to be noticed
    TortoiseGit配置
    impot和require区别
    websocket搭建简单的H264实时视频流播放
    应用索引技术优化SQL 语句(Part 3)
    应用索引技术优化SQL 语句(Part 2)
    应用索引技术优化SQL 语句(Part 1)
  • 原文地址:https://www.cnblogs.com/hadex/p/5774312.html
Copyright © 2011-2022 走看看