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)的更多相关文章

  1. python 核心编程课后练习(chapter 6)

    6-1 #6-1 #help(string) import string str = "helloworld" substr = "h1e" if string ...

  2. python 核心编程课后练习(chapter 3)

    3-8 #3-8 "makeTextFile.py -- create text file" import os ls = os.linesep #get filename fna ...

  3. python 核心编程课后练习(chapter 2)

    2-4 #2-4(a) print "enter a string" inputstring = raw_input() print"the string is: &qu ...

  4. Python核心编程课后习题-第六章

    1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...

  5. Python 核心编程 课后习题 第五章

    2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...

  6. Python核心编程 课后练习 第二章

    2.4 使用raw_input()函数得到用户输入. (a) 创建一段脚本使用raw_input()函数从用户输入得到一个字符串, 然后显示这个用户杠杠输入的字符串. #coding = utf-8 ...

  7. python核心编程(第二版)习题

    重新再看一遍python核心编程,把后面的习题都做一下.

  8. Python核心编程这本书的一些错误

    <Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...

  9. Python核心编程-描述符

    python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...

随机推荐

  1. TOJ3136

                                                          3136: Ubiquitous Religions 时间限制(普通/Java):2000M ...

  2. mysql数据库的基本操作

    mysql数据库的基本操作dos命令启动mysql服务:net start mysql启动数据库: mysql -uroot -p查看所有的数据库:show databases:新建数据库:creat ...

  3. Swift笔记

    最近从Xcode6 beta4开始到现在的Xcode6.0.1,使用Swift一段时间了,Swift大体来说,语法与java.c++比较接近,相比objective-c要友好多了,也更容易上手,这里记 ...

  4. 在windows平臺下使用cygwin獲取root用戶權限

    最近在使用cygwin時發現一個問題,當我要使用root用戶權限時,竟然創建不了root賬戶.最後在網上找了下後,暫時衹找到了通過更改當前用戶權限獲得root權限的方法,具體如下: 实际环境:win1 ...

  5. unity行为树制作AI简单例子(1)

    用行为树来制作AI是非常方便的,今天就给大家简单介绍一下行为树的强大之处. 所用插件 Behavior Designer v1.421 最开始 我使用过Rain插件,不过用过Behavior Desi ...

  6. IO:InputStream

    InputStream类(java.io.InputStream) public abstract class InputStream extends Object implements Closea ...

  7. Javascript定义类(class)的三种方法

    将近20年前,Javascript诞生的时候,只是一种简单的网页脚本语言.如果你忘了填写用户名,它就跳出一个警告. 如今,它变得几乎无所不能,从前端到后端,有着各种匪夷所思的用途.程序员用它完成越来越 ...

  8. C#读写锁ReaderWriterLockSlim的使用

    读写锁的概念很简单,允许多个线程同时获取读锁,但同一时间只允许一个线程获得写锁,因此也称作共享-独占锁.在C#中,推荐使用ReaderWriterLockSlim类来完成读写锁的功能. 某些场合下,对 ...

  9. web异常流量定位:iftop+tcpdump+wireshark

    一个简单的运维小经验. 场景:web服务器出现异常流量,web集群内部交互出现大流量,需要定位具体的http请求,以便解决问题. 目的:找出产生大流量的具体http请求. 工具:        ift ...

  10. Rails 4.0 移除了 XML 参数解析器。若要使用请加入 actionpack-xml_parser

    拜读了用 Rails 搭建微信公众平台 API之后发现, params[:xml]这个办法在Rails 4里面已经被办掉了,于是就看了一下Rails 4的新特性发现XML Parameter pars ...