1.#输入a,b,c,d4个整数,计算a+b-c*d的结果
a=input("please input a nimber:") b=input("please input a number:") c=input("please input a number:") d=input("please input a number:") print a+b-c*d 输出结果: please input a nimber:3please input a number:3please input a number:3please input a number:3-3 2.#计算一个12.5m*16.7m的矩形房间的面积和周长 a=12.5 b=16.7 s=a*b c=a+a+b+b print "面积是%f,周长是%f"%(s,c) 输出结果是 面积是208.750000,周长是58.400000 3.#怎么得到9/2的小数结果 >>> float(9/2)4.0 4.#python计算中7*7*7*7.可以有多少种写法 第一种: >>> 7*7*7*72401第二种:>>>pow(7,4)2401第三种:>>>7**424401第四种:>>> eval("%s*%s*%s*%s"%(7,7,7,7))2401
第五种:
>>> for i in range(3):
... a*=7
...
>>> a
2401 第六种:
>>> "*7"*4
'*7*7*7*7'
>>> "*7"*4[1:]
Traceback (most recent call last)
File "<stdin>", line 1, in <mod
TypeError: 'int' object has no at
>>> ("*7"*4)[1:]
'7*7*7*7'
>>> eval(("*7"*4)[1:])
2401
第七种:
>>> a=['7']*4>>> "*".join(a)
'7*7*7*7'
>>> eval("*".join(['7']*4))
2401 5.#写程序将温度从华氏温度转换为摄氏温度。转换公式为C=5/9*(F-32) >>> def FtoC(degree):
... return float("%.2f" %(5.0*100/9*(degree-32)/100))
...
>>> print FtoC(100)
37.78 6.#一家商场在降价促销。如果购买金额50-100元(包含50元和100元)之间,会给10%的折扣,如果购买金额大于100元会给20%折扣。编写一程序,询问购买价格,再显示出折扣(%10或20%)和最终价格
#coding=utf-8
customer_price=float(raw_input("please input pay money:"))
if customer_price >=50 and customer_price<=100:
print "disconunt 10%% ,after discount you shoud pay %s" \
%(customer_price*(1-0.1))
elif customer_price >100:
print "disconunt 20%% ,after discount you shoud pay %s" \
%(customer_price*(1-0.2))
else:
print "disconunt 0%% ,after discount you shoud pay %s" \
%customer_price
>>> b=2
>>> a,b=b,a
>>> a,b
(2, 1) 10.#输入三个数字,以逗号隔开,输出其中最大的数 a=input("please input a list of three bumber:") print max(a) 输出结果: please input a list of three bumber:4,7,3711.#输入一个年份,输出是否为闰年#闰年条件:能被4整除但不能被100整除,或者能被400整除的年份都是闰年
#encoding=utf-8
a=input("please input a number as
time of year:")
if a%4==0 and a%400==0:
print "%d is a leap year!"%a
elif a%400==0:
print "%d is a leap year!"%a
else:
print "%d is not leap year!"%a
12.#一个足球队在寻找年龄在10岁到12岁的小女孩(包括10岁和12岁)加入。编写一个程序,询问用户的性别(m表示男性,f表示女性)和年龄,然后显示一条消息指出这个人是否可以加入球队,询问10次后,输出满足条件的总人数。
#encoding=utf-8
i=0
sum=0
while i<10:
a=raw_input("please tell me your gender:")
b=input("please tell me your age:")
if a=='f' and 10<=b<=12:
print "You can join in our basketball team!"
sum+=1
i+=1
print u"\n满足条件的人数为:%d人" %sum
13.#输入1-127的ASCII码并输出对应的字符while 1: a=input("please input the ASCII of 1-127:") print chr(a) 14.#3个人在餐厅吃饭,想分摊饭费。总共花费35.27美元,他们还想给15%的消费。每个人该怎么付钱 print (35.27+0.15*35.27)/3 输出结果: 13.5201666667>>>15.#一周有几天,一天有几个小时,一小时有几分钟,一分钟有多少秒#encoding=utf-8a=int(raw_input('一周有几天:'))b=int(raw_input('一天有几小时:'))c=int(raw_input('一小时有几分钟:'))d=int(raw_input('一分钟有几秒:')) print '*'*20print '一周有%d分钟'%(a*b*c)print '一周有%d秒'%(a*b*c*d)15、#猜数:猜数小练习:
生成随机整数,从1-5取出来
然后输入一个数字,来猜,如果大于,则打印bigger
小了,则打印less
如果相等,则打印equal
#coding=utf-8
import random
a=random.randint(1,5)
print "a is %d"%a
b=input("please input a number:")
if b>a:
print"bigger!"
elif b<a:
print"less!"
elif b==a:
print "equal!"
16、打印10到1的数字:
17.写一个列表,倒序输出,用while循环实现
18.在15题的基础上,补充只允许输入3次,猜错了就退出
#coding=utf-8import random
target_number=random.randint(1,5)
print target_number
guess_times=3
while guess_times>0:
guess_number=int(raw_input("please input your guess number:"))
if guess_number>target_number:
print "bigger"
elif guess_number<target_number:
print "less"
else:
print "you got it"
break
guess_times-=1
19.在18题的基础上,补充猜不中的情况下一直循环,直到猜中为止,计算猜的次数
#coding=utf-8import random
target_number=random.randint(1,5)
print target_number
guess_times=3
while guess_times>0:
guess_number=int(raw_input("please input your guess number:"))
if guess_number>target_number:
print "bigger"
elif guess_number<target_number:
print "less"
else:
print "you got it"
break
guess_times-=1 20、什么是素数 素数:只能被1和它本身整除,这样的数称之为素数 >>> number=raw_input("input a number:")
input a number:13
>>> number=int(number)
>>> number
13
>>> for i in range(2,number):
... if number%i==0:
... print "not a prime"
... break
... else:
... print "%s is a prime" %number
...
13 is a prime 21.#阶乘 >>> def fact(n):
... result=1
... for i in range(1,n+1):
... result*=i
... return result
...
>>> print fact(4)#递归 #coding=utf-8
def fact(n):
#第一个条件:写出结束递归的分支
#第二个条件:写出自己调用自己的分支
if n<=1:
return 1
else:
return n*fact(n-1)
print fact(5)练习:
# coding=utf-8
def f(n):
for i in n:
if isinstance(i,(tuple,dict,list)):
f(i),#,表示不打回车
else:
print i,#,表示不打回车
a = [1,2,(3,4,'sdsf',[5,6,'greg','kohi',
(7,8,9)]),'greg','pprgr']
f(a)
输出结果
1 2 3 4 sdsf 5 6 greg kohi 7 8 9 greg pprgr
22.#嵌套循环输出10-50中个位带有1-5的所有数字
方法一:
>>> for i in range(1,5):... for j in range(1,6):
... print str(i)+str(j)方法二: >> for i in range(10,50):
.. if str(i)[1] in ["1","2","3","4","5"]:
.. print i
23、输入1-127的ascii码并输出对应字符
>>> for i in range(1,128):
... print chr(i)
输出结果:
24、计算一周有多少分钟、多少秒钟
>>> days_in_a_week=7
>>> hours_in_a_day=24
>>> mins_in_a_hour=60
>>> secs_in_a_minute=60
>>> print days_in_a_week*hours_in_a_day* mins_in_a_hour*secs_in_a_minute
604800
25、3个人在餐厅吃饭,想分摊饭费。总共花费35.27美元,他们还想给15%的消费。每个人该怎么付钱
>>> print "pay %.2f per person" %(35.27*(1+0.15)*100/3/100) #.2f意思是保留两个小数pay 13.52 per person26.现在有面包、热狗、番茄酱、芥末、洋葱,预定是1,不订是0,其中面包必订, #coding=utf-8
for bread in ["1"]:
for hotdog in ["1","0"]:
for jam in ["1","0"]:
for jiemo in ["1","0"]:
for onion in ["1","0"]:
print bread+hotdog+jam+jiemo+onion26、
#coding=utf-8
cal_in_bread=1
cal_in_hotdog=1
cal_in_jam=1
cal_in_jiemo=1
cal_in_onion=1
for bread in ["1"]:
for hotdog in ["1","0"]:
for jam in ["1","0"]:
for jiemo in ["1","0"]:
for onion in ["1","0"]:
print "*"*50
print bread+hotdog+jam+jiemo+onion
print int(bread)*cal_in_bread+int(hotdog)*cal_in_hotdog+int(jam)*cal_in_jam+int(jiemo)*cal_in_jiemo+int(onion)*cal_in_onion
27、输入3个数,以逗号隔开,输出其中最大的数
>>> a=1>>> b=2
>>> c=3
>>> if a>b:
... max=a
... else:
... max=b
...
>>> if max>c:
... print max
... else:
... print c
28、求两个正整数m和n的最大公约数: def hcf(x, y):
# 获取最小值
if x > y:
smaller=y
biger=x
else:
smaller=x
biger=y
if biger%smaller==0:
hcf=smaller
else:
for i in range(1,(smaller + 1)/2):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
# 用户输入两个数字
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
print num1,"和", num2,"的最大公约数为", hcf(num1, num2)