python常用模块

random #用于生成随机数

In [1]:
import random #用于生成随机数
In [3]:
random.random()#随意生成
Out[3]:
0.5923203537141584
In [4]:
random.uniform(1,2)#制定区间
Out[4]:
1.0999623761862738
In [8]:
random.randint(1,3)#生成整数
Out[8]:
1
In [9]:
random.randrange(10,100,2)#制定步长

Out[9]:

12
In [12]:
random.choice([1,2,3,4,5])#序列中随机选择一个.一定要有序数列
Out[12]:
5
In [16]:
p=[1,2,3,4,5]
random.shuffle(p)#将一个列表中的元素打乱
p
 
Out[16]:
[4, 1, 2, 5, 3]
 

time #用于处理时间

In [17]:
import time #用于处理时间
In [18]:
time.time()#返回当前时间的时间戳(1970年后的浮点秒数)
Out[18]:
1572419713.7745376
In [19]:
time.localtime()#返回本地的当前时间
Out[19]:
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=30, tm_hour=15, tm_min=16, tm_sec=8, tm_wday=2, tm_yday=303, tm_isdst=0)
In [23]:
t=time.localtime()
time.asctime(t)#接受一个时间元组,并返回一个可读的形式,注:时间戳不可以
Out[23]:
'Wed Oct 30 15:18:32 2019'
In [24]:
time.ctime()#返回当前时间
Out[24]:
'Wed Oct 30 15:19:50 2019'
In [25]:
import datetime #获取datetime格式的时间
In [29]:
datetime.datetime.now()#获取当前时间
Out[29]:
datetime.datetime(2019, 10, 30, 15, 22, 30, 869765)
In [30]:
datetime.date.today()#获取当天日期
Out[30]:
datetime.date(2019, 10, 30)
 

pymysql#用于连接数据库 pip install pymysql

In [32]:
import pymysql#用于连接数据库 pip install pymysql
In [ ]:
#conn=pymysql.connect("LOCALHOST","账号","密码",数据库名)#用于连接数据库
# conn
#cur=conn.cursor()# 创建游标
#cur.execute(sql语法)
#lis=cur.fetchall()#返回所有元组
#cur.close()#关闭
原文地址:https://www.cnblogs.com/Koi504330/p/11909181.html