zoukankan      html  css  js  c++  java
  • 一些python练习题

    手动手机的一些python练习题,有时间的时候敲一敲。

    """
        Python使用简洁代码
    """
    
    # # 1,交换两个变量的值
    # a = 666
    # b = 999
    # a, b = b, a # 交换值
    # print(a,b) # 999 666
    #
    # # 2,查找对象使用的内存
    # import sys
    # slogan = '今天是个好日子!'
    # print(sys.getsizeof(slogan)) # 90
    
    # 3,翻转字符串
    import random
    
    a = '今天是个好日子!'
    b = a[::-1] # 翻转
    
    print(b) # !子日好个是天今
    
    # 4,检查字符串是否为回文
    def is_palindrome(string):
        return string == string[::-1]
    print(is_palindrome('今天是个好日子')) # False
    print(is_palindrome('我们们我')) # True
    
    # 5,将列表里的元素合并成字符串
    alist = ['I', 'am', 'god', 'man']
    astring = ' '.join(alist)
    print(astring) # I am god man
    
    # 6,查找两个列表中所有不重复的元素
    def union(list1, list2):
        return list(set(list1+list2))
    
    alist = [5,2,0]
    blist = [1,2,3]
    print(union(alist, blist)) # [0, 1, 2, 3, 5]
    
    # 7,打印字符串N次
    astring = '我们'
    nstring = 10 * astring
    print(nstring) # 输出:我们我们我们我们我们我们我们我们我们我们
    
    # 8,链式比较
    a = 100
    print(98 < a < 120) # True
    
    print(100==a<120) # True
    
    # 9,单词大小写问题
    a = 'I am god man'
    
    b = a.upper() # 全部大写 I AM GOD MAN
    c = a.capitalize() # 首字母大写 I am god man
    d = a.title() # 各单词首字母大写 I Am God Man
    print(b, c, d)
    
    # 10,统计列表中元素出现的频率
    from collections import Counter
    numbers = [1, 1, 3, 2, 4, 4, 3, 6]
    # 开始统计
    counters = Counter(numbers)
    print(counters) # 返回的是类似一个字典Counter({1: 2, 3: 2, 4: 2, 2: 1, 6: 1})
    
    # 11,判断字符串所含元素是否相同
    a = 'python'
    b = 'ypthon'
    count_a, count_b = Counter(a), Counter(b)
    
    if count_a == count_b:
        print('Yes') # Yes
    else:
        print('不相同')
    
    # 12,将数字字符串转换成数字列表
    sstring = "666888"
    numbers = list(map(int, sstring))
    
    print(numbers) # [6, 6, 6, 8, 8, 8]
    
    
    # 13,使用enumerate来获取列表的索引和值
    string = 'python'
    
    for index, value in enumerate(string):
        print(index, value)
    
    
    # 14,代码执行消耗时间
    import time
    
    start_time = time.time()
    numbers = [i for i in range(1, 100)]
    end_time = time.time()
    time_cosume = end_time - start_time
    
    print('代码执行消耗的时间为:{}'.format(time_cosume)) # 代码执行消耗的时间为:0.0
    
    # 15,比较集合和列表的查找效率
    
    number = 999999
    # 生成数字列表和数字集合
    numList = [i for i in range(1, 1000000)]
    numSet = {i for i in range(1, 1000000)}
    
    # 列表的查找耗时评估
    start_time = time.time()
    _ = number in numList
    end_time = time.time()
    print('列表的查找时间为:{}'.format(end_time-start_time)) # 列表的查找时间为:0.009906291961669922
    
    # 集合的查找耗时评估
    start_time = time.time()
    _ = number in numSet
    end_time = time.time()
    print('集合的查找时间为:{}'.format(end_time-start_time)) # 集合的查找时间为:0.0
    
    # 16,字典的合并
    d1 = {"apple":13, "orange":12} # 其实打印是字典
    d2 = {"爆款协作": 48, "特朗普":"没妈妈"}
    
    # 合并两个字典
    new_d = {**d1, **d2}
    print(new_d) # {'apple': 13, 'orange': 12, '爆款协作': 48, '特朗普': '没妈妈'}
    
    # 17,随机采样
    import random
    books = ["爆款写作", "这个世界,偏爱会写作的人", "写作七堂课", "越书写越明白"]
    
    # 随机取两本
    reading_books = random.sample(books, 2)
    
    print(reading_books) # ['爆款写作', '写作七堂课']
    
    # 18,判断列表元素的唯一性
    def is_unique(lst):
        if len(lst) == len(set(lst)): # 集合会去除列表的重复元素
            return True
        else:
            return False
    
    numbers = [1, 1, 2, 3, 4, ]
    print(is_unique(numbers)) # False
    
    # 19,递归实现阶乘
    def fac(n):
        if n > 1:
            return n * fac(n-1)
        else:
            return 1
    
    number = 5
    # number = int(input('请输入整数: '))
    print('结果为:{}'.format(fac(number)))
    
    # 20,列出当前文件夹下所有的目录和文件名
    import os
    
    files = [file for file in os.listdir('.')]
    print(files)
    
    # 21,将原字典的键值对调并产生新的字典
    orginal_dict = {1: "java", 2:"python"}
    new_dict = {value:key for key, value in orginal_dict.items()}
    print(new_dict) # {'java': 1, 'python': 2}
    
    # 22,九九乘法表
    for i in range(1, 10):
        print("")
        for j in range(1, i + 1):
            print('{} * {} = {}'.format(i, j, i*j),end=' ')
    
    # 23,计算每个月的天数
    import calendar
    month_days = calendar.monthrange(2021, 8)
    print(month_days) # (4, 31)
    
    # 24, 随机生成验证码
    import random, string
    
    # 数字
    str_1 = '0123456789'
    
    # 字母
    str_2 = string.ascii_letters
    print(str_2) # print(str_2)
    
    # 拼接
    str_3 = str_1 + str_2
    print(str_3)
    
    # 多个字符选项中选取特定数量的字符
    captcha = random.sample(str_3, 6) # 得到随机取样得到列表 ['G', 'y', 'g', 'V', '0', 's']
    print(captcha)
    
    # 将其转换成字符串
    captcha_str = ''.join(captcha)
    print(captcha_str) # WQj2hM
    
    # 25,判断闰年
    year = input('请输入四位数年份:')
    year = int(year)
    # 两个闰年条件,一个普通闰年,是4的倍数且不是100倍数的;还一个世纪闰年,公历年份是整百的,且必须是400的倍数
    if ((year % 4 == 0 and year % 100!= 0) or (year % 400 == 0)):
        print("{}是闰年!".format(year))
    else:
        print("{}不是闰年!".format(year))

    逐梦很累,坚持加油。

  • 相关阅读:
    菜鸟记录:如何获取LOGINVIEW控件状态模板中的子控件
    无法安装dotnetFramework35sp1的解决方法
    MOSS2007小技巧:不用SPD轻松删除错误页面上的问题Webpart
    在动态页面里象静态页面一样控制整个网页的缓存和更新
    烦人的网页iframe去除
    经典sql注入教程
    自己写的后台静态权限验证类
    Asp.net项目从Vs2003转换到Vs2005的常见问题大全及解决方法
    C# 相当于ASP里Eval中的计算公式的方法(超简单的方法)
    1 UNIX与Linux的发展
  • 原文地址:https://www.cnblogs.com/mafu/p/15524442.html
Copyright © 2011-2022 走看看