5-1.整型。讲讲Python普通整型和长整型的区别。
标准整型类型是最通用最基本的数字类型等价于C语言的长整型,一般以十进制表示。
长整型则是标准整型类型的超集,当需要用到比标准整型类型更大的整型时,长整型就大有作为了。在一个整型后面加上L(l也是可行的),表示整型为长整型。
5-2.操作符。
(a).写一个函数,计算并返回两个数的乘积。
def Multipliers(a, b):
return a * b
(b).写一段代码调用这个函数,并显示它的结果。
def Multipliers(a, b):
return a * b
result = Multipliers(2, 3)
print("The result is %d" % result)
5-3.标准类型操作符。写一段脚本,输入一个测验成绩,根据下面的标准,输出他的评分成绩(A-F).
A:90~100
B:80~89
C:70~79
D:60~69
F:<60
def testScore():
score = int(raw_input("Input your real grade: "))
if score >= 90 and score <= 100:
print("Your testscore is A.")
elif score >= 80 and score <= 89:
print("Your testscore is B.")
elif score >= 70 and score <= 79:
print("Your testscore is C.")
elif score >=60 and score <= 69:
print("Your testscore is D.")
elif score < 60:
print("Your failed in this test and got a F.")
else:
print("Try input some numbers.")
testScore()
5-4.取余。判断给定年份是否是闰年。使用下面的公式。
def testYear():
years = int(raw_input("Input a year in your mind: "))
if (years % 4 == 0 and years % 100 != 0) or (years % 4 == 0 and years % 100 == 0):
print("This year is a leap year.")
else:
print("This is a normal year.")
testYear()
5-5.取余。取一个任意小于1美元的金额,然后计算可以换成最少多少枚硬币。
def usDollar():
dollars = float(raw_input("Input under 1 dollar like 0.76: "))
dollars *= 100
quartcoin = dollars // 25
tencoin = (dollars - quartcoin * 25) // 10
fivecoin = (dollars - quartcoin * 25 - tencoin * 10) // 5
onecoin = (dollars - quartcoin * 25 - tencoin * 10 - fivecoin * 5)
print("Dollars is %d Quartcoin and %d Tencoin and %d Fivecoin and %d Onecoin."
% (quartcoin, tencoin, fivecoin, onecoin))
usDollar()
5-6.算术。写一个计算器程序。你的代码可以接受这样的表达式,两个操作数加一个操作符:N1操作符N2。
def Calculator(): N1 = int(raw_input("Input number 1: ")) N2 = float(raw_input("Input number 2: ")) sign = raw_input("Input what you want N1 N2 do: ") if sign == '+': print("N1 + N2 == %f" % (N1 + N2)) elif sign == '-': print("N1 - N2 == %f" % (N1 - N2)) elif sign == '*': print("N1 * N2 == %f" % (N1 * N2)) elif sign == '/': print("N1 / N2 == %f" % (N1 / N2)) elif sign == '%': print(N1 % N2) elif sign == '**': print("N1 ** N2 == %f" % (N1 ** N2)) else: print("Out of ranges.") Calculator()
5-7.营业税。随意取一个商品金额,然后根据当地营业税额度计算应该交纳的营业税。
假设税率为5%
def taxes(m):
percent = 0.05
tax = earnmoney * percent
return tax
if __name__ == '__main__':
earnmoney = int(raw_input("Input your earn money number: "))
print taxes(earnmoney)
5-8.几何。计算面积和体积。
(a).正方形和立方体
def geo():
side = int(raw_input("Input one side with figure: "))
print("[s] means square, [c] means cubic")
figures = raw_input("input figure you want to calculate: ")
if figures == 's':
a = side * side
print("The area is %d. " % a)
elif figures == 'c':
a = side * side * 6
b = side * side * side
print("The area is %d. The bulk is %d." % (a ,b))
geo()
(b).圆和球
def geo():
side = float(raw_input("Input one side with figure: "))
print("[s] means square, [c] means cubic, [r] means round, [sp] means spheres.")
figures = raw_input("input figure you want to calculate: ")
if figures == 's':
a = side * side
print("The area is %d. " % a)
elif figures == 'c':
a = side * side * 6
b = side * side * side
print("The area is %d. The bulk is %d." % (a, b))
elif figures == 'sp':
a = 4 * 3.14 * side * side
b = 4 / 3 * 3.14 * side * side * side
print("The area is %f. The bulk is %f." % (a, b))
elif figures == 'r':
a = 3.14 * side * side
print("The area is %f." % a)
else:
print("Please follow the orders.")
geo()
转:
# -*- coding: utf-8 -*-
import math
if __name__=='__main__':
ss = input()
print '正方形面积:',ss*ss
print '立方体面积:',ss*ss*6
print '立方体体积:',ss*ss*ss
print '圆面积:',ss*ss*math.pi
print '球面积:',ss*ss*math.pi*4
print '球体积:',ss*ss*ss*math.pi*4/3.0
5-9.(a).
>>> 17 + 32
49
>>> 017 + 32
47
>>> 017 + 032
41
因为在整型的数值前加上0表示这个数是一个八进制的数。所以017换算成十进制为15,032转换成十进制为26,与下面两个计算结果符合。在十进制数前加0x表示十六进制数。
(b).使用长整型运算更高效。
5-10.转换。写一对函数来进行华氏度到摄氏度的转换。
from __future__ import division
def transForm():
print("[f] means Fah, [c] means Cel.")
Fah = float(raw_input("Input Fahrenheit: "))
Cel = float(raw_input("Input Celsius: "))
choice = raw_input(">>> ")
if choice == 'f':
Cel = (Fah - 32) * (5 / 9)
print(Cel)
elif choice == 'c':
Fah = (Cel * (9 / 5)) + 32
print(Fah)
else:
print("Please follow orders.")
transForm()
5-11.取余。
(a).使用循环和算术运算,求出0~20之间的所有偶数。
def reMainder():
for i in range(21):
if i % 2 == 0:
print("The dual is %d" % i)
reMainder()
(b).
def reMainder():
for i in range(21):
if i % 2 == 1:
print("The dual is %d" % i)
reMainder()
(c).对该数除2取余数,为0就是偶数,为1就是奇数。
(d).
def reMainder():
inti1 = int(raw_input("Input an int number: "))
inti2 = int(raw_input("Input an other number: "))
if inti1 % inti2 == 0:
return True
else:
return False
print(reMainder())
5-12.系统限制。写一段脚本确认一下你的Python所能处理的整型,长整型,浮点型和复数的范围。
Pass
5-13.转换。写一个函数把由小时和分钟表示的时间转换为只用分钟表示的时间。
def tranS(h, m):
mins = hour * 60 + min
return mins
hour = int(raw_input("Input hours: "))
min = int(raw_input("Input minutes: "))
if (hour < 23 and hour > -1) and (min < 60 and min > -1):
print("The times is %d." % tranS(hour, min))
else:
print("Input some right numbers.")
def inTerest(i, p):
return p * (1 + i) ** 365
i = float(raw_input("Input rates: "))
p = int(raw_input("Input your money: "))
print("Your interest is %f." % inTerest(i, p))
5-15.最大公约数和最小公倍数。请计算两个整型的最大公约数和最小公倍数。
转
# -*- coding: utf-8 -*-
def gongyueshu(m , n):
if m< n:
min = m
else:
min = n
for i in range(min , 0 ,-1):
if m % i ==0 and n % i ==0:
return i
return 0
def gongbeishu(m , n):
l = gongyueshu(m,n)
return m * n / l
if __name__ == '__main__':
m = input()
n = input()
print '最大公约数:',gongyueshu(m, n)
print '最小公倍数:',gongbeishu(m, n)
5-16.转
# -*- coding: utf-8 -*-
def Payment(cost,total):
count = 0
print ' Amount Remaining'
print 'Pymt# Paid Balance'
print '----- ------ --------'
while True:
print '%-2d $%.2f $%6.2f'%(count,total,cost)
if cost - total >=0:
cost = cost-total
else:
if cost !=0:
print '%-2d $%.2f $%6.2f'%(count+1,cost,0)
break
count += 1
if __name__=='__main__':
cost = input('Enter opening balance:')
total = input('Enter monthly payment:')
Payment(cost,total)