• python核心编程 第五章 数字


    重点掌握,除法运算,随机数产生,浮点数。

    encoding=utf-8
    from __future__ import division
    实现真正的除法

    #复数

    num = -8.234-1.23j
    print num.real
    print num.imag
    print num.conjugate() #共轭复数
    print str(num)
    print '%s %s %s' % (str(num.real),str(num.imag),str(num.conjugate()))

    绝对值,多次方,进制变换。浮点数保留两位小数。返回商,余数

    77 > 66 == 66 # same as (77 > 66) and (66 == 66)
    
    abs(-1) #绝对值
    coerce(1.2,1)#数据类型转换函数
    divmod(10,3) #返回商,余数
    pow(3,2) #3的2次方
    round(3.2565564,3)#浮点数四舍五入。三位小数
    
    
    hex(255)    #十进制变十六进制
    oct(17)        #十进制变8进制

    '----------------------excerice-----------------------'
    5-2 运算符
    (a) 写一个函数,计算并返回两个数的乘积
    (b) 写一段代码调用这个函数,并显示它的结果

    def mul(num1,num2):
        return num1*num2
    print 'cacolate multiply of two number'
    print 'input first number'
    first = int(raw_input())
    print 'input second number'
    second = int(raw_input())
    print 'answer is : %d' % mul(first,second)

    5-3 标准类型运算符. 写一段脚本,输入一个测验成绩,根据下面的标准,输出他的评分
    成绩(A-F)。
    A: 90–100
    B: 80–89
    C: 70–79
    D: 60–69
    F: <60

    def desicion(score):
        if 90 <= score <= 100:
            print 'A'
        elif 80 <= score <= 89:
            print 'B'
        elif 70 < score <= 79:
            print 'C'
        elif 60 <= score <= 69:
            print 'D'
        elif score <= 60:
            print 'E'
        else:
            print 'Error'
    
    score = float(raw_input('input score:'))
    desicion(score)

    5-4 取余。判断给定年份是否是闰年。使用下面的公式:
    一个闰年就是指它可以被 4 整除,但不能被 100 整除, 或者它既可以被 4 又可以被 100 整
    除。比如 1992,1996 和 2000 年是闰年,但 1967 和 1900 则不是闰年。下一个是闰年的整世
    纪是 2400 年。

    def is_runyear(year):
        if (year%4 == 0 and year%100 != 0) or (year%400 == 0 ):
            print "yes"
        else:
            print 'no'
    
    while True:
        year = raw_input('input year:')
        if year == 'z':
            break
        else:
            year = int(year)
        is_runyear(year)

    5-5 取余。取一个任意小于 1 美元的金额,然后计算可以换成最少多少枚硬币。硬币有 1
    美分,5 美分,10 美分,25 美分四种。1 美元等于 100 美分。举例来说,0.76 美元换算结果
    应该是 3 枚 25 美分, 1 枚 1 美分。类似 76 枚 1 美分,2 枚 25 美分+2 枚 10 美分+1 枚 5 美分+1
    枚 1 美分这样的结果都是不符合要求的。

    def change(money):
        money = int(money * 100)
        money25 = money / 25
        if money25 != 0:
            print 'can change %d 25 cent' % money25
        money -= money25 * 25
        print money
        money10 = money /10
        if money10 != 0:
            print 'and  %d 10 cent' % money10
        money -= money10 * 10
        money5 = money / 5
        if money5 != 0:
            print 'and %d 5 cent' % money5
        money -= money5 * 5
        if money != 0:
            print 'and change %d cent' % money
    
    money = float(raw_input('input change money:'))
    if  0<money<1:
        change(money)
    else:
        print 'input error'

    5-6 算术。写一个计算器程序 你的代码可以接受这样的表达式,两个操作数加一个运算符:
    N1 运算符 N2. 其中 N1 和 N2 为整数或浮点数,运算符可以是+, -, *, /, %, ** 分别表示
    加法,减法, 乘法, 整数除,取余和幂运算。计算这个表达式的结果,然后显示出来。提示:
    可以使用字符串方法 split(),但不可以使用内建函数 eval().

    while True:
        caculate_input = raw_input('input like N1 + N2:')
        if caculate_input == 'z':
            break
        N1,f,N2 = caculate_input.split(' ',2)
        #split()分割,去
        N1 = int(N1)
        N2 = int(N2)
        if f == '+':
            print N1 + N2
        elif f == '-':
            print N1 - N2
        elif f == '*':
            print N1 * N2
        elif f == '/':
            print N1 / N2
        elif f == '%':
            print N1 % N2
        elif f == '**':
            print N1 ** N2
        else:
            print 'now dont have this caculate %s' % f



    5-10 转换。写一对函数来进行华氏度到摄氏度的转换。转换公式为 C = (F - 32) * (5 / 9)
    应该在这个练习中使用真正的除法, 否则你会得到不正确的结果。

    def temperature_change(F):
        C = (F - 32) * (5 / 9)
        print round(C,3)    #保留三位小数
    
    print '5 / 9 is %f ' % (5/9)
    F = raw_input('F is ')
    temperature_change(float(F))

    5–15. 最大公约数和最小公倍数。请计算两个整数的最大公约数和最小公倍数。

    def max_min(m,n):
        if m < n:
            m,n=n,m
        M,N = m,n
        while True:
            r = m % n
            m,n = n,r
            if r == 0:
                print 'min is %d,max is %d' % (m,M*N/m)
                break
            elif r == 1:
                print 'min is 1,max is %d' % (M*N)
                break
    m = int(raw_input('m is'))
    n = int(raw_input('n is'))
    print 3 / 2
    max_min(m,n)

    5-16 家庭财务。给定一个初始金额和月开销数, 使用循环,确定剩下的金额和当月的支
    出数, 包括最后的支出数。 Payment() 函数会用到初始金额和月额度, 输出结果应该类似下
    面的格式(例子中的数字仅用于演示):
    Enter opening balance:100.00
    Edit By Vheavens
    Edit By Vheavens
    Enter monthly payment: 16.13
    Amount Remaining
    Pymt# Paid Balance
    ----- ------ ---------
    0 $ 0.00 $100.00
    1 $16.13 $ 83.87
    2 $16.13 $ 67.74
    3 $16.13 $ 51.61
    4 $16.13 $ 35.48
    5 $16.13 $ 19.35
    6 $16.13 $ 3.22
    7 $ 3.22 $ 0.00

    balance = round(float(raw_input('enter opening balance:')),2)
    payment = round(float(raw_input('enter monthly payment:')),2)
    
    print 'Pymt# Paid Balance'
    i = 0
    print '0  $ 0.00  $ %.2f' % balance
    while balance > payment:
        i += 1
        balance -= payment
        print '%d  $ %.2f  $ %.2f '% (i,payment,balance)
    
    i += 1
    payment += balance
    print '%d  $ %.2f  $ 0.00 '% (i,payment)


    5-17 随机数。熟读随机数模块然后解下面的题:
    生成一个有 N 个元素的由随机数 n 组成的列表, 其中 N 和 n 的取值范围分别为: (1 <
    N <= 100), (0 <= n <= 231 -1)。然后再随机从这个列表中取 N (1 <= N <= 100)个随机数
    出来, 对它们排序,然后显示这个子集。

    import random
    
    N = random.randint(1,10)
    print N
    all = []
    i = 0
    print 'all[] is'
    while i < N:
        all.append(random.randint(0,231))
        print all[i];
        i += 1
    
    all2 = []
    j = 0
    while j < N:
        all2.append(random.choice(all))
        j += 1
    
    print 'all[2] is ';
    i = 0
    while i < N:
        print all2[i];
        i += 1
    
    i = 1
    def InsertSort(all2, N):
        while i<N:
            if all2[i] < all2[i-1]:
                j = i - 1
                x = all2[i]
                all2[i] = all2[i-1]
                while x<all2[j]:
                    all2[j+1] = all2[j]
                    j -= 1
            all2[j+1] = x
    
    print 'sorted all[2] is ';
    i = 0
    while i < N:
        print all2[i];
        i += 1
  • 相关阅读:
    Python笔记_第一篇_面向过程_第一部分_7.文件的操作(.txt)
    Python笔记_第一篇_面向过程_第一部分_6.语句的嵌套
    Python笔记_第一篇_面向过程_第一部分_6.其他控制语句(with...as等)
    Python笔记_第一篇_面向过程第一部分_6.循环控制语句(while 和 for)_
    Python笔记_第一篇_面向过程_第一部分_6.条件控制语句(if)
    matplot 代码实例
    python下的MySQLdb使用
    vim操作笔记
    使用k-近邻算法改进约会网站的配对效果
    python 读取文本
  • 原文地址:https://www.cnblogs.com/lovely7/p/5729952.html
Copyright © 2020-2023  润新知