码迷,mamicode.com
首页 > 编程语言 > 详细

8道python练习题,能做出来的没几个

时间:2020-07-26 00:20:22      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:NPU   字符串   字符串分割   UNC   输入   ict   保留   pen   tty   

  1. 变量的定义

    程序就是用来处理数据的,而变量就是用来存储数据的

  2. 很多人学习python,不知道从何学起。
    很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
    很多已经做案例的人,却不知道如何去学习更加高深的知识。
    那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
    QQ群:1097524789

  3. Python3 的六个标准数据类型中:

    不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组); 可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。

  4. 变量的命名规则

    在Python程序中,变量是用一个变量名表示,变量名必须是大小写英文、数字和下划线(_)的组合,且不能用数字开头

  5. 字符串常用方法

    1. find方法可以在一个较长的字符串中查找子串,他返回子串所在位置的最左端索引,如果没有找到则返回-1

       a = ‘abcdefghijk‘
       print(a.find(‘abc‘)) #the result : 0
       print(a.find(‘abc‘,10,100)) #the result : 11 指定查找的起始和结束查找位置
    2. join方法是非常重要的字符串方法,他是split方法的逆方法,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。

       a = [‘1‘,‘2‘,‘3‘]
       print(‘+‘.join(a)) #the result : 1+2+3
    3. split方法,是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列

       print(‘1+2+3+4‘.split(‘+‘))                          #the result : [‘1‘, ‘2‘, ‘3‘, ‘4‘]
    1. strip 方法返回去除首位空格(不包括内部)的字符串

       print("   test   test    ".strip())                  #the result :“test   test”
    1. replace方法返回某字符串所有匹配项均被替换之后得到字符串

       print("This is a test".replace(‘is‘,‘is_test‘))     #the result : This_test is_test a test
  1. 常见操作练习

 ‘‘‘
 1. str = "" 写一个函数,只去掉字符串右侧的空格,左侧的空格保留
 ‘‘‘
 def fun1(s):
  a = s[s.find(‘f‘):]
  print(a)
  return a
 ?
 if __name__ == ‘__main__‘:
  str=‘ fgh ‘
  fun1(str)
 ?
 ?
 ‘‘‘
 2. 输入10个数字到列表中,如果输入的不是数字,则跳过,不存
 ‘‘‘
 def fun2(a):
  alist = []
  while True:
  if len(a) == 10:
  if a.isdigit():
  alist.append(a)
  print("存入成功:", alist)
  else:
  print("请输入10位‘数字‘")
  else:
  pass
  print("请输入‘10位‘数字")
  return a
 ?
 if __name__ == ‘__main__‘:
  a=input("请输入数字:")
  fun2(a)
 ?
 ?
 ‘‘‘
 3. 写一个函数,可以判断一个字符串是否为回文例子qwewq,函数返回true或者false
 ‘‘‘
 def fun3(s):
  if s == ‘‘.join(reversed(s)):
  print(True)
  else:
  print(False)
 ?
 if __name__ == ‘__main__‘:
  s=input("请输入字符串:")
  fun3(s)
 ?
 ?
 ‘‘‘
 4. 请手写一个函数,可以打印出 I‘m "ok" it‘s your‘s 注意必须是原样输出
 ‘‘‘
 def fun4():
  a = [‘I‘, ‘m‘]
  b = "‘".join(a)
 ?
  c = [‘"ok"‘]
  d = ‘‘.join(c)
 ?
  e = ["it‘s"]
  f = ‘‘.join(e)
 ?
  g = ["your‘s"]
  h = ‘‘.join(g)
 ?
 ?
  sum = b + " " + d + " " + f + " " + h
  print(sum)
 ?
 if __name__ == ‘__main__‘:
  fun4()
 ?
 ?
 ‘‘‘
 5. str2 = "This is the voa special English,health,report" 写一个函数,统计字符串中单词出现的个数,注意是单词而不是字母
 ‘‘‘
 def fun5():
  str2 = "This is the voa special English,health,report"
  a = str2.split()[:-2]
  b = str2.split()[-1].split(‘,‘)
  for i in a:
  print(i,a.count(i))
  for i in b:
  print(i,b.count(i))
 if __name__ == ‘__main__‘:
  fun5()
 ?
 ?
 ‘‘‘
 6. My_str = ‘11sdsfsdf45sfxcv67qwe_9’ 手写一个函数,计算出字符串中所有数字的和
 ‘‘‘
 def fun6():
  My_str = ‘11sdsfsdf45sfxcv67qwe_9‘
  sum = 0
  for i in My_str:
  if i.isdigit():
  sum += int(i)
  else:
  pass
 ?
  print(sum)
  return sum
 ?
 if __name__ == ‘__main__‘:
  fun6()
 ?
 ?
 ‘‘‘
 7. s = ‘<a href="www.test.com">test</a>‘ 写一个函数,能将字符串中的网址提取出来,即提取出www.test.com
 ‘‘‘
 def fun7():
  s = ‘<a href="www.test.com">test</a>‘
  link = re.findall(r‘<a href="(.*?)">‘,s)[0]
  print(link)
 if __name__ == ‘__main__‘:
  fun7()
  
  
 ‘‘‘
 8. str = "卡巴斯基#杀毒软件#免费版#俄罗斯#" 手写一个函数,将该字符串解析为[‘卡巴斯基‘, ‘杀毒软件‘, ‘免费版‘, ‘俄罗斯‘]
 ‘‘‘
 def fun8():
  str = "卡巴斯基#杀毒软件#免费版#俄罗斯#"
  a = str.replace("#",‘ ‘).split()
  print(a)
 if __name__ == ‘__main__‘:
  fun8()

8道python练习题,能做出来的没几个

标签:NPU   字符串   字符串分割   UNC   输入   ict   保留   pen   tty   

原文地址:https://www.cnblogs.com/shann001/p/13376542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!