1.自定义模块及调用
模块一:
name = 'yyy'
def info():
print('day day up')
模块二:
name = 'lll2'
age = 16
def info():
print('day up 2')
函数
import my_info
print(my_info.name)
my_info.info()
print('-'*40)
import my_info as a
print(a.name)
a.info()
print('-'*40)
from my_info import name
print(name)
print('-'*40)
from my_info import *
print(name)
info()
# 若函数同名,后面的函数会将前面的覆盖
import my_info
import my_info2
print(my_info.name)
my_info2.info()
2.包
learning1:
import admin.Jan as a
print(a.name)
print('-'*40)
from admin import Jan as b
b.info()
# 导入admin包,init代码只执行一次
print('-'*40)
from admin.Jan import name
print(name)
print('-'*40)
from admin.Jan import *
info()
主程序运行
用main 后,阻止代码运行
3. 内置模块
random
import random
random.seed(10)
print(random.random())
print(random.random())
random.seed(10)
print(random.randint(1,100))
for i in range(10):
print(random.randrange(1, 20, 3))
print('-'*40)
lst = [i for i in range(1, 11)]
print(random.choice(lst))
random.shuffle(lst)
print(lst)
time
import time
now = time.time()
print(now) # 时间戳
obj = time.localtime()
print(obj)
obj2 = time.localtime(60)
print(obj2)
print(obj2.tm_year)
print(obj2.tm_wday) # [0,6]表示周一到周天
print(time.ctime())
print(time.strftime('%Y-%m-%d', time.localtime()))
print(time.strftime('%H:%M:%S', time.localtime()))
print(time.strftime('%B', time.localtime()))
print(time.strftime('%A', time.localtime()))
print(time.strptime('2009-09-09', '%Y-%m-%d'))
time.sleep(5) # 程序等待5s,后继续执行
print('hello world')
datetime
from datetime import datetime
dt = datetime.now()
print(dt)
# datetime 是一个类,手动创造这个类的对象
dt2 = datetime(2050, 10, 1, 2, 1)
print(dt2, type(dt2))
print(dt2.year)
# 可以比较两个datetime的大小
# 与字符串之间的转换
nowdt = datetime.now()
nowdt_str = nowdt.strftime('%Y/%m/%d %H %M %S')
print(type(nowdt), nowdt)
print(type(nowdt_str), nowdt_str)
timedelta
from datetime import datetime
from datetime import timedelta
delta1 = datetime(2030,10,1)-datetime(2028,5,5)
print(type(delta1), delta1)
# 通过传入参数的方式创建一个timedelta对象
td1 = timedelta(10, 33)
print(td1)
标签:info,python,random,datetime,模块,time,print,import
From: https://blog.csdn.net/m0_67249562/article/details/140545827