1 输入1-127的ascii码并输出对应字符
方法一:
>>> for i in range(1,128):
... print(chr(i),ord(chr(i)))
方法二:
[chr(i) for i in range(1,128)]
[ord(chr(i)) for i in range(1,128)]
方法三:
list(map(lambda x:chr(x),range(1,128)))
list(map(lambda x:ord(chr(x)),range(1,128)))
方法四:
[eval("chr({0})".format(i)) for i in range(1,128)]
方法五:
[chr(i) for i in {}.fromkeys(range(1,128)).keys()]
2. 输入a,b,c,d4个整数,计算a+b-c*d的结果
>>> a=1
>>> b=2
>>> c=3
>>> d=4
>>> print(a+b-c*d)
-9
>>> print(eval("a+b-c*d"))
-9
3.计算一周有多少分钟、多少秒钟
>>> print("一周有%s 分钟" %str(1*7*24*60))
一周有10080 分钟
>>> print("一周有%s 秒钟" %str(1*7*24*60*60))
一周有604800 秒钟
4.3个人在餐厅吃饭,想分摊饭费。
总共花费35.27美元,他
们还想给15%的小费。每个人该怎么付钱,
编程实现
>>> print("每人付费%s$" %str(round(35.27*1.15/3,2)))
每人付费13.52$
5.计算一个12.5m X 16.7m的矩形房间的面积和周长
>>> print("面积%s平方米" %str(12.5*16.7))
面积208.75平方米
>>> print("周长%s米" %str((12.5+16.7)/2))
周长14.6米
6 怎么得到9 / 2的小数结果
>>> print(9/2)
4.5
7.python计算中7 * 7 *7 * 7,可以有多少种写法
>>> print(7*7*7*7) #方法一
2401
>>> print(7**4) #方法二
2401
>>> import math #方法三
>>> math.pow(7,4)
2401.0
>>> print(eval("7*7*7*7")) #方法四
2401
>>> result=1 #方法五
>>> for i in range(1,5):
... result*=7
...
>>> print(result)
2401
>>> import operator #方法六
>>> operator.pow(7,4)
2401
8.写程序将温度从华氏温度转换为摄氏温度。
转换公式为C = 5 / 9*(F - 32)
>>> f=30
>>> print("摄氏温度:%s" %str(round(5/9.0*(f-32),1)))
摄氏温度:-1.1
9.一家商场在降价促销。如果购买金额50-100元(包含50元和100元)之间,会给10%的折扣,如果购买金额大于100元会给20%折扣。编写一程序,询问购买价格,再显示出折扣(10%或20%)和最终价格。
while True:
Amount = float(raw_input('Please enter the amount of purchase:\n'))
print Amount
if (Amount >= 50 and Amount <= 100):
print ('You can enjoy a discount is 10% of the amount: %s',(Amount*0.9))
continue
elif Amount > 100:
print ('You can enjoy a discount is 20% of the amount %s:',(Amount * 0.8))
continue
else:
print ("You don't have a discount price ")
break
10. 判断一个数n能否同时被3和5整除
>>> n = 100
>>> if n%3==0 and n%5==0:
... print("this number can be divided exactly by 3 and 5")
... else:
... print("this number can not be divided exactly by 3 and 5")
...
this number can not be divided exactly by 3 and 5
11.求1 + 2 + 3 +….+100
>>> sum = 0
>>> for i in range(1,101):
... sum+=i
...
>>> print(sum)
5050
12.交换两个变量的值
方法一
>>> a=1
>>> b=2
>>>
>>> a,b = b,a
>>> print (a,b)
2 1
>>>
方法二
>>> a=1
>>> b=2
>>> tmp = a
>>> a=b
>>> b=tmp
>>>
>>> print (a,b)
2 1
面试题:不用第三个变量,对两个变量进行交换
a=1
b=2
a=a+b //a=3
b=a-b //b=1
a=a-b //a=2
13一个足球队在寻找年龄在10到12岁的小女孩(包括10岁和12岁)加入。编写一个程序,询问用户的性别(m表示男性,f表示女性)和年龄,然后显示一条消息指出这个人是否可以加入球队,询问10次后,输出满足条件的总人数。
#encoding = utf-8
count = 0
sex = ""
for i in range(10):
student_info = input("please input age and sex,sep by ',':")
age = int(student_info.split(',')[0])
sex = student_info.split(',')[1]
if age>=10 and age<=12 and sex.lower() == "f":
print("The student can join in the football team!")
count+=1
print("满足条件的孩子有%s个" %count)
14 长途旅行中刚到一个加油站,距下一个加油站还有200km,而且以后每个加油站之间距离都是200km。编写一个程序确定是不是需要在这里加油,还是可以等到接下来的第几个加油站再加油。
程序询问以下几个问题:
1)你车的油箱多大,单位升
2)目前油箱还剩多少油,按百分比算,比如一半就是0.5
3)你车每升油可以走多远(km)
提示:
油箱中包含5升的缓冲油,以防油表不准。
car_gas_volume = 50
car_current_gas_volume = 30
car_100_km_gas_consume = 0.8
gas_station_gap_distance = 200
print("请在第%s个加油站加油" %int(((car_current_gas_volume-5)*
(100/car_100_km_gas_consume))/gas_station_gap_distance))
#(30-5)/0.8*100/200
15 现有面包、热狗、番茄酱、芥末酱以及洋葱,数字显示有多少种订购组合
,其中面包必订,0不订,1订,比如10000,表示只订购面包
bread=["b1"]
hotdog=["h1","h0"]
tomato_jam=["t1","t0"]
jiemo=["j1","j0"]
onion=["o1","o0"]
result =[]
for b in bread:
for h in hotdog:
for t in tomato_jam:
for j in jiemo:
for o in onion:
result.append(b+" "+h+" "+t+" "+j+" "+o)
print (len(result))
print (result)
for i in result:
print(i)
16 基于上题:
给出每种食物的卡路里(自定义),再计算出每种组合总共的卡路里
bread=["b1"]
hotdog=["h1","h0"]
tomato_jam=["t1","t0"]
jiemo=["j1","j0"]
onion=["o1","o0"]
result =[]
bread_calori=1
hotdog_calori=2
tomato_jam_calori=3
jiemo_calori=4
onion_calori=5
for b in bread:
for h in hotdog:
for t in tomato_jam:
for j in jiemo:
for o in onion:
result.append(b+" "+h+" "+t+" "+j+" "+o)
print (len(result))
print (result)
for i in result:
calori_count = 0
for x in i.split():
if x == "b1":
calori_count+=bread_calori
if x == "h1":
calori_count+=hotdog_calori
if x == "t1":
calori_count+=tomato_jam_calori
if x == "j1":
calori_count+=jiemo_calori
if x == "o1":
calori_count+=onion_calori
print(i,"组合的卡路里总和是:",calori_count,u"千卡")
17输入5个名字,排序后输出
name_list = []
for i in range(5):
name_list.append(input("input a name:").strip())
name_list.sort()
print(name_list)
18实现一个简单的单词本
功能:
可以添加单词和词义,当所添加的单词已存在,让用户知道;
可以查找单词,当查找的单词不存在时,让用户知道;
可以删除单词,当删除的单词不存在时,让用户知道;
以上功能可以无限制操作,直到用户输入bye退出程序。
word_dict = {}
print("""
1:add a word
2:find a word meaning
3:delete a word
4:input bye to exit
""")
while 1:
command = input("please input your command:")
if command ==str(1):
word = input("please input your word:")
word_meaning = input("please input your word meaning:")
if word in word_dict.keys():
print("the word is already exist")
word_dict[word] =word_meaning
continue
if command ==str(2):
word = input("please input your word to find:")
if word in word_dict.keys():
print (word_dict[word])
continue
print ("the word is not found!")
if command == str(3):
word = input("please input your word to delete:")
if word in word_dict.keys():
del word_dict[word]
print ("delete is done!")
continue
print ("word to delete is not found!")
if command == "bye":
break
19输入一个正整数,输出其阶乘结果
>>> result = 1
>>> for i in range(1,11):
... result*=i
...
>>> print(result)
3628800
20 输入3个数字,以逗号隔开,输出其中最大的数
num_list = input("please input three numbers,sep by ',':")
num_list = num_list.split(',')
a = int(num_list[0])
b = int(num_list[1])
c = int(num_list[2])
max_num = a
for i in a,b,c:
if i > max_num:
max_num = i
print(max_num)
21输入一个年份,输出是否为闰年
是闰年的条件:
能被4整数但不能被100整除,或者能被400整除的年份都是闰年。
year = int(input("please input a year to judge a leap year:"))
if (year % 4 ==0 and year % 100 !=0) or year % 400 == 0:
print("%s is a leap year!" %year)
else:
print("%s is not a leap year!" %year)
22求两个正整数m和n的最大公约数
m = 20
n = 10
if m < n:
y = m
else:
y = n
result = 0
for i in range(1,y+1):
if m % i == 0 and n % i ==0:
result = i
print("%s和%s的最大公约数是:%s" %(m,n,result))