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. Myeclipse非正常关闭出现问题

    Could not create the view: An unexpected exception was thrown. 解决办法: 关闭myeclipse 原来工作空间的.metadata文件夹 ...

  2. jQuery中$.fn的用法示例介绍

    $.fn是指jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效,下面有个不错的示例,喜欢的朋友可以参考下 如扩展$.fn.abc(),即$.fn.abc()是对jquery ...

  3. 差分:IncDec Sequence 差分数组

    突然就提到了这个东西,为了不再出现和去年联赛看见二分没学二分痛拿二等第一的情况,就去学了一下,基础还是比较简单的-- 先看一个经典例题: 给定一个长度为n的数列{a1,a2...an},每次可以选择一 ...

  4. Monkey工具使用详解

    上节中介绍了Monkey工具使用环境的搭建,传送门..本节我将详细介绍Monkey工具的使用. 一.Monkey测试简介 Monkey测试是Android平台自动化的一种手段,通过Monkey程序模拟 ...

  5. circular_buffer

    编程有时需要使用定长的容器(fixed size container).实现旋转容器可以像下面这样: std::vector<T> vec(size); vec[i % size] = n ...

  6. JSPatch常见问题解答

    原文地址:https://github.com/bang590/JSPatch/wiki/JSPatch%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E8%A7%A3%E7 ...

  7. inotify +rsync进行实时同步

    1.安装rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpmyum -y install ino ...

  8. Linux MySql install and use with c++

    1.安装mysql客户端 用命令: yum install -y mysql-server mysql mysql-devel 此命令包含了安装客户端和服务器 2.访问myslq 在命令行输入: my ...

  9. MyBatis入门学习教程-调用存储过程

    一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_incr ...

  10. MyBatis入门学习教程-使用MyBatis对表执行CRUD操作

    上一篇MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对use ...