第5章

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第5章相关的知识,希望对你有一定的参考价值。

5-1:

标准整型通常的取值范围是:-2147483648~2147483647,长整型能表达的数值仅与机器支持的(虚拟)内存大小有关,即能轻松表达很大的整型。在一个整型值后面加个L即表示长整型。

5-2:

#!/usr/bin/env python
def myProduct(a, b):  #(a)
    return a * b

x = 5  #(b)
y = 6
ans = myProduct(x, y)
print ans

5-3:

#!/usr/bin/env python
test = int(raw_input(Enter test score: ))
if 90 <= test <= 100:
    print A
elif 80 <= test <= 89:
    print B
elif 70 <= test <= 79:
    print C
elif 60 <= test <= 69:
    print D
elif test < 60:
    print F

5-4:

#!/usr/bin/env python
year = int(raw_input(-> ))
if (year%4 == 0 and year%100 != 0) or (year%400 == 0):
    print leap year
else:
    print not leap year

5-5:

#!/usr/bin/env python
money = 100 * float(raw_input(-> ))
quarter = money / 25
money = money % 25
dimes = money / 10
money = money % 10
nickel = money / 5
money = money % 5
penny = money / 1
print %d quarter, %d dimes, %d nickel, %d penny % (quarter, dimes, nickel, penny)

5-6:

#!/usr/bin/env python
exp = raw_input().split()
op = exp[1]
if op == +:
    print float(exp[0]) + float(exp[2])
elif op == -:
    print float(exp[0]) - float(exp[2])
elif op == *:
    print float(exp[0]) * float(exp[2])
elif op == /:
    print float(exp[0]) / float(exp[2])
elif op == %:
    print float(exp[0]) % float(exp[2])
elif op == **:
    print float(exp[0]) ** float(exp[2])

5-7:

#!/usr/bin/env python
from decimal import Decimal
billing = Decimal(raw_input())
tax = billing * Decimal(0.06)
print tax

5-8:

#!/usr/bin/env python
import math

def sqcube():
    s = float(raw_input(enter length of one side: ))
    print the area is:, s ** 2., (units squared)
    print the volumn is:, s ** 3., (cubic units)

def cirsph():
    r = float(raw_input(enter length of radius: ))
    print the area is:, math.pi * (r ** 2.), (units squared)
    print the volumn is:, (4. / 3.) * math.pi * (r ** 3.), (cubic units)

sqcube()
cirsph()

5-9:

(a):因为017为八进制表示,它所代表的十进制数为15,同理,八进制的032代表十进制的26

(b):因为两个操作数分别为56L和78L,不是561和781,错将小写l误认为1

5-10:

#!/usr/bin/env python
from __future__ import division

def f2c(f):
    c = (f - 32) * (5 / 9)
    return c

5-11:

(a):

#!/usr/bin/env python
from __future__ import division

def f2c(f):
    c = (f - 32) * (5 / 9)
    return c

print f2c(10)

(b):

#!/usr/bin/env python
num = 0
while num <= 20:
    if num % 2 == 1:
        print num
    num += 1

(c):

判断是否能被2整除,如果能,就是偶数,否则就是奇数。

(d):

#!/usr/bin/env python
def f(a, b):
    if (a % b == 0) or (b % a == 0):
        return True
    return False

print Enter two numbers
x = int(raw_input(->))
y = int(raw_input(->))
print f(x, y)

5-12:

import sys

print sys.maxint    #int最大值
print -sys.maxint-1    #int最小值
print sys.float_info.max    #float最大值
print sys.float_info.min    #float最小值

长整型能表达的数值仅仅与机器支持的(虚拟)内存大小有关。

通过type(num.real), type(num.imag)可知复数类型的实部和虚部都是浮点型,而前面已经得到了浮点型的范围。

 

5-13:

print Enter a time such as 3:50
t = raw_input(->)
minute = int(t.split(:)[0]) * 60 + int(t.split(:)[1])
print minute

 5-14:

#!/usr/bin/env python
def f(rate):
    ans = (1 + rate) ** 365 - 1
    return ans

5-15:

def gcd(a, b):
    while True:
        if a % b == 0:
            return b
        else:
            a, b = b, a % b

def lcm(a, b):
    return a * b / gcd(a, b)

5-16:

#!/usr/bin/env python
ob = float(raw_input(Enter opening balance: ))
mp = float(raw_input(Enter monthly payment: ))
print      \tAmount\tRemaining
print Pymt#\t Paid \t Balance
print -----\t------\t---------
count = 0
paid = 0
remain = ob
while remain >= 0:
    print %d\t$%5.2f\t$%6.2f % (count, paid, remain)
    if remain == 0:
        break
    count += 1
    if remain > mp:
        paid = mp
    else:
        paid = remain
    remain -= paid

5-17*:

#!/usr/bin/env python
import random
N = random.randint(2, 100)
count = 0
alist = []
while count < N:
    alist.append(random.randint(0, 2**31 - 1))
    count += 1
c = random.randint(2, N)
count = 0
blist = []
while count < c:
    ele = random.choice(alist)
    blist.append(ele)
    alist.remove(ele)
    count += 1
blist.sort()
print blist

 

以上是关于第5章的主要内容,如果未能解决你的问题,请参考以下文章

PHP初学者第5章-数组-代码块1

复现经典:《统计学习方法》第 7 章 支持向量机

编写高质量代码:改善Java程序的151个建议(第3章:类对象及方法___建议36~40)

《Python从小白到大牛》第5章 Python编码规范

《编写可读代码的艺术》《第5章 该写什么样的注释》

第5章小结