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. 最近这两天看了关于H5游戏开发的一个教程,实践很短暂,看了很多理论的东西,现在呢也只是想回忆回忆关于EUI的部分知识吧

    首先我了解了什么是Egret: Egret中文就是白鹭的意思,Egret是一套H5游戏开发的软件.(纯粹属于个人理解) 其次我对以下几款软件的相关知识做了些了解: Egret Engine(引擎),E ...

  2. iPhone应用开发 UITableView学习点滴详解

    iPhone应用开发 UITableView学习点滴详解是本文要介绍的内容,内容不多,主要是以代码实现UITableView的学习点滴,我们来看内容. -.建立 UITableView DataTab ...

  3. CSS样式--实际开发总结

    1.   div 嵌套,子div中内容超出范围可以设置: display:inline-block; overflow:auto 即可让子div中出现滚轴 2.   让div中内容垂直方向居中 设置: ...

  4. android之animation

    Android  Animation一共有四种 Alpha: 淡入淡出效果 Scale: 缩放效果 Rotate: 旋转效果 Translate:移动效果 使用Tweened Animations的步 ...

  5. VS2008编译bat

    工程文件为AirCode,批处理文件为bulit.bat(与*.sln文件在同级目录). 以下是批处理的代码: echo %~dp0 rem set build_config="Debug| ...

  6. 强制span不换行

    对于上一篇提到的overflow的问题我好像搞懂一些了.事情大概是这个样子的:如果用了float属性,那么元素就会脱离文本的束缚,无法无天起来,这肯定是猿类无法忍受的.要想让他们乖乖就范,要么用清除浮 ...

  7. 【转载】关于在vs2013中配置opengl红宝书第八版环境

     本文为转载 原文地址 http://blog.csdn.net/qq821869798/article/details/45247241 本人刚开始学习opengl,买了一本opengl红宝书第八版 ...

  8. java代码

    io的使用 package com.tan.io; import java.io.*; import java.util.*; class Employee{ private String name; ...

  9. OpenGL投影矩阵

    概述 透视投影 正交投影 概述 计算机显示器是一个2D平面.OpenGL渲染的3D场景必须以2D图像方式投影到计算机屏幕上.GL_PROJECTION矩阵用于该投影变换.首先,它将所有定点数据从观察坐 ...

  10. 如何搭建 node,react 开发环境

    项目相关内容:Sublime + Node + React --注意:在 windows 操作系统中,如果把 node 安装在系统盘(一般是C盘),会导致 node 没有操作文件的权限的问题,如无法新 ...