Python练习 List join,判断等,执行字符串内的代码,字典合并,模拟秒表

List join,判断等

def list_join() -> None:
    """
list 常用join 相反的是 split split的第二个参数是分割的次数
list 过滤
    """
    list1 = ['1', '2', '3', '4', '5', '6']
    print(",".join(list1))

    dict1 = {"name": "jack", "age": "male", "long": "2.0"}
    print(";".join(["%s:%s" % (k, v) for k, v in dict1.items()]))

    print([i * 2 for i in [1, 2, 3, 4]])

    print(dict1.keys())
    print(type(dict1.keys()))
    print(dict1.values())
    print(type(dict1.values()))
    print(dict1.items())
    print(type(dict1.items()))

    # 过滤
    print([i for i in [1, 2, 3, 4, 5, 6] if i % 2 == 0])
    # 元素出现次数
    print(list1.count('1'))

执行字符串内的代码

def exec_str() -> None:
    """
执行字符串内的代码
    """
    localstr = """
def calender_show() -> None:

    year = int(input("输入年份:"))
    month = int(input("输入月份:"))

    # calendar.calendar(指定年份 输出当前年的所有月份日历)
    print(calendar.month(year, month))

calender_show()
    """
    exec(localstr)

字典合并

def merge_dicts() -> None:
    """
字典合并
    """
    dict1 = {'a':123, 'bf': 35, 'cjw':97}
    dict2 = {'cc':123, 'qq': 35, 'ee':97}
    # dict1.update(dict2)
    dictresult = {**dict1, **dict2}
    print(dictresult)

模拟秒表

def stopwatch() -> None:
    """
秒表计时
    """
    print("按下enter开始计时")
    input("")
    start = time.time()
    while True:
        try:
            print(f'-> :{round(time.time() - start, 0)} senonds')
            time.sleep(1)
        except KeyboardInterrupt:
            print('total:{} seconds'.format(round(time.time() - start)))
            break
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值