python 核心编程课后练习(chapter 5)
5-2
#5-2
def mul(x, y):
return x * y print mul(4,5)
5-3
#5-3
def value_score(num):
if 90<=num<=100:
return 'A'
elif 80<=num<=89:
return 'B'
elif 70<=num<=79:
return 'C'
elif 60<=num<=69:
return 'D'
elif 0<=num<=59:
return 'F'
else:
print "invalid value" print value_score(90)
print value_score(1000)
print value_score(0)
5-4
#5-4
def Leap_year(year):
if year%4 == 0 and year%100 == 0 :
return True
elif year%4 == 0 and year%100 != 0 :
return True
else:
return False if Leap_year(2004):
print "leap year"
else:
print "common year"
5-5
#5-5
import random def least_cents(cts):
left = 0
sum = 0
#get the coins of 25cents
sum = cts/25
left = cts%25
#get the coins of 10cents
sum += left/10
left = left%10
#get the coins of 5cents
sum += left/5
left = left%5
#get the coins of 1cents
sum += left return sum cents = random.randint(1, 100) print "%d cents will have at least: %d coins" % (cents, least_cents(cents))
5-6
#5-6 def add(x, y):
return x + y def sub(x, y):
return x - y def mul(x, y):
return x * y def div(x, y):
return float(x)/float(y) def mod(x, y):
return x % y def pow(x, y):
return x ** y def cal(str):
a = []
if(len(str.split('**')) == 2):
a = str.split('**')
return pow(int(a[0]), int(a[1]))
elif (len(str.split('*')) == 2):
a = str.split('*')
return mul(int(a[0]), int(a[1]))
elif (len(str.split('%')) == 2):
a = str.split('%')
return mod(int(a[0]), int(a[1]))
elif (len(str.split('+')) == 2):
a = str.split('+')
return add(int(a[0]), int(a[1]))
elif (len(str.split('-')) == 2):
a = str.split('-')
return sub(int(a[0]), int(a[1]))
else:
print "Not support" print "enter the formula:"
formula_str = raw_input()
print cal(formula_str)
5-7
#5-7
def tax(num):
return num * 0.03 turnover = raw_input("enter the turnover:")
print"the tax you need to turn over to the state: %f" %(tax(int(turnover)))
5-8
#5-8
import math
def suqare_area(len):
return float(len) * float(len) def suqare_volume(len):
return float(len) ** 3 def circle_area(r):
return math.pi * float(r) * float(r) def spere_volum(r):
return 4 * math.pi * float(r) * float(r) * float(r)/ 3.0 len = raw_input("enter the len:") print "the area of suqare is: %f" % suqare_area(len)
print "the volume of square is: %f" % suqare_volume(len) r = raw_input("enter the r:")
print "the area of the circle is: %f" % circle_area(r)
print "the volume of spere is: %f" % spere_volum(r)
5-10
#5-10
def fah_to_centi(c):
return (c - 32)*(5/float(9)) c = raw_input("enter the fahrenheit:") print "the centi degrees is: %f" % fah_to_centi(float(c))
5-11
#5-11
def odd_num(num):
return num %2 == 1 def aliquant(n1, n2):
return n1 % n2 == 0 for e in range(0, 20):
if not odd_num(e):
print "even number:%d" % e for e in range(0, 20):
if odd_num(e):
print "odd number: %d" % e n1 = raw_input("enter the first number:")
n2 = raw_input("enter the second number:")
if aliquant(int(n1), int(n2)):
print"aliquant"
else:
print"not aliquant"
5-12
#5-12
import sys print sys.maxint
print -sys.maxint - 1 print sys.float_info.max
print sys.float_info.min
5-13
#5-13 def hm_to_m(str):
a = str.split(':')
return int(a[0])* 60 + int(a[1]) hm = raw_input("enter the time:") print "the currents mins is: %d" % hm_to_m(hm)
5-14
#5-14
def year_rate():
return (1+0.0385)** 365 - 1.0 print year_rate()
5-15
#5-15
def GCD(m,n):
if m%n == 0:
return n
else:
return GCD(n, m%n) def LCM(m, n):
return m*n/GCD(m, n) m = int(raw_input("enter m:"))
n = int(raw_input("enter n:")) print"the GCD of %d, %d is: %d" % (m, n, GCD(m, n))
print"the LCM of %d, %d is: %d" % (m, n, LCM(m, n))
5-17
#5-17
import random N = random.randint(2, 100)
a = [] def show_all(a):
for i in range(len(a)):
print a[i] for i in range(0, N-1):
tmp = random.randint(2, 2**31)
a.append(tmp)
print "Original:"
show_all(a) for i in range(0, N-1):
for j in range(i, N-1):
if a[i] > a[j]:
a[i], a[j] = a[j], a[i] print"New arrary:"
show_all(a)
python 核心编程课后练习(chapter 5)的更多相关文章
- python 核心编程课后练习(chapter 6)
		
