1、求出1 / 1 + 1 / 3 + 1 / 5……+1 / 99的和 (1分之一+1分之三+1分支5....)
2、用循环语句,计算2 - 10之间整数的循环相乘的值 (2*3*4*5....10)
3、用for循环打印九九乘法表
end=" \t" :表格制表横向制表符
end=‘\n’: 表示换行
方法1:
for y in range(1,10):
x=1
while x<=y:
z=x*y
print(f'{x}x{y}={z}',end=" ")
x+=1
print('')
方法2:
for i in range(1,10):
for a in range(1,i+1):
print(a,'*',i,'=',a*i,end=' ')
print('')
方法3:
for x in range(1,10):
for y in range(1,x+1):
print("%d*%d=%d" % (y,x,x*y),end=" ")
print("")
4、求每个字符串中字符出现的个数如:helloworld
方法一:子典中的键是唯一
a='helloworld'
c={}
for i in a:
c[i]=a.count(i)
print(c)
方法2: if not in
a='helloworld'
count={}
for i in a:
if i not in count:
count[i]=1
else:
count[i]+=1
print(count)
方法3:set
s='helloworld'
t=set(s)
for i in t:
print(i,s.count(i))
方法4:
str1='helloppoopo'
print([(i,str1.count(i)) for i in set(str1)])
5、实现把字符串str = "duoceshi"中任意字母变为大写(通过输入语句来实现)
方法1:
str='duoceshi'
c=input('输入字母')
if c in str:
a=c.upper()
print(str.replace(c,a))
方法2:
str="duoceshi"
a=input("请输入字母: ")
print(str.replace(a,a.upper()))
6、分别打印100以内的所有偶数和奇数并存入不同的列表当中
方法1:
print(list(range(2,101,2)))
print(list(range(1,101,2)))
方法2:
a=[]
b=[]
for x in range(1,100,2):
a.append(x)
print(a)
for y in range(0,100,2):
b.append(y)
print(b)
方法3:
b=[]
c=[]
for i in range (0,101):
if i%2!=0:
b.append(i)
else:
c.append(i)
print(b)
print(c)
7、请写一段Python代码实现删除一个list = [1, 3, 6, 9, 1, 8]# 里面的重复元素不能用set
方法1:
list = [1, 3, 6, 9, 1, 8]
for x in list:
y=list.count(x)
if y>1:
list.remove(x)
print(list)
方法2:
li1=[1,1,1,2,3,4,4,4,5,6,5,7]
d={}
for i in li1:
d[i]=0
print(d.keys())
方法3:
list = [1, 3, 6, 9, 1, 8]
a=[]
for i in list:
if i not in a:
a.append(i)
print(a)
方法四:
list = [1, 3, 6, 9, 1, 8]
a=set(list)
print(a)
8、将字符串类似:"k:1|k3:2|k2:9" 处理成key:value或json格式,比如{"k": "1", "k3": "2"}
方法1:
a="k:1|k3:2|k2:9"
print(dict([tuple(i.split(':')) for i in a.split("|")]))
方法2:
dir={}
a="k:1|k3:2|k2:9"
b=a.split('|')
print(b) #['k:1', 'k3:2', 'k2:9']
for i in b:
c=i.split(':')
dir[c[0]]=c[1]
print(c)
方法3:
s="k:1|k3:2|k2:9"
s=s.split('|')
a={}
for i in s:
b=i.split(':')
a.setdefault(b[0],b[1])
print(a)
9、把字符方串user_controller转换为驼峰命名UserController大驼峰在java用作变量命名
(前英文为大写后英文为小写) 小驼峰:作为变量命名
方法1:
a='user_controller'
b=a.split('_')
print(b)
print(b[0].capitalize()+b[1].capitalize())
方法2:
a='user_controller'
b=a.title()
print(b.replace('_',''))
方法3:
a='user_controller'
b=a.title().split("_")
print(b)
for i in b :
print(i,end="")
10、给一组无规律的数据从大到小或从小到大进行排序如:list = [2, 6, 9, 10, 18, 15, 1]
方法1:
a=[2,6,10,18,15,1]
a.sort()
print(a)
方法2:
a=[2,6,10,18,15,1]
a.sort()
print(list)
print(sorted(a,reverse=True))
print(sorted(a,reverse=False))
方法3:冒泡排序
l1 = [2, 6, 9, 10, 18, 15, 1]
print(len(l1))
for i in range(len(l1)):
for j in range(len(l1)-1):
if l1[j]>l1[j+1]:
l1[j] ,l1[j + 1]=l1[j+1] ,l1[j]
print(l1)
11、分析以下数字的规律, 1 1 2 3 5 8 13 21 34用Python语言编程实现输出(斐波那契数列)
方法1:
list=[1,1]
for i in range(2,9):
list.append(list[i-1]+list[i-2])
print(list)
方法2:
a = []
def fun(n):
if n == 1:
return 1
if n == 2:
return 1
else:
return fun(n - 1) + fun(n - 2)
for i in range(1, 11):
a.append(fun(i))
print(a)
方法3:
a=0
b=1
c=[1]
while a<50:
d=a+b
c.append(d)
a=b
b=d
print(c)
方法四:
a=0
b=1
while b<=100:
print(b,end= " ")
a,b=b,a+b
#分析题目:根据规律 1+1=2 2+1=3 2+3=5 3+5=8....
#此为斐波那契数列 (考试题非常多次题目)
12、如有两个list:
a =['a','b','c','d','e']
b =[1,2,3,4,5] 将a中的元素作为key b中的元素作为value,将a,b合并为字典
方法1:
a =['a','b','c','d','e']
b =[1,2,3,4,5]
c={}
for i in range(len(a)):
c.update({a[i]:b[i]})
print(c)
方法2:
a =['a','b','c','d','e']
b =[1,2,3,4,5]
print(dict(zip(a,b)))
13、有如下列表,统计列表中的字符串出现的次数
# a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']
方法1:
a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']
c={}
for i in a:
c[i]=a.count(i)
print(c)
方法2:
a = ['apple', 'banana', 'apple', 'tomao', 'orange', 'apple', 'banana', 'watermeton']
count={}
for i in a:
if i not in count:
count[i]=1
else:
count[i]+=1
print(count)
方法3:
a = ['apple', 'banana', 'apple', 'tomao', 'orange', 'apple', 'banana', 'watermeton']
for i in set(a):
c=a.count(i)
print(i,c)
14、、列表推导式求出列表所有奇数并构造新列表 a =[1,2,3,4,5,6,7,8,9,10]
方法1:
a=[1,2,3,4,5,6,7,8,9,10]
b=[i for i in a if i%2==1]
print(b)
方法2:
c=[]
a=[1,2,3,4,5,6,7,8,9,10]
for i in a:
if i%2==1:
c.append(i)
print(c)
15、有如下url地址, 要求实现截取出"?"号后面的参数, 并将参数以"key value"的键值形式保存起来, 并最终通过#get(key)的方式取出对应的value值。
#url地址如下:http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0"
方法1:
a='http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0'
b=a.split('?')
print(b)
c=b[1]
print(c) #page_size=20&page_index=1&user_id=203317&trade_type=0
d=c.split('&')
print(d)
zd={}
for i in d:
k,v=i.split('=')
zd[k]=v
print(zd)
print(zd.get('page_index'))
===================================================