小工具知识网

当前位置: 首页 > 行业知识 > 【python基础】python经典题目100题

行业知识

【python基础】python经典题目100题

2026-11-09 22:55:26 来源:互联网转载

文章目录

  • 前言
  • 初阶题目
    • 1.字符串
    • 2.列表
    • 3.元组
    • 4.字典
    • 5.运算
    • 6.random 模块
    • 7.open函数
    • 8.time模块时间
    • 9.其他
    • 进阶题目

      前言

      本文主要是python经典题目100题,适合入门的新手。仅作自己学习的记录。


      初阶题目

      1.字符串

      • 题目1:怎么找出序列中的最⼤最⼩值?

        方法:使用内置函数max()和min()。

        print(max([x for x in range(5)]))print(min([x for x in range(5)]))l=(233,456,445)print(max(l))print(min(l))
        • 题目2: 怎么将字符列表转为字符串?

          方法一:

          list1=['hello','world']str1=list1[0]str2=list1[1]print(str1+' '+str2)

          方法二: 使用join方法,合并序列的元素,推荐!!!

          功能:join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

          语法:string.join()

          list1=['hello','world']str1=' '.join(list1)print(str1)
          • 题目3:怎么快速打印出包含所有 ASCII 字⺟(⼤写和⼩写)的字符串?
            import stringstring.ascii_letters
            • 题目4: 怎么让字符串居中?

              方法:使用字符串中的center方法。

              功能:返回一个指定宽度width居中的字符串,length为所返回字符串的长度。fillchar为填充的字符,默认为空格。

              语法:string.center(length, character)

              str1='hello'str1.center(50)str1.center(50,'###')
              • 题目5:怎么在字符串中找到⼦串?

                方法:使用find方法。

                功能:检测字符串中是否包含子字符串 str 。如果找到,就返回子串的第一字符的索引值,如果找不到,则返回-1。

                语法:string.find(value,start,end)

                str1='hello world'str1.find('h')str1.find('w')
                • 题目6:怎么让字符的⾸字⺟⼤写,其他字⺟⼩写?

                  方法一:使用title方法。

                  功能:所有单词的首个字母转化为大写,其余字母均为小写。

                  语法:string.title()

                  str1='hello world'str1.title()

                  方法二:使用capwords方法

                  import stringstr1='hello world'string.capwords(str1)
                  • 题目7:怎么批量替换字符串中的元素?

                    方法一:使用replace()方法。

                    str1="hello"str1.replace('l','w')

                    方法二:使用re模块的sub方法。

                    import re str1="hello"re.sub('l','w',str1)
                    • 题目8:怎么把字符串按照空格进⾏拆分?

                      方法一:使用split方法,括号为空的情况下默认为空格拆分 。

                      str1="hello world hello"str1.split()

                      方法二:使用re模块下的split方法。

                      str1="hello world hello"import rere.split(r'\s+',str1)
                      • 题目9:怎么去除字符串⾸位的空格?

                        方法:用strip方法

                        str1=" hello world "str1.strip()

                        2.列表

                        • 题目10:怎么清空列表内容?

                          方法一:使用del方法清除整个列表,删除后表不存在了。

                          list1=[x for x in range(5)]del list1

                          方法二:使用clear方法,删除后表为空。

                          list1=[x for x in range(5)]list1.clear()

                          方法三:使用切片赋值方法,删除后表为空。

                          list1=[x for x in range(5)]list1[:]=[]list1
                          • 题目11:怎么计算指定的元素在列表中出现了多少次?

                            方法:使用count方法。

                            list1=[x for x in np.random.randint(40,90,10)]list1.count(40)
                            • 题目12:怎么在列表末尾加⼊其它元素?

                              方法:使用extend方法。

                              list1=[x for x in range(5)]list2=[x for x in np.random.randint(1,10,3)]list1.extend(list2)
                              • 题目13:extend 和列表相加的区别?

                                两者效果看起来是一致的,但是extend是直接在list1列表里加入元素,相加会生成一个新的列表,list1本质没有改变。

                                • 题目14:怎么查找列表中某个元素第⼀次出现的索引,从 0 开始?
                                  list1=[x for x in range(1,5)]list1.index(3)
                                  • 题目15:怎么将⼀个对象插⼊到列表中?

                                    方法一:使用append方法。在最后插入一个对象。

                                    list1=[x for x in range(1,5)]list1.append(6)

                                    可以指定元素位置后插入。

                                    方法一:使用insert方法。

                                    list1=[x for x in range(1,5)]list1.insert(2,6)

                                    方法二:使用切片方法。

                                    list1=[x for x in range(1,5)]list1[2:2]=['hello']
                                    • 题目16:怎么删除列表中元素?

                                      方法:使用pop方法。

                                      删除单个元素,按位删除(根据索引删除),默认为列表对象最后一个元素,list.pop(i)则删除下标为i的元素,删除时会返回被删除的元素。

                                      list1=[x for x in range(10)]list1.pop[3]
                                      • 题目17:怎么删除列表中指定元素?

                                        方法:使用remove方法,删除第一次出现的元素。

                                        list1=[x for x in range(10)]list.remove(5)
                                        • 题目18:怎么让列表按相反顺序排列?

                                          方法一:使用reverse方法。

                                          list1=[x for x in range(10)]list1.reverse()

                                          方法二:使用切片的方式。

                                          list1=[x for x in range(10)]list1[::-1]

                                          3.元组

                                          • 题目19:怎么表示只包含⼀个元素的元组?

                                            1个元素的元组,必须在唯一的元素后面加上逗号。

                                            tup1=('hello',)print(type(tup1))

                                            4.字典

                                            • 题目20:怎么给字典中不存在的key指定默认值?

                                              方法一

                                              dic1={"a":"hell0","b":"world"}dic1.get('aa','N/A')

                                              方法二

                                              dic1={"a":"hell0","b":"world"}dict1["c"]="mary"dict1

                                              参考链接:https://www.runoob.com/python/att-dictionary-get.html

                                              • 题目21:花括号{} 是集合还是字典?

                                                字典

                                                type({})

                                                5.运算

                                                基本运算

                                                • 题目22:怎么计算2的3次⽅?

                                                  方法一:使用运算符**。

                                                  print(2**3)

                                                  方法二: 用pow()函数,pow(x,y)返回 x 的 y 次方的值

                                                  print(pow(2,3))
                                                  • 题目23:怎么快速求 1 到 100 所有整数相加之和?

                                                    方法一:使用sum方法

                                                    print(sum(range(1,101)))

                                                    方法二:使用while方法

                                                    a=0b=0while a"a","b","c","d","e","f"}set2={"a","b","c","d","g","h"}print(set1.union(set2))"a","b","c","d","e","f"}set2={"a","b","c","d","g","h"}print(set1|set2)"a","b","c","d","e","f"}set2={"a","b","c","d","g","h"}print(set1&set2)"a","b","c","d","e","f"}set2={"a","b","c","d","g","h"}print(set1.intersection(set2))"a","b","c","d","e","f"}set2={"a","b","c","d","g","h"}print(set1^set2)"a","b","c","d","e","f"}set2={"a","b","c","d","g","h"}print(set1.symmertric_differece(set2))"a","b","c","d","e","f"}set2={"a","b","c","d","g","h"}print(set1-set2)"a","b","c","d","e","f"}set2={"a","b","c","d","g","h"}print(set1.differece(set2))'a':1,'b':2,'c':3}dict2=dict(zip('abc','123'))dict3=dict(a=1,b=2,c=3)dict4=dict([('a',1),('b',2),('c',3)])coutry:code for code, coutry in dial_code}coutry_code # 输出:# {'a': 1, 'b': 2, 'c': 3}'a':1,'b':2,'c':3}dict1.setdefault('d',[]).append(4)dict1# 输出:# {'a': 1, 'b': 2, 'c': 3, 'd': [4]}'a':1,'b':2,'c':3}if 'd' not in dict1.keys():dict1['d']=[]dict1['d'].append(4)print(dict1)# 输出:# {'a': 1, 'b': 2, 'c': 3, 'd': [4]}chr(i) for i in range(32,256) if 'SIGN' in name(chr(i),'')}# 输出:{'#', '$', '%', '+', '

上一篇:Docker 安装 MySQL

下一篇:服务器系统红灯怎么消除?