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

2024-06-08 1046阅读

文章目录

  • 前言
  • 初阶题目
    • 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 string
            string.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 string
                  str1='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 re
                      re.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=0
                                                    b=0
                                                    while 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),'')}
                                                    # 输出:
                                                    {'#',
                                                     '$',
                                                     '%',
                                                     '+',
                                                     '
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]