今天给大家分享30道Python练习题,超级适合Python初学者,进阶的学习者也可以来练手,建议大家先独立思考一下解题思路,再查看答案。

1. 已知一个字符串为 “hello_world_yoyo”,如何得到一个队列 [“hello”,”world”,”yoyo”] ?

  • 使用 split 函数,分割字符串,并且将数据转换成列表类型:

test = 'hello_world_yoyo'
print(test.split("_"))
12
  • 结果:

['hello', 'world', 'yoyo']

2. 有个列表 [“hello”, “world”, “yoyo”],如何把列表里面的字符串联起来,得到字符串 “hello_world_yoyo”?

  • 使用 join 函数将数据转换成字符串:

test = ["hello", "world", "yoyo"]
print("_".join(test))
  • 结果:

hello_world_yoyo

如果不依赖 python 提供的 join 方法,还可以通过 for 循环,然后将字符串拼接,但是在用“+”连接字符串时,结果会生成新的对象,使用 join 时结果只是将原列表中的元素拼接起来,所以 join 效率比较高。for 循环拼接如下:

“我自己是一名从事了6年python开发的老程序员,今年年初我花了一个月整理了一份最适合2022年自学的Python全套培训教程(视频+源码+笔记+项目实战),加我微信:paisen110打包给每一位python小伙伴,这里是python学习者聚集地,欢迎初学和进阶中的小伙伴(所有python教程关注我的微信公众号:python教程营,关注后回复“python教程”即可领取

test = ["hello", "world", "yoyo"]
# 定义一个空字符串
j = ''
# 通过 for 循环打印出列表中的数据
for i in test:j = j + "_" + i
# 因为通过上面的字符串拼接,得到的数据是“_hello_world_yoyo”,前面会多一个下划线_,所以把这个下划线去掉
print(j.lstrip("_"))

3. 把字符串 s 中的每个空格替换成”%20”,输入:s = “We are happy.”,输出:“We%20are%20happy.”。

  • 使用 replace 函数,替换字符换即可:

s = 'We are happy.'
print(s.replace(' ', '%20'))
12
  • 结果:

We%20are%20happy.

4. Python 如何打印 99 乘法表?

  • for 循环打印:

for i in range(1, 10):for j in range(1, i+1):print('{}x{}={}\t'.format(j, i, i*j), end='')print()
  • while 循环实现:

i = 1
while i <= 9:j = 1while j <= i:print("%d*%d=%-2d"%(i,j,i*j),end = ' ')  # %d: 整数的占位符,'-2'代表靠左对齐,两个占位符j += 1print()i += 1
  • 结果

1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

5. 从下标 0 开始索引,找出单词 “welcome” 在字符串“Hello, welcome to my world.” 中出现的位置,找不到返回 -1。

def test():message = 'Hello, welcome to my world.'world = 'welcome'if world in message:return message.find(world)else:return -1print(test())结果:
7

6. 统计字符串“Hello, welcome to my world.” 中字母 w 出现的次数。

def test():message = 'Hello, welcome to my world.'# 计数num = 0# for 循环 messagefor i in message:# 判断如果 ‘w’ 字符串在 message 中,则 num +1if 'w' in i:num += 1return numprint(test())# 结果
2

7. 输入一个字符串 str,输出第 m 个只出现过 n 次的字符,如在字符串 gbgkkdehh 中,找出第 2 个只出现 1 次的字符,输出结果:d

def test(str_test, num, counts):""":param str_test: 字符串:param num: 字符串出现的次数:param count: 字符串第几次出现的次数:return:"""# 定义一个空数组,存放逻辑处理后的数据list = []# for循环字符串的数据for i in str_test:# 使用 count 函数,统计出所有字符串出现的次数count = str_test.count(i, 0, len(str_test))# 判断字符串出现的次数与设置的counts的次数相同,则将数据存放在list数组中if count == num:list.append(i)# 返回第n次出现的字符串return list[counts-1]print(test('gbgkkdehh', 1, 2))结果:
d

8. 判断字符串 a = “welcome to my world” 是否包含单词 b = “world”,包含返回 True,不包含返回 False。

def test():message = 'welcome to my world'world = 'world'if world in message:return Truereturn Falseprint(test())结果:
True

9. 从 0 开始计数,输出指定字符串 A = “hello” 在字符串 B = “hi how are you hello world, hello yoyo!”中第一次出现的位置,如果 B 中不包含 A,则输出 -1。

def test():message = 'hi how are you hello world, hello yoyo!'world = 'hello'return message.find(world)print(test())结果:
15

10. 从 0 开始计数,输出指定字符串 A = “hello”在字符串 B = “hi how are you hello world, hello yoyo!”中最后出现的位置,如果 B 中不包含 A,则输出 -1。

def test(string, str):# 定义 last_position 初始值为 -1last_position = -1while True:position = string.find(str, last_position+1)if position == -1:return last_positionlast_position = positionprint(test('hi how are you hello world, hello yoyo!', 'hello'))结果:
28

11. 给定一个数 a,判断一个数字是否为奇数或偶数。

while True:try:# 判断输入是否为整数num = int(input('输入一个整数:'))# 不是纯数字需要重新输入except ValueError: print("输入的不是整数!")continueif num % 2 == 0:print('偶数')else:print('奇数')break结果:
输入一个整数:100
偶数

12. 输入一个姓名,判断是否姓王。

def test():user_input = input("请输入您的姓名:")if user_input[0] == '王':return "用户姓王"return "用户不姓王"print(test())结果:
请输入您的姓名:王总
用户姓王

13. 如何判断一个字符串是不是纯数字组成?

利用 Python 提供的类型转行,将用户输入的数据转换成浮点数类型,如果转换抛异常,则判断数字不是纯数字组成。

def test(num):try:return float(num)except ValueError:return "请输入数字"print(test('133w3'))

14. 将字符串 a = “This is string example….wow!” 全部转成大写,字符串 b = “Welcome To My World” 全部转成小写。

a = 'This is string example….wow!'
b = 'Welcome To My World'print(a.upper())
print(b.lower())

15. 将字符串 a = “ welcome to my world ”首尾空格去掉

  • Python 提供了strip() 方法,可以去除首尾空格,rstrip() 去掉尾部空格,lstrip() 去掉首部空格,replace(" ", “”) 去掉全部空格。

a = '  welcome to my world   '
print(a.strip())
  • 还可以通过递归的方式实现:

def trim(s):flag = 0if s[:1]==' ':s = s[1:]flag = 1if s[-1:] == ' ':s = s[:-1]flag = 1if flag==1:return    trim(s)else:return s
print(trim('  Hello world!  '))
  • 通过 while 循环实现:

def trim(s):while(True):flag = 0if s[:1]==' ':s = s[1:]flag = 1if s[-1:] == ' ':s = s[:-1]flag = 1if flag==0:breakreturn s
print(trim('  Hello world!  '))

16. 将字符串 s = “ajldjlajfdljfddd”,去重并从小到大排序输出”adfjl”。

def test():s = 'ajldjlajfdljfddd'# 定义一个数组存放数据str_list = []# for循环s字符串中的数据,然后将数据加入数组中for i in s:# 判断如果数组中已经存在这个字符串,则将字符串移除,加入新的字符串if i in str_list:str_list.remove(i)str_list.append(i)# 使用 sorted 方法,对字母进行排序a = sorted(str_list)# sorted方法返回的是一个列表,这边将列表数据转换成字符串return "".join(a)print(test())结果:
adfjl

17. 打印出如下图案(菱形):

在这里插入图片描述

def test():n = 8for i in range(-int(n/2), int(n/2) + 1):print(" "*abs(i), "*"*abs(n-abs(i)*2))print(test())结果:********************************

18.  给一个不多于 5 位的正整数(如 a = 12346),求它是几位数和逆序打印出各位数字。

class Test:# 计算数字的位数def test_num(self, num):try:# 定义一个 length 的变量,来计算数字的长度length = 0while num != 0:# 判断当 num 不为 0 的时候,则每次都除以10取整length += 1num = int(num) // 10if length > 5:return "请输入正确的数字"return lengthexcept ValueError:return "请输入正确的数字"# 逆序打印出个位数def test_sorted(self, num):if self.test_num(num) != "请输入正确的数字":# 逆序打印出数字sorted_num = num[::-1]# 返回逆序的个位数return sorted_num[-1]print(Test().test_sorted('12346'))结果:
1

19. 如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。例如:153 = 13 + 53 + 33,因此 153 就是一个水仙花数。那么如何求 1000 以内的水仙花数(3 位数)。

def test():for num in range(100, 1000):i = num // 100j = num // 10 % 10k = num % 10if i ** 3 + j ** 3 + k ** 3 == num:print(str(num) + "是水仙花数")test()

20. 求 1+2+3…+100 相加的和。

i = 1
for j in range(101):i = j + iprint(i)结果:
5051

21. 计算 1-2+3-4+5-…-100 的值。

def test(sum_to):# 定义一个初始值sum_all = 0# 循环想要计算的数据for i in range(1, sum_to + 1):sum_all += i * (-1) ** (1 + i)return sum_allif __name__ == '__main__':result = test(sum_to=100)print(result)-50

22. 现有计算公式 13 + 23 + 33 + 43 + …….+ n3,如何实现:当输入 n = 5 时,输出 225(对应的公式 : 13 + 23 + 33 + 43 + 53 = 225)。

def test(n):sum = 0for i in range(1, n+1):sum += i*10+ireturn sumprint(test(5))结果:
225

23. 已知 a 的值为“hello”,b 的值为“world”,如何交换 a 和 b 的值,得到 a 的值为“world”,b 的值为”hello”?

a = 'hello'
b = 'world'c = a
a = b
b = c
print(a, b)

24. 如何判断一个数组是对称数组?

  • 例如 [1,2,0,2,1],[1,2,3,3,2,1],这样的数组都是对称数组。

  • 用 Python 判断,是对称数组打印 True,不是打印 False。

def test():x = [1, 'a', 0, '2', 0, 'a', 1]# 通过下标的形式,将字符串逆序进行比对if x == x[::-1]:return Truereturn Falseprint(test())结果:
True

25. 如果有一个列表 a = [1,3,5,7,11],那么如何让它反转成 [11,7,5,3,1],并且取到奇数位值的数字 [1,5,11]?

def test():a = [1, 3, 5, 7, 11]# 逆序打印数组中的数据print(a[::-1])# 定义一个计数的变量count = 0for i in a:# 判断每循环列表中的一个数据,则计数器中会 +1count += 1# 如果计数器为奇数,则打印出来if count % 2 != 0:print(i)test()结果:
[11, 7, 5, 3, 1]
1
5
11

26. 对列表 a = [1, 6, 8, 11, 9, 1, 8, 6, 8, 7, 8] 中的数字从小到大排序。

a = [1, 6, 8, 11, 9, 1, 8, 6, 8, 7, 8]
print(sorted(a))结果:
[1, 1, 6, 6, 7, 8, 8, 8, 8, 9, 11]

27. 找出列表 L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88] 中最大值和最小值。