6-1 #6-1 #help(string) import string str = "helloworld" substr = "h1e" if string ...
 - python 核心编程课后练习(chapter 3)
		
3-8 #3-8 "makeTextFile.py -- create text file" import os ls = os.linesep #get filename fna ...
 - python 核心编程课后练习(chapter 2)
		
2-4 #2-4(a) print "enter a string" inputstring = raw_input() print"the string is: &qu ...
 - Python核心编程课后习题-第六章
		
1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...
 - Python 核心编程 课后习题 第五章
		
2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...
 - Python核心编程 课后练习 第二章
		
2.4 使用raw_input()函数得到用户输入. (a) 创建一段脚本使用raw_input()函数从用户输入得到一个字符串, 然后显示这个用户杠杠输入的字符串. #coding = utf-8 ...
 - python核心编程(第二版)习题
		
重新再看一遍python核心编程,把后面的习题都做一下.
 - Python核心编程这本书的一些错误
		
<Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...
 - Python核心编程-描述符
		
python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...
 
随机推荐
- SpringMVC中定时器继承Task后无法对service注入问题
			
最近在做一个Spring+MyBatis的一个项目,其中用到了Redis的存储,然后遇到问题是这样的: RedisTask是通过定时器来每分钟像数据库里推送的,于是就有了 public class R ...
 - psp记录个人项目花费时间
			
预估时间 120min 设计分析时间 30min 具体设计时间 ≍40min 编码时间 ≍1h 测试时间 10min 整理结论时间 10min 总结与探讨时间 5min
 - Javascript.//DOM
			
文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.Document Object Model的历史可以追溯至1990年代后期微 ...
 - FizzBuzz 问题
			
public class FizzBuzz { static int start = 1; static int end = 100; public static void main(String[] ...
 - SolrCloud分布式集群部署步骤
			
Solr及SolrCloud简介 Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成 ...
 - SQL 行转列和列转行
			
SQL 行转列和列转行 行列互转,是一个经常遇到的需求.实现的方法,有case when方式和2005之后的内置pivot和unpivot方法来实现. 在读了技术内幕那一节后,虽说这些解决方案早就用过 ...
 - windows2003 DHCP中批处理绑定IP与MAC
			
最近正在实施Windows Server 2008 R2 DHCP服务器部署,要求把员工的IP地址和MAC以及姓名完成在DHCP服务器上的绑定,使用的系统是windows2003-x64,要添加的用户 ...
 - windows系统下在dos命令行kill掉被占用的pid (转)
			
原文出自:http://www.2cto.com/os/201304/203771.html windows系统下在dos命令行kill掉被占用的pid 1.开始-->运行-->c ...
 - ETL利器Kettle实战应用解析系列二 【应用场景和实战DEMO下载】
			
本文主要阅读目录如下: 1.应用场景 2.DEMO实战 3.DEMO下载 1.应用场景 这里简单概括一下几种具体的应用场景,按网络环境划分主要包括: 表视图模式:这种情况我们经常遇到,就是在同一网络环 ...
 - Zookeeper启动过程
			
在上一篇,我们了解了zookeeper最基本的配置,也从中了解一些配置的作用,那么这篇文章中,我们将介绍Zookeeper的启动过程,我们在了解启动过程的时候还要回过头看看上一篇中各个配置参数在启动时 ...