函数的小练习题

# 1、求出1 / 1 + 1 / 3 + 1 / 5……+1 / 99的和 (1分之一+1分之三+1分支5....)
# sum=0  #一开始的和为零
# for i in range(1,100): #开始遍历,范围1到99
#     if i%2 !=0:  #判断条件,
#         sum=sum+(1/i) #每次遍历加一个数,i是变量第一次遍历加1,第二次加3
#         print(1/i)
# print(sum) #2.937774848474907

# a=1
# sum=0
# while a<=99:
#     sum=sum+1/a
#     a=a+2
# print(sum) #2.937774848474907

# total_sum=0
# for i in range(1,100,2):
#     total_sum+=1/i
# print(total_sum) #2.937774848474907

# i=1
# sum=0
# for i in range(1,51):
#     sum=sum+1/(2*i-1)
# print(sum) #2.937774848474907

# 2、用循环语句,计算2 - 10之间整数的循环相乘的值 (2*3*4*5....10)
# a=2
# sum=1
# while a<=10:
#     sum=sum*a
#     a=a+1
#     print(a)
# print(sum) #3628800

# a=1
# for i in range(1,10):
#     a=a*(i+1)
# print(a)

# i=2
# x=1
# while i<=10:
#     x=x*i
#     i+=1
# print(x)

# 3、用for循环打印九九乘法表
# for a in range(1,10):
#     for b in range(1,a+1):
#        print(a,"*",b,"=",b*a,end=' ,')
# print(" ")

# for a in range(1,10):
#     for b in range(1,a+1):
#         print(b,"*",a,"=",b*a,end='  , ')
#     print('')

# 4、求每个字符串中字符出现的个数如:helloworld
# a=input("输入字符:")
# b=set(a)
# for c in b:
#     print(c,"字符个数:",a.count(c),sep="")

# def hs1(s):
#     dict={}
#     for i in s:
#         if i in dict:
#           dict[i]+=1
#         else:
#           dict[i]=1
#     return dict
# s="helloword"
# print(hs1(s))  #{'h': 1, 'e': 1, 'l': 2, 'o': 2, 'w': 1, 'r': 1, 'd': 1}

# 5、实现把字符串str = "duoceshi"中任意字母变为大写(通过输入语句来实现)
# str="duoceshi"
# c=input("输入变大写的字母")
# for a in str:
#     if a==c.lower():
#         print(a.upper(),end=" ")
#     else:
#         print(a,end=" ") #d u o C e s h i

# str="duoceshi"
# a=input("请输入要转换为大写的字母")
# if a in str:
#     index=str.index(a)
#     new_str=str[:index]+a.upper()+str[index+1:]
#     print(new_str)
# else:
#     print("字符未找到,无法转换为大写")

# 6、分别打印100以内的所有偶数和奇数并存入不同的列表当中
# list1=[]
# list2=[]
# for i in range(1,101):
#     if i%2==0:
#         list2.append(i)
#     else:
#         list1.append(i)
# print(list1)
# print(list2)

# print(list(range(0,101,2)))
# print(list(range(1,101,2)))

# list1=[]
# list2=[]
# for a in range(1,101,2):
#     list1.append(a)
# for b in range(0,101,2):
#     list2.append(b)
# print(list2)
# print(list1)

# list1=[]
# list2=[]
# i=1
# j=2
# for i in range(1,51):
#     i=2*i-1
#     list1.append(i)
# for j in range(1,51):
#     j=2*j
#     list2.append(j)
# print("100以内的奇数:",list1)
# print("100以内的偶数:",list2)

# 7、请写一段Python代码实现删除一个list = [1, 3, 6, 9, 1, 8] 去重
# list=[1,3,6,9,1,8]
# s=set(list)
# print(s) #{1, 3, 6, 8, 9}

# list1=[1,3,6,9,1,8]
# d={}
# c=d.fromkeys(list1)
# print(list(c))

# list=[1,3,6,9,1,8]
# list1=[]
# for i in list:
#     if i not in list1:
#         list1.append(i)
# print(list1)

# list=[1,3,6,9,1,8]
# for i in list:
#     if list.count(i)>1:
#         list.remove(i)
# print(list)


# 8、将字符串类似:"k:1|k3:2|k2:9" 处理成key:value或json格式,比如{"k": "1", "k3": "2"}
# (前英文为大写后英文为小写) 小驼峰:作为变量命名
# str1=("k:1|k3:2|k2:9")
# dict1={s.split(":")[0]:s.split(":")[1] for s in str1.split("|")}
# print(dict1) #{'k': '1', 'k3': '2', 'k2': '9'}

# dict={}
# str="k:1|k3:2|k2:9"
# a=str.split("|") #['k:1', 'k3:2', 'k2:9']
# print(a)
# for i in a:
#     print(i)
#     dict[i.split(":")[0]] = i.split(":")[1]
# print(dict)

# dict={}
# str="k:1|k3:2|k2:9"
# a=str.split("|") #['k:1', 'k3:2', 'k2:9']
# print(a)
# for i in a:
#     print(i)
#     k,v=i.split(":")
#     dict[k]=v
# print(dict)

# 9、把字符串user_controller转换为驼峰命名UserController大驼峰在java用作变量命名
# str="user_controller"
# list1=str.split('_')
# print(list1) #['user', 'controller']
# s1=list1[0]
# s2=list1[1]
# print(s1.capitalize()+s2.capitalize()) #UserController

# s=("user_controller")
# result=''.join([word.title()for word in s.split("_")])
# print(result)

# 10、给一组无规律的数据从大到小或从小到大进行排序如:list = [2, 6, 9, 10, 18, 15, 1]
# list1=[2, 6, 9, 10, 18, 15, 1]
# list1.sort()
# print(list1) #[1, 2, 6, 9, 10, 15, 18]

# list=[2, 6, 9, 10, 18, 15, 1]
# print(sorted(list,reverse=True)) #[18, 15, 10, 9, 6, 2, 1]
# print(sorted(list,reverse=False)) #[1, 2, 6, 9, 10, 15, 18]

# list = [2, 6, 9, 10, 18, 15, 1]
# for i in range(0,len(list)-1):
#     for j in range(0,len(list)-1):
#         if list[j]>list[j+1]:
#             list[j],list[j+1]=list[j+1],list[j]
# print(list)

# 11、分析以下数字的规律, 1 1 2 3 5 8 13 21 34用Python语言编程实现输出
# #分析题目:根据规律 1+1=2 2+1=3 2+3=5 3+5=8....
# #此为斐波那契数列 (考试题非常多次题目)
# list=[1,1]
# def fei(n):
#     for i in range(0,n-2):
#         list.append((list[-1])+(list[-2]))
#         #print(fei(10))
# fei(10)
# print(list) #[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

# a,b=1,1
# print(a,end=",")
# print(b,end=",")
# for  i in range(10):
#     a,b=b,a+b
#     print(b,end=",") #1,1,2,3,5,8,13,21,34,55,89,144,

# list = []
# def  fei(n):
#     for  i  in range(0,n-2):
#         if i==0 or  i==1:
#             list.append(i)
#         else:
#             list.append((list [-1]) + (list [-2]))
# fei(22)
# print(list)

# 12、如有两个list:a =['a','b','c','d','e']
# b =[1,2,3,4,5] 将a中的元素作为key b中的元素作为value,将a,b合并为字典
# a =['a','b','c','d','e']
# b = [1, 2, 3, 4, 5]
# s=zip(a,b)
# print(dict(s)) #{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

# a =['a','b','c','d','e']
# b =[1,2,3,4,5]
# s={}
# for x in a:
#     for y in b:
#         i=a.index(x)
#         j=b.index(y)
#         if i==j:
#             s[x]=y
# print(s)


# 13、有如下列表,统计列表中的字符串出现的次数
# # a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']

# a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']
# b=set(a) #去重
# for i in b:  #遍历
#     print(i,"出现的次数",a.count(i))  #打印变量和变量出现的次数

# def hs(a):
#     dict={}
#     for i in a:
#         if i in dict:
#             dict[i]+=1
#         else:
#             dict[i]=1
#     return dict
# a=['apple','banana','apple','tomao','orange','apple','banana','watermeton']
# print(hs(a)) #{'apple': 3, 'banana': 2, 'tomao': 1, 'orange': 1, 'watermeton': 1}



# 14、、列表推导式求出列表所有奇数并构造新列表 a =[1,2,3,4,5,6,7,8,9,10]
# a=[1,2,3,4,5,6,7,8,9,10]
# for i in a:
#     c=a[0::2]
# print(c) #[1, 3, 5, 7, 9]
#
#
# b=[]
# for i in a:
#     if i%2!=0:
#         b.append(i)
# print(b)

# 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"

# url="http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0"
# list1=url.split("?")[1]
# print(list1) #page_size=20&page_index=1&user_id=203317&trade_type=0
# s=list1.split("&")
# print(s) #['page_size=20', 'page_index=1', 'user_id=203317', 'trade_type=0']
# d={}
# for i in s:
#     k,v=i.split("=")
#     d[k]=v
# print(d) #{'page_size': '20', 'page_index': '1', 'user_id': '203317', 'trade_type': '0'}
# print(d.get("page_index")) #1

# a="http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0"
# c=a.split("?")
# d=list(c)
# s=c[-1]
# b=[]
# s1=s.split("&")
# for i in s1:
#     h=i.split("=")
#     b.append(h)
# print(b) #[['page_size', '20'], ['page_index', '1'], ['user_id', '203317'], ['trade_type', '0']]
# e=dict(b)
# print(e) #{'page_size': '20', 'page_index': '1', 'user_id': '203317', 'trade_type': '0'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值