python创建变量revenue、并赋值为98765_Python序列与文件编程练习

练习一:

使用以下语句存储一个字符串:

string = ‘My moral standing is: 0.98765’

将其中的数字字符串转换成浮点数并输出。

代码:

# -*- coding: utf-8 -*-

"""

Spyder Editor

This is a temporary script file.

"""

s = "My moral standing is: 0.98765"

p = float(s.split(":")[1])

print(p)

练习二:

自定义函数move_substr(s, flag, n),将传入的字符串s按照flag(1代表循环左移,2代表循环右移)的要求左移或右移n位(例如对于字符串abcde12345,循环左移两位后的结果为cde12345ab,循环右移两位后的结果为45abcde123),结果返回移动后的字符串,若n超过字符串长度则结果返回-1。__main__模块中从键盘输入字符串、左移和右移标记以及移动的位数,调用move_substr()函数若移动位数合理则将移动后的字符串输出,否则输出“the n is too large”。

代码:

# -*- coding: utf-8 -*-

"""

Spyder Editor

This is a temporary script file.

"""

def mv(s, flag, n):

if (n>len(s)):

return -1

else:

if (flag==1):

return s[n:]+s[:n]

else:

return s[-n:]+s[:-n]

s,flag,n=input("Please input s flag n").split()

ans = mv(s, int(flag), int(n))

if (ans==-1):

print("n's too large")

else:

print(ans)

练习三:

定义函数countchar()按字母表顺序统计字符串中26个字母出现的次数(不区分大小写)。例如字符串“Hope is a good thing.”的统计结果为:

[1, 0, 0, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 1, 3, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]

代码:

"""

Spyder Editor

This is a temporary script file.

"""

def coalpha(s):

lst = [0]*26

for w in range(len(s)):

if (s[w]>'a') and (s[w]

lst[ord(s[w])-ord('a')]+=1

return lst

s = input("Please input S:")

s = s.lower()

print(coalpha(s))

练习四:

有一个咖啡列表[‘32Latte’, ‘_Americano30’, ‘/34Cappuccino’, ‘Mocha35’],列表中每一个元素都是由咖啡名称、价格和一些其他非字母字符组成,编写一个函数clean_list()处理此咖啡列表,处理后列表中只含咖啡名称,并将此列表返回。__main__模块中初始化咖啡列表,调用clean_list()函数获得处理后的咖啡列表,并利用zip()函数给咖啡名称进行编号后输出,输出形式如下:

1 Latte

2 Americano

3 Cappuccino

4 Mocha

代码:

# -*- coding: utf-8 -*-

"""

Spyder Editor

This is a temporary script file.

"""

def pos(lis):

cleaned_list = []

for item in lis:

for key in item:

if key.isalpha()==False:

item = item.replace(key,'' )

cleaned_list.append(item)

return cleaned_list

coffee_list = ['32Latte', '_Americano30', '/34Cappuccino', 'Mocha35']

cleaned = pos(coffee_list)

i=1

for key in cleaned:

print(i,key)

i+=1

练习五:

从键盘输入整数n(1-9之间),对于1-100之间的整数删除包含n并且能被n整除的数,例如如果n为6,则要删掉包含6的如6,16这样的数及是6的倍数的如12和18这样的数,输出所有满足条件的数,要求每满10个数换行。

测试数据:

Enter the number: 6

屏幕输出:

1,2,3,4,5,7,8,9,10,11

13,14,15,17,19,20,21,22,23,25

27,28,29,31,32,33,34,35,37,38

39,40,41,43,44,45,47,49,50,51

52,53,55,57,58,59,70,71,73,74

75,77,79,80,81,82,83,85,87,88

89,91,92,93,94,95,97,98,99,100

参考代码:

# -*- coding: utf-8 -*-

"""

Spyder Editor

This is a temporary script file.

这里为了保证输出的10个数在同一行中不使用列表而使用字符串类型

"""

n = int(input("Please input n")) #这里再次注意输入的为字符串类型

num=0 #用于判断个数

lis=''

for i in range(1,101):

if (i % 10==n) or ((i//10) % 10==n) or ((i//100) % 10==n) or (i % n==0):

continue

lis+=str(i)

num+=1

if num==10:

print(lis)

lis=''

num=0

else:

lis+=','

if num!=0:

print(lis)

练习六:

请用随机函数产生500行1-100之间的随机整数存入文件random.txt中,编程寻找这些整数的众数并输出,众数即为一组数中出现最多的数。

参考代码:

# -*- coding: utf-8 -*-

"""

Spyder Editor

This is a temporary script file.

"""

import random

with open('random.txt','w+') as f:

for i in range(1,501):

f.write(str(random.randint(1,100)))

f.write('\n')

f.seek(0)

ans=f.readlines()

for key in ans:

key = key.strip()

co = [0]*120

for key in ans:

co[int(key)]+=1

maxx=-10000

ret=-1

for i in range(1,101):

if maxx < co[i]:

maxx = co[i]

ret=i

else:

continue

print(ret)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值