Python

Python语言程序设计——第四部分全部

在这里插入图片描述

示例程序在课程配套教材《Python语言程序设计基础》83页。稍作修改

  1. PassInit = input()
  2. for ind in PassInit:
  3.  if (ord('a') <= ord(ind) <= ord('z')):
    
  4.      PassKaisa = chr(ord('a')+(ord(ind)-ord('a')+3)%26)
    
  5.      print(PassKaisa,end='')
    
  6.  elif  (ord('A') <= ord(ind) <= ord('Z')):
    
  7.      PassKaisa = chr(ord('A')+(ord(ind)-ord('A')+3)%26)
    
  8.      print(PassKaisa,end='')
    
  9.  else:
    
  10.     PassKaisa = ind
    
  11.     print(PassKaisa,end='')
    
  12.     continue
    
  13. 三位水仙花数计算
    除了计算个位、十位和百位,以及对应输出格式之外,也没有什么难点
  14. NumofSXH = []
  15. for ind in range(100,1000):
  16.  A = ind//100
    
  17.  B = (ind//10%10)
    
  18.  C = ind%10
    
  19.  if pow(A,3) + pow(B,3) + pow(C,3) == ind:
    
  20.      NumofSXH.append(ind)
    
  21.  else:
    
  22.      continue
    
  23. LenSXH = len(NumofSXH)
  24. for ind1 in range(LenSXH):
  25. if ind1 < (LenSXH-1):
    
  26.     print('{},'.format(NumofSXH[ind1]),end='')
    
  27. else:
    
  28.     print('{}'.format(NumofSXH[ind1]))
    
  29. 说句心里话
    你们都想说什么呢~~
  30. a = input()
  31. b = input()
  32. str1 = a + ‘,我想对你说,’ + b
  33. print(str1)
  34. 字符串垂直输出
    我在想是否应该可以一行代码搞定的,我可能写复杂了
  35. a = input()
  36. for ind in a:
  37.  print(ind)
    
  38. 词频统计《哈姆雷特》
    这道题相对来说是代码量比较大的题,自己写有一丢丢的难度,但是与课程上给的实例完全相同,只有一些输出格式上的变化,因此代码有问题的。
  39. def getText():
  40.  txt = open('hamlet.txt','r').read()
    
  41.  txt = txt.lower()
    
  42.  for ch in '!"#$%&()*+,-./:;<>=?@[]\\^_{}|`~':
    
  43.      txt = txt.replace(ch," ")
    
  44.  return txt
    
  45. if name == ‘main’:
  46.  content = getText()
    
  47. words = content.split()
    
  48. counts = {}
    
  49. for ind in words:
    
  50.     counts[ind] = counts.get(ind,0) + 1
    
  51. items = list(counts.items())
    
  52. items.sort(key=lambda x:x[1], reverse=True)
    
  53. for ind1 in range(10):
    
  54.     word, times = items[ind1]
    
  55.     print('{:<10},{:>5}'.format(word,times))
    

在这里插入图片描述
用到看到的点个赞呗!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值