L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]
print(max(L1))
print(min(L1))结果:
88
1
  • 上面是通过 Python 自带的函数实现,如下,可以自己写一个计算程序:

class Test(object):def __init__(self):# 测试的列表数据self.L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]# 从列表中取第一个值,对于数据大小比对self.num = self.L1[0]def test_small_num(self, count):""":param count: count为 1,则表示计算最大值,为 2 时,表示最小值:return:"""# for 循环查询列表中的数据for i in self.L1:if count == 1:# 循环判断当数组中的数据比初始值小,则将初始值替换if i > self.num:self.num = ielif count == 2:if i < self.num:self.num = ielif count != 1 or count != 2:return "请输入正确的数据"return self.numprint(Test().test_small_num(1))
print(Test().test_small_num(2))
结果:
88
1

28. 找出列表 a = [“hello”, “world”, “yoyo”, “congratulations”] 中单词最长的一个。

def test():a = ["hello", "world", "yoyo", "congratulations"]# 统计数组中第一个值的长度length = len(a[0])for i in a:# 循环数组中的数据,当数组中的数据比初始值length中的值长,则替换掉length的默认值if len(i) > length:length = ireturn lengthprint(test())结果:
congratulations

29. 取出列表 L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88] 中最大的三个值。

def test():L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]return sorted(L1)[:3]print(test())结果:
[1, 2, 2]

30. 把列表 a = [1, -6, 2, -5, 9, 4, 20, -3] 中的数字绝对值从小到大排序。

def test():a = [1, -6, 2, -5, 9, 4, 20, -3]# 定义一个数组,存放处理后的绝对值数据lists = []for i in a:# 使用 abs() 方法处理绝对值lists.append(abs(i))return listsprint(test())结果:
[1, 6, 2, 5, 9, 4, 20, 3]

值得收藏的30道Python练手题(附详细答案)相关推荐

  1. 值得收藏的30道Python练手题(附详解)

    今天给大家分享30道Python练习题,建议大家先独立思考一下解题思路,再查看答案. 1. 已知一个字符串为 "hello_world_yoyo",如何得到一个队列 [" ...

  2. 用心整理值得收藏的30道Python练手题(附详细答案)

    1.已知一个字符串为"hello_world_yoyo",如何得到一个队列["hello" ,"world","yoyo" ...

  3. 值得收藏的25道Python练手题(附详细答案)

    来源丨吾爱破解 题目 1:水仙花数 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数. ...

  4. 墙裂建议收藏,100道Python练手题目

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 转自丨Python知识大全 ‍来源丨大学 github.com/R ...

  5. 柚柚建议收藏,100道Python练手题目(适合新手练级)

    目录 实例001:数字组合 实例002:"个税计算" 实例003:完全平方数 实例004:这天第几天 实例005:三数排序 实例006:斐波那契数列 实例007:copy 实例00 ...

  6. 值得收藏的45个Python优质资源(附链接)

    授权自AI科技大本营(ID:rgznai100) 本文共4157字,建议阅读7分钟. 本文为大家挑选了适合初学的45个Python的优质项目. 热门资源博客 Mybridge AI 比较了18000个 ...

  7. 几道python练手小题目

    文章目录 python期末考试复习 实验 基本 列表 字典 函数 方法 字符串 文件 做图 python期末考试复习 实验 基本 1. 水仙花数 value = input("请输入一个三位 ...

  8. 30个Python练手项目

    大家好,今天为大家推荐一个Pythoner和他的公众号:可以叫我才哥. 才哥目前从事游戏运营,是我的读者也是我的好朋友.他最早只是用excel和tableau等工具进行游戏数据分析,后来从0开始自学P ...

  9. 掌握这30道python面试总结题,华为,阿里,腾讯offer拿到手软~

    Python是目前编程领域最受欢迎的语言.在本文中,我将总结华为.阿里巴巴等互联网公司Python面试中最常见的30个问题.每道题都提供参考答案,希望能够帮助你在求职面试中脱颖而出,找到一份高薪工作. ...

  10. 70个Python练手项目列表 预祝大家 快乐

    小孩眺望远方,成人怀念故乡. 为此给大家分享一下珍藏的Python实战项目,祝大家节日快乐哦!!! Python 前言:不管学习哪门语言都希望能做出实际的东西来,这个实际的东西当然就是项目啦,不用多说 ...

最新文章

  1. 超20亿!2018年国家自然科学基金(重点项目)出炉!
  2. python爬取站长素材网页图片保存到ppt中
  3. 在(CListView)列表视图中添加右键菜单的方法
  4. Python入门100题 | 第063题
  5. 高效、易用、功能强大的 api 管理平台(内附彩蛋)
  6. php泥浆护壁,谈一下静态泥浆护壁的湿式旋挖工法施工      常见质量方面问题处理...
  7. iOS开发--地图与定位
  8. 微软推行 Windows 10 更新政策 往后不会太强硬要求升级!
  9. Pandas Series 1
  10. 一步步教你轻松学主成分分析PCA降维算法
  11. 全国短消息服务中心号码
  12. Java8新特性之一:Lambda表达式
  13. 检查网络是否畅通的四个步骤
  14. mysql编译器安卓版_手机在线编译器
  15. 如果你在it院校学习累了,你能干什么?
  16. 详解intel处理器命名规则
  17. unity3D 跳一跳
  18. php 判断字符串乱码,php如何检测乱码字符
  19. 3.9、互斥锁(互斥量)
  20. Vue组织架构树图组件vue-org-tree的使用

热门文章

  1. 热点争议:Web设计师需要编程知识吗?
  2. MonoMac 1.0正式发布
  3. NameNode DataNode
  4. android swap 大小,Android系统手动创建虚拟内存swap
  5. java生成随机数的代码_java随机数代码的三种生成方式怎么编写?
  6. MyBatis官方文档——SQL语句构建及日志部分
  7. win7系统下装ubuntu系统
  8. android studio 自动实现方法,Android Studio怎么自动实现所有的接口函数
  9. 在CentOS6.7搭建Qt5.6.2环境
  10. 密码库LibTomCrypt学习记录——(2)分组密码算法的工作模式