1、输入字符串,判断是否为回文(例:abcdcba即为回文)
str = raw_input("请输入一个字符串:")
step = len(str)/2
for i in range(0,step):
if str[i] != str[-(i+1)]:
print "不是回文"
break
else:
print "是回文"
2、有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
fenzi = 2
fenmu = 1
sum = 0
for i in range(1,21):
fenzi,fenmu = fenzi+fenmu,fenzi #分子和分母同时赋值,python语言特性,无需中间变量
sum = sum + float(fenzi)/fenmu
print sum
3、有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
count = 0
for m in range(1,5):
for n in range(1,5):
for k in range(1,5):
if m!=n and m!=k and n!=k:
print str(m)+str(n)+str(k)
count += 1
print count
4、下划线风格转驼峰风格 (下划线风格:abc_def_ghi,驼峰风格:AbcDefGhi)
data = raw_input("请输入字符串:")
data = data.split('_')
word = []
for d in data:
word.append(d.capitalize())
print ''.join(word)
5、写一个脚本解析url
http://localhost:8080/test/data?abc=def&test=debug
6、列表元素去重
c = [1, 2, 3, 4, 5, 6, 3, 7, 8, 9, 5, 8]
r = []
for d in c:
if d not in r:
r.append(d)
print r
7、用字典表示学生与数学,语文,英语成绩, 并计算平均分
dict = {
"chinese":90,
"math":78,
"english":88,
}
print sum(dict.values())/len(dict)
8、打开文件,统计英文单词出现的次数
file = open("this.txt")
lines = file.readlines()
file.close()
result = {}
for line in lines:
data = line.strip(".\n").split(" ") #.strip()去空格和换行
for d in data:
word = d.lower()
if not word in result:
result.setdefault(word,1)
else:
result[word] += 1
print result["is"] #输出单词is的出现次数
9、实现排序函数
data = [2, 4, 7, 9, 1, 6, 3, 5, 8]
def sort(data):
for _ in range(len(data)):
for d in range(0,len(data)-1):
if data[d]>data[d+1]:
data[d],data[d+1] = data[d+1],data[d]
return data
print sort(data)
10、利用递归方法求n!
def jiec(x):
if x==1:
return 1
return x * jiec(x-1)
print jiec(5)
11、输入某年某月某日,判断这一天是这一年的第几天
year = int(raw_input("请输入年份:"))
month = int(raw_input("请输入月份:"))
day = int(raw_input("请输入几号:"))
list = [31,28,31,30,31,30,31,31,30,31,30,31]
if (year%400==0) or (year%4==0 and year%100!=0) :
list[1] = 29
print sum(list[0:month-1]) + day
else:
print sum(list[0:month-1]) + day
12、输出9*9口诀表
from __future__ import print_function #python2中无print函数无end属性,需导包
for i in range(1,10):
for j in range(1,i+1):
print ("%d×%d=%d" % (j,i,i*j),end=" ")
print () #换行作用