python-20191222

1.random模块

import random  # 调用random模块

a = random.random()  # 随机从0-1之间抽取一个小数
print(a)

a = random.randint(0,100)  # 随机从0-100之间抽取一个数字
print(a)

a = random.choice('abcdefg')  # 随机从字符串/列表/字典等对象中抽取一个元素(可能会重复)
print(a)

a = random.sample('abcdefg', 3) # 随机从字符串/列表/字典等对象中抽取多个不重复的元素
print(a)

items = [1, 2, 3, 4, 5, 6]  # “随机洗牌”,比如打乱列表
random.shuffle(items)
print(items)

2.可以使用dir()函数查看一个模块,看看它里面有什么变量、函数、类、类方法

  • dir(x),可以查询到x相关的函数,x可以是模块,也可以是任意一种对象

a = ''  # 设置一个字符串
print('字符串:')
print(dir(a))    # 把字符串相关的函数展示出来

a = []  # 设置一个列表
print('列表:')
print(dir(a))    # 把列表相关的函数展示出来

a = {}  # 设置一个字典
print('字典:')
print(dir(a))  # 把字典相关的函数展示出来

  • # dir()函数会得到一个列表,用for循环一行行打印列表比较直观

import csv
for i in dir(csv):
    print(i)

3.模块自学

中文网址:https://yiyibooks.cn/xx/python_352/library/csv.html#module-csv

英文网址:https://docs.python.org/3.6/library/csv.html

4.获取当前时间并且用指定格式显示

 

 time获取当前时间戳
 now = int(time.time())     # 1533952277
 timeArray = time.localtime(now)
 print timeArray
 otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
 print otherStyleTime  

Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)

Python time localtime() 函数类似gmtime(),作用是格式化时间戳为本地的时间

Python time strftime() 函数接收以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定

import time

print "time.time(): %f " %  time.time()
print time.localtime( time.time() )
print time.asctime( time.localtime(time.time()) )

以上实例输出结果为:

time.time(): 1234892919.655932
(2009, 2, 17, 10, 48, 39, 1, 48, 0)
Tue Feb 17 10:48:39 2009

python中时间日期格式化符号:

  • %y 两位数的年份表示(00-99)
  • %Y 四位数的年份表示(000-9999)
  • %m 月份(01-12)
  • %d 月内中的一天(0-31)
  • %H 24小时制小时数(0-23)
  • %I 12小时制小时数(01-12)
  • %M 分钟数(00=59)
  • %S 秒(00-59)
  • %a 本地简化星期名称
  • %A 本地完整星期名称
  • %b 本地简化的月份名称
  • %B 本地完整的月份名称
  • %c 本地相应的日期表示和时间表示
  • %j 年内的一天(001-366)
  • %p 本地A.M.或P.M.的等价符
  • %U 一年中的星期数(00-53)星期天为星期的开始
  • %w 星期(0-6),星期天为星期的开始
  • %W 一年中的星期数(00-53)星期一为星期的开始
  • %x 本地相应的日期表示
  • %X 本地相应的时间表示
  • %Z 当前时区的名称
  • %% %号本身

​​​​​​​5.print函数

info = '请专注任务,还要保持专注 ' + str(t) + ' 秒哦!'
print(info,end="")
print("\b"*(len(info)*2),end="",flush=True)

"\b"表示退格,字符串长度 1个中文字符 = 2个英文字符(占位),所以*2

len() 方法返回对象(字符、列表、元组等)长度或项目个数。


print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值