码迷,mamicode.com
首页 > 编程语言 > 详细

python习题练习(chapater 5 -- python核心编程)

时间:2015-08-26 00:05:31      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:python;python习题

更新中。。。

 

#!/usr/bin/env python
# coding: utf-8
‘for practise in chapater five‘


#定义一个函数,计算并返回两个数的乘机
def product(a, b):
 return(a * b)

#根据分数输出同学的评分成绩(A-F)
def score(i):
 if (i > 90) & (i < 100):
  return(‘A‘)
 elif (i > 80) & (i < 89):
  return(‘B‘)
 elif (i > 70) & (i < 79):
  return(‘C‘)
 elif (i > 60) & (i < 69):
  return(‘D‘)
 else:
  return(‘F‘)
#print(score(88))
  
#-----练习取余-----  

#1.判断年份是否是闰年
def estimate_leap(j):  #j 就是年份
 if ((j%4 == 0) and (j%100 != 0)) or (j%400 == 0):
  print("%d is a leap year." %j)
 else:
  print("%d is a not leap year." %j)
 return
#print(estimate_leap(1997))

#2.取一个任意小于1美元的金额,然后计算可以换成最少多少枚硬币。硬币有1,5,10,25美分
def coin(k):   #k 就是小于1美元的金额
 coin25 = int(k/0.25)
 coin10 = int((k-coin25*0.25)/0.1)
 coin5 = int((k-coin25*0.25-coin10*0.1)/0.05)
 coin1 = int((k-coin25*0.25-coin10*0.1)*100)
 return("%.2f cent can change to at least %d coins." %(k, (coin25+coin10+coin5+coin1)))
#print(coin(0.87))

#3.1 使用循环和算术运算,求出0-20之间的所有偶数
def even_num():  #even number
 for i in range(21):
  if i % 2 == 0:
   print (i),
#even_num()

#3.2 使用循环和算术运算,求出0-20之间的所有奇数
def odd_num():  #odd number(uneven number)
 for i in range(21):
  if i % 2 == 1:
   print (i),
#odd_num()
  
#3.3 写一个函数,检测一个整数能否被另一个整数整除。先要求用户输入两个数,然后你的函数判断
#     两者是否有整除关系,根据判断结果分别返回 True 和 False
def exact_divi(a, b):  #检测整数a能否被整数b整除, b/a
 if b % a == 0:
  print (‘%d can be divisible by %d‘ %(a, b))
  return True
 else:
  print (‘%d can\‘t be divisible by %d‘ %(a, b))
  return False
#a = int(raw_input(‘First number> ‘))
#b = int(raw_input(‘Second number> ‘))
#exact_divi(a, b)


  
#-----算术-----
#1.一个计算器程序,两个操作数加一个运算符:N1 运算符 N2.(未完成)
def calculate(l):  #l 是表达式:N1 运算符 N2
 num1 = float(l.split(‘ ‘)[0])
 num2 = float(l.split(‘ ‘)[2])
 opert = l.split(‘ ‘)[1]
# return (num1 opert num2)
#print(calculate(‘11.2 - 2.9‘)) #这个题目还没解决

#2.转换,写一对函数来进行华氏度到摄氏度的转换。转换公式为C = (F - 32) * (5 / 9)
def centi_fahren(a):  #参数a为华氏度,该函数的返回值为摄氏度。
 return(‘%.2f‘ %((a - 32) * (float(5)/9)))

#print(centi_fahren(100))

#3.系统限制。写一段脚本确认一下你的Python 所能处理的整数,长整数,浮点数和复数的范围。
import sys
def num_limit():
 print(‘The limit for int: %d %d‘ %(-sys.maxint-1, sys.maxint))
 print(‘The limit for float: %e %e‘ %(sys.float_info[3], sys.float_info[0]))
 print(‘The limit for long: %s‘ %(sys.long_info) )
#num_limit()

#4.转换。写一个函数把由小时和分钟表示的时间转换为只用分钟表示的时间。
def minutes(str):
 hours = int(str.split(‘:‘)[0])
 minutes = int(str.split(‘:‘)[1])
 minutes_all = hours * 60 + minutes
 return minutes_all
#print(minutes(‘12:50‘))

#5.银行利息。写一个函数,以定期存款利率为参数, 假定该账户每日计算复利,请计算并返回年回报率。
#工商银行定期存日款率0.0035%
def interest_year_return(capital, rate):  #capital 是本金,rate 是日利率
 capital_ori = capital
 i = 1
 while True:
  capital = capital * (1+rate)  # capital = capital * ((1+rate)**365)
  i += 1
  if i == 366:
   break
 return((capital - capital_ori)/capital_ori)
#print(interest_year_return(10000, 0.000035))

#6.最大公约数和最小公倍数。请计算两个整数的最大公约数和最小公倍数。
def max_com_divisor(a, b):  #整数a和b的最大公约数,使用辗转相除法(algorithm of division)
 if a / b == 0:
  a, b = b, a
  algorithm_lst = [a, b]
  i = 0
  while True:
   algorithm_lst.append(algorithm_lst[i] % algorithm_lst[i+1])
   if algorithm_lst[i+2] == 0:
    return algorithm_lst[i+1]
    break
   else:
    i += 1
#print max_com_divisor(66, 242)
def least_com_multiple(a, b): #整数a和b的最小公倍数,同样使用辗转相除法(algorithm of division)
 if a / b == 0:
  a, b = b, a
  algorithm_lst = [a, b]
  i = 0
  while True:
   algorithm_lst.append(algorithm_lst[i] % algorithm_lst[i+1])
   if algorithm_lst[i+2] == 0:
    break
   else:
    i += 1
 lcm = (a * b) / algorithm_lst[i+1]
 return(lcm)
#print least_com_multiple(12, 33)

本文出自 “炫酷运维” 博客,请务必保留此出处http://19901007.blog.51cto.com/10641668/1688208

python习题练习(chapater 5 -- python核心编程)

标签:python;python习题

原文地址:http://19901007.blog.51cto.com/10641668/1688208

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!