Python笔记:常用的标准库

Python 中常用的标准库

  • python中获取当前时间和日期的模块: datetime

  • python中具有更改当前工作目录的方法 os

  • 哪个模块可以将逗号分隔 (.csv) 文件中的每行数据读取到 Python 中? csv

  • 哪个模块可以帮助我们从 zip 文件中提取所有文件? zipfile

  • 哪个模块可以显示代码的运行时间? time

Python 中常用的标准库[相关练习]

  • 使用 math 模块,计算 e 的3次幂,然后 输出 答案

      import math
      print(math.exp(3)) # 20.085536923187668
    
  • 写一个叫做 generate_password 的函数,该函数会从提供的单词文件中随机选择三个单词,并将它们连接成一个字符串。我们已经在起始代码中提供了从文件中读取数据的代码,你需要利用这些部分构建一个密码。

    words.txt:

      Alice
      was
      beginning
      to
      get
      very
      tired
      of
      sitting
      by
      her
      sister
      bank
      having
      nothing
      Once
      twice
      she
      had
      peeped
      into
      the
      book
      her
      sister
      was
      reading
      but
      it
      had
      no
      pictures
      or
      conversations
      in
      it
      and
      what
      is
      the
      use
      of
      a
      book
      thought
      Alice
      without
      pictures
      or
      conversations
    

    password_generator.py :

      # Use an import statement at the top
      import random
      word_file = "words.txt"
      word_list = []
    
      #fill up the word_list
      with open(word_file,'r') as words:
        for line in words:
          # remove white space and make everything lowercase
          word = line.strip().lower()
          # don't include words that are too long or too short
          if 3 < len(word) < 8:
            word_list.append(word)
    
      # Add your function generate_password here
      # It should return a string consisting of three random words 
      # concatenated together without spaces
      def generate_password() :
          return random.choice(word_list) + random.choice(word_list) + random.choice(word_list)
    
      # test your function
      print(generate_password())
    

    运行 $ python password_generator.py, 随机输出三个单词连成的字符串

学习链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值