day:22 python的作业

1、求出1 / 1 + 1 / 3 + 1 / 5……+1 / 99的和 (1分之一+1分之三+1分支5....)
sum=0
for i in range(1,100,2):
sum+=1/i
print(sum)

2、用循环语句,计算2 - 10之间整数的循环相乘的值 (2345....10)
方法一:
b=1
a=1
while a<10:
a+=1
b
=a
print(b)
3628800

方法二:
a = 1
for i in range(1,11):
a*=i
print(a))

3、用for循环打印九九乘法表
for i in range(1,10):控制个数
for j in range(1,i+1):
print(f"{i}{j}={ij}",end=",") 控制格式
print()

错误格式:

f-string 是 Python 3.6 引入的一种字符串格式化方法,全称为 格式化字符串字面值(Formatted String Literals)。它通过在字符串前加前缀 fF,允许在字符串中直接嵌入表达式(如变量、运算等),使代码更简洁、易读。

4、求每个字符串中字符出现的个数如:helloworld
方法一:利用for语句遍历
a=input("请输入字符串:")
for i in set(a):
print(i,"有",a.count(i),end="个; ")

方法二:利用字典键的唯一性
a="helloworld"
d={}
for i in a: #在a中遍历d的键
if i in d: #i键在d中存在
d[i]+=1 #i键值加1
else:
d[i]=1 #否则此键值为1
print(d)

5、实现把字符串str = "duoceshi"中任意字母变为大写(通过输入语句来实现)
遍历
a=input("请输入字符串:")
print(a)
for i in a:
if i>="a" and i<="z":
print(i.upper(),end="")
else:
print(i,end="") 把所有的都变成了大写

输入任意值变为大写
str="duoceshi"
l=len(str)
b=input("请输入改成大写的字母:")
i=0
while i < l :
if b==str[i]:
print(b.title(),end="")
else:
print(str[i],end="")
i=i+1

6、分别打印100以内的所有偶数和奇数并存入不同的列表当中

for i in range(1,100,2):
print(i, end=",")
for m in range(1,100):
if m==i:
continue
print(m,end=",")

a=list();b=list()
for i in range(1,101):
if i%2==1:
a.append(i)
else:
b.append(i)
print(a);print(b)

x=[]
y=[]
for i in range(1,101):
if i%2==0:
x.append(i)
else:
y.append(i)
print(x)
print(y)

sumj = []

sumo =[]

for i in range(1,101):

if i%2 == 1:

sumj.append(i)

else:

sumo.append(i)

print('奇数',sumj)

print('偶数',sumo)

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

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

x=[1, 3, 6, 9, 1, 8]
d={}
s=d.fromkeys(x)
print(list(s))

if 计数 如果超过1,则只记录一个

8、将字符串类似:"k:1|k3:2|k2:9" 处理成key:value或json格式,比如{"k": "1", "k3": "2"}
d={}
str="k:1|k3:2|k2:9"
str1=str.replace("|","😊
str2=str1.split("😊
for i in range(0,len(str2),2):
key=str2[i]
value=str2[i+1]
d[key]=value
print(d)

9、把字符串user_controller转换为驼峰命名UserController大驼峰在java用作变量命名(前英文为大写后英文为小写) 小驼峰:作为变量命名
驼峰命名:每个单词的首字母大写
小驼峰命名:第一个单词首字母小写,后续单词首字母大写
驼峰命名
方法一:capitalize 首字母大写
a='user_controller'
str1=a.split('')
a=str1[0].capitalize()
b=str1[1].capitalize()
方法二:title 每段首字母大写
str = "user_controller"
newstr = str.title()
ansstr=newstr.replace("
",'')
print(ansstr)
print(a+b)
小驼峰命名
a='user_controller'
str1=a.split("_")
a=str1[0]
b=str1[1].capitalize()
print(a+b)

str="user_controller"
for i in str.split('_'):
print(i.title(),end='')

def to_camel_case(s, uppercase_first=False):
components = s.split('_')
if uppercase_first:
return ''.join(x.capitalize() for x in components)
else:
return components[0].lower() + ''.join(x.capitalize() for x in components[1:])

print(to_camel_case("user_controller", uppercase_first=True))
print(to_camel_case("user_controller", uppercase_first=False))

str="user_controller"
a=str.split("_")
b=a[0]
c=a[1]
print("大驼峰命名:",b.title()+c.title())
print("小驼峰命名",b+c.title())

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

冒泡排序
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....

此为斐波那契数列 (考试题非常多次题目)

方法一:
a=1;b=1;c=0;
print(a,b,end=" ")
while a+b<=34:
c=a+b
a=b
b=c
print(c,end=" ")
方法二:
def hs(n):
if n<=0:
return 1
elif n==1:
return 1
a,b=1,1
for _ in range(2,n+1):
a,b=b,a+b
return b
for i in range(9):
print(hs(i),end=" ")

list1 = [1,1]

print(len(list1))

print(list1)

def adds():

n = int(input("请输入要增加的数量:"))

num = 0

while num < n:

num+=1

i = len(list1)

val = list1[i-2]+list1[i-1]

list1.append(val)

print(list1)

adds()

sum=0
x=int(input("请输入计算几次"))
y=1
z=0
a=[]
for i in range(1,x+1):
z=sum
sum+=y
a.append(sum)
y=z
print(a)

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

13、有如下列表,统计列表中的字符串出现的次数

a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']

a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']
b={}
for i in a:
b[i]=a.count(i)
print(b)

14、、列表推导式求出列表所有奇数并构造新列表 a =[1,2,3,4,5,6,7,8,9,10]

b=[]
a =[1,2,3,4,5,6,7,8,9,10]
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"
a=url.find("?")
b=url[a+1:]
c=b.split("&")
dd={}
for i in c:
key,values=i.split("=")
dd[key]=values
print(dd)
print(dd.get("page_size"))
print(dd.get("page_index"))
print(dd.get("user_id"))
print(dd.get("trade_type"))

posted @ 2025-03-15 19:11  君庭  阅读(26)  评论(0)    收藏  举报