.讲讲Python 普通整型和长整型的区别?
普通整型范围-231--231-1
长整型是普通整型的超集。
python2.3版本以后
普通整型和长整型已经统一了,普通用户已经看不到长整型的存在了。
2.5-2 运算符
(a) 写一个函数,计算并返回两个数的乘积
(b) 写一段代码调用这个函数,并显示它的结果
def count(a,b):
return a*b
num1=float(raw_input('number 1:'))
num2=float(raw_input('number 2:'))
c=count(num1,num2)
print c
5-5 取余。取一个任意小于1 美圆的金额,而后计算能够换成最少多少枚硬币。硬币有1
美分,5 美分,10 美分,25 美分四种。1 美圆等于100 美分。举例来讲,0.76 美圆换算结果
应该是 3 枚25 美分,1 枚1 美分。相似76 枚1 美分,2 枚25 美分+2 枚10 美分+1 枚5美分+1
枚1 美分这样的结果都是不符合要求的。
from random import randint
coincount=0
while True:
test=raw_input('inputjudgement:')
if test=='goon':
i=randint(1,100)
print i
b=divmod(i,25)
c=divmod(b
![[1] [1]](http://static.javashuo.com/static/loading.gif)
,10)
d=divmod(c
![[1] [1]](http://static.javashuo.com/static/loading.gif)
,5)
coincount=b[0]+c[0]+d[0]+d
print coincount
else:
print 'I want to quit.'
break
别人写的:
def NmuOfdollar(money):
num=[25,10,5,1]
count=0
for i in num:
result=divmod(money,i)
count=count+result[0]
money=result
return count
if __name__ == "__main__":
while True:
money = raw_input("please enter the money(0 toquit):")
if money == "0":
break
else:
print "thecount is: %d" % NmuOfdollar(int(money))
5-6 算术。写一个计算器程序 你的代码能够接受这样的表达式,两个操做数加一个运算符:
N1 运算符 N2. 其中 N1 和 N2 为整数或浮点数,运算符能够是+, -, *, /, %, **分别表示
加法,减法, 乘法, 整数除,取余和幂运算。计算这个表达式的结果,而后显示出来。提示:
能够使用字符串方法 split(),但不能够使用内建函数 eval_r().
def calexpress(express):
a=express.split('')
if a
![[1] [1]](http://static.javashuo.com/static/loading.gif)
=='+':
return float(a[0])+float(a
![[2] [2]](http://static.javashuo.com/static/loading.gif)
)
elif a
![[1] [1]](http://static.javashuo.com/static/loading.gif)
=='-':
return float(a[0])-float(a
![[2] [2]](http://static.javashuo.com/static/loading.gif)
)
elif a
![[1] [1]](http://static.javashuo.com/static/loading.gif)
=='*':
return float(a[0])*float(a
![[2] [2]](http://static.javashuo.com/static/loading.gif)
)
elif a
![[1] [1]](http://static.javashuo.com/static/loading.gif)
=='/':
return float(a[0])/float(a
![[2] [2]](http://static.javashuo.com/static/loading.gif)
)
elif a
![[1] [1]](http://static.javashuo.com/static/loading.gif)
=='%':
return float(a[0])%float(a
![[2] [2]](http://static.javashuo.com/static/loading.gif)
)
elif a
![[1] [1]](http://static.javashuo.com/static/loading.gif)
=='**':
return float(a[0])**float(a
![[2] [2]](http://static.javashuo.com/static/loading.gif)
)
if __name__=='__main__':
while True:
expresslist=raw_input('please inputexpress:')
if expresslist.lower()=='q':
print 'iwant to quit'
break
print '%s result is %f'%(expresslist,calexpress(expresslist))
若是使用内建函数的话:
expresslist=raw_input('pleaseinput express:')
printeval_r(expresslist)
5-11 取余。
(a) 使用循环和算术运算,求出 0-20 之间的全部偶数
(b) 同上,不过此次输出全部的奇数
(c) 综合 (a) 和 (b), 请问辨别奇数和偶数的最简单的方法是什么?
(d) 使用(c)的成果,写一个函数,检测一个整数可否被另外一个整数整除。 先要求用户输
入两个数,而后你的函数判断二者是否有整除关系,根据判断结果分别返回 True 和 False;
coding:
evennumber=[]
oddnumber=[]
for i in range(21):
if i%2==0:
evennumber.append(i)
else:
oddnumber.append(i)
print 'oddnumber is: %s'% oddnumber
print 'evennumber is: %s'% evennumber
d:
def count(num1,num2):
if num1%num2==0:
return True
else:
return False
num1=int(raw_input('number1:'))
num2=int(raw_input('number2:'))
A=count(num1,num2)
print A
5-13 转换。写一个函数把由小时和分钟表示的时间转换为只用分钟表示的时间。
code:
def Tran(settime):
list=settime.split(':')
newlist=int(list[0])*60+int(list
![[1] [1]](http://static.javashuo.com/static/loading.gif)
)
return newlist
if __name__=='__main__':
while True:
time=raw_input('please input time:')
if time.lower()=='q':
print'want to quit.'
break
print 'the time is %dminutes'%(Tran(time))
5-17 随机数。熟读随机数模块而后解下面的题:
生成一个有 N 个元素的由随机数 n 组成的列表, 其中 N 和 n 的取值范围分别为: (1<</div>
N <= 100), (0 <= n <= 231 -1)。而后再随机从这个列表中取 N (1 <=N <= 100)个随机数
出来, 对它们排序,而后显示这个子集。
code:
from random import randint
def count(N,n):
b=[]
for i in range(N):
b.append(randint(0,n))
return b
if __name__=='__main__':
N=randint(0,100)
print 'N:%d'%N
n=randint(0,231-1)
print 'n:%d'%n
c=count(N,n)
print sorted(c)