zoukankan      html  css  js  c++  java
  • 第十一天

    第一部分 模块
    一、 模块
    一个py文件就是一个模块,文件从物理角度,模块从逻辑的角度
    一个模块中可能存在 定义的类、函数、变量
    模块的好处:
    1. 有利于把项目按照功能进行划分
    2. 模块提供了不同的独立的命名空间(全局命名空间),解决命名冲突问题
    3. 模块可以提供多人使用,提高了代码的复用性,可以自己维护自己的代码
    """
    a=1
    二、 模块的使用
    # 模块和模块之间的交互
    # 通过模块的导入来解决模块之间的相互交互
    # 导入的方式:两种 import 导入, from 模块 import 导入
    1. import 导入
    # 格式:import 模块, 如果有多个模块,可以使用“,”分隔。
    # 当import了一个模块之后,计算机会把该模块的语句执行一次,但是只执行一次
    # 使用导入的模块:【模块名】.【名称 】
    # import math
    # print(math.pow(10,2))
    # import another
    # import another import导入只能执行一次,如果重复导入,不再执行导入模块的代码
    # print(another.a)
    # another.b()
    # 自己定义的变量不会跟导入模块的名字冲突,因为导入的模块的名字有【模块名】
    a=1
    print(a)
    2. from 模块 import 名字1, 名字2 只导入某一个模块下的几个名字
    # form 也会把模块执行一次
    # 使用模块下的名字 : 直接使用import 后面的名字即可
    # 使用的时候,要注意,不要跟自己模块内部定义的变量名冲突,否则会造成相互覆盖的结果,后者覆盖前者
    print(a)
    a=1
    print(a)
    from another import a,b
    print(a)
    b()
    # from 模块 import * :会将模块中所有的名字全部导入。慎用,因为造成跟本文件中的名称冲突的可能性比较大

    # 练习:自定义一个模块A,有变量、方法,在另一个模块B中对A中的成员进行访问

    3. dir函数:返回指定对象相关绑定的名称。如果不写参数,默认是当前作用域下的名称
    import another
    print(dir())
    from another import a,b
    print(dir())
    print(dir(another))
    4. 模块的别名
    # 当导入模块是或者直接导入的是模块中的名称,都可以使用as为名称器别名
    # 当起了别名之后,原名就不起作用了。
    # 语法
    import another as an
    print(an.a)
    # print(another.a)
    from another import a as new_a , b as b_new
    print(new_a)
    print(a)
    b_new()
    # 别名的好处:1. 可以解决当前文件和导入模块中的名称冲突
    # 2. 如果原来的名字很长,可以进行简化


    三、 隐藏模块的数据
    # 原因:from another import *
    # 一定注意,隐藏模块数据的方式,只对from 模块这种情况起作用,对import的情况不起作用
    # 对不导入的模块内容进行标注,标注的方式两种:
    1. 在名字前面是_,模块被导入的时候,就不会被import进入其他的模块
    # from another import *
    # print(a)
    # print(_w)

    2. 在__all__列表,表明能被导入的名字
    from another import *
    print(a,b)
    print(c)
    print(_w)

    # 当1,2冲突时,当__all__指定了能够被导入的名字之后,以__all__为准

    四、 __name__
    import another
    # __name__:显示当前模块的名称,属性
    print(__name__)
    """
    模块运行的时候有两种方式:
    1. 作为脚本运行,在后台使用python xx.py 返回__name__固定是__main__
    2. 被其他模块导入的时候,会执行被导入的模块 返回的是模块的真名
    """
    # 当在模块中加入if__name__ == "__main__" : 判断之后,
    # 该模块被其他模块导入的时候,不会执行if之后的语句

    五、 模块的搜索路径
    import sys
    """
    路径:
    1. 内建解释器中 buildin
    2. 作为脚本执行模块的路径
    3. pythonpath的环境变量
    4. python的安装路径(lib库)
    """
    import random
    # 注意,在给模块py文件起名字的时候一定避免起lib库中一样,
    # 虽然可以重名,但是尽量不要这样用

    六、模块的缓存
    # python的缓存文件:在模块(自定义的模块)被导入的时候,产生缓存文件
    # 字节码文件 文件的位置:模块所在的文件路径下。扩展名pyc
    """
    说明:
    1. 缓存字节码文件只能加快加载速度,不提高模块的运行速度
    2. 只有导入的模块才会产生字节码文件,作为脚本运行时没有,
    lib中和内建包中的模块,被导入是,产生的字节码
    3. 字节码文件可以脱离源文件运行
    """
    第二部分 包
    """

    包也能提供独立的命名空间,允许有子包,可以将模块分放到不同的包
    """
    一、 导入包
    # 语法跟导入模块基本一样
    # import 包名
    # import 包名.(子包).模块名
    # import day11.another as an
    # from 包 import 模块
    # from day11 import another
    # from day11.another import a

    # 使用 包名.模块名.名字
    # day11.another.a

    # 如果使用包名.模块名.名字,比较长,可以使用别名简化调用
    # print(an.a)

    二、 __init__.py
    # 创建python的开发包时就会创建__init__文件。
    # 只有具有init文件的开发包,才能创建可运行的py文件
    # 只要运行开发包中的任何一个模块,我们都会运行__init__.py文件中的内容
    # 作用:对于整个包来说,起到初始化信息的作用

    三、 __all__ 变量
    # 在init中可以通过定义__all__变量的形式,确定使用from形式导入时,允许导入哪些名称
    from day11 import *

    第三部分 math
    """
    math模块 数学模块
    """
    import math
    # 返回圆周率
    print(math.pi)
    # 返回数学常数e
    print(math.e)
    # ceil:返回大于等于x的最小整数 向上取整
    print(math.ceil(5.3))
    print(math.ceil(-5.3))
    # floor():向下取整
    print(math.floor(5.3))
    print(math.floor(-5.3))

    # 返回e的x幂
    print(math.exp(2))
    #e**x

    # 返回x的y次幂
    print(math.pow(10,2))
    # x**y

    # log(x,base),返回以base为底,x的对数
    # 如果不写base是以e为底
    print(math.log(30))
    print(math.log(30,2))

    # 返回浮点数的绝对值
    abs(-100)
    print(math.fabs(-2.43))

    # 返回x的阶乘
    print(math.factorial(10))

    # fmod 取余
    print(math.fmod(-10,3))

    # 求累积和
    print(math.fsum([1,2,3,4,5]))

    print(sum([1,2,3,4,5]))

    # 最大公约数
    print(math.gcd(12,15))

    # 返回平方根
    print(math.sqrt(16))

    第四部分 random
    """
    random模块
    """
    import random
    # 随机生成一个0-1之间的浮点数,包括0,不包括1
    print(random.random())
    # 随机:伪随机,是人为的符合正态分布的,符合概率的一个函数。
    # while random.random != 0:
    # pass

    # random.randint(a,b),可以随机返回一个a-b之间的整数,既包含a也包含b
    # 在使用的时候,start<end,否则会报错
    print(random.random(1,5))
    # print(random.randint(5,1))

    # random.randrange(start,stop,step)
    # 生成从start开始到stop(不包括stop)随机数,step默认值1
    print(random.randrange(1,3))
    print(random.randrange(1,10,2))
    # print(random.randrange(1,10,-2))


    # random.uniform(a,b) 返回a和b之间的浮点数,包括a和b
    # a和b的大小可以互换
    print(random.uniform(1.5,2.3))
    print(random.uniform(2.3,1.5))

    # choices()3.6中,随机选取n个数
    # p:从哪些数据里面选取
    # weights:权重
    # 累计权重
    # random.choice([1,2,3,4,5])

    # sample,随机抽取k个值,对于取出来的值,不再返回原来的列表中。
    print(random.sample([1,2,3,4,5],3))

    # shuffle,按照random产生的随机数洗牌, 就地洗牌,改变原有序列
    li=[1,2,3,4,5,6]
    random.shuffle(li)
    print(li)

    第五部分 time
    """
    时间模块 time
    """
    import time
    # timezone 返回的是与utc时间(本初子午线)相差的秒数
    print(time.timezone)
    # time():从新纪元(纪念unix产生的时间1970.1.1)到当前时间走过的秒数
    print(time.time())

    # localtime(参数)
    # 返回从新纪元走过的秒数之后的(本地)时间,如果不写参数,返回的是当前时间
    # 返回的是时间元组
    print(time.localtime())
    print(time.localtime(1))
    dt=time.localtime()
    print(dt.tm_year)

    # gmtime:返回的是从新纪元走过的秒数之后的utc时间,如果不写参数,返回的是当前时间
    print(time.gmtime())
    print(time.gmtime(1))

    # mktime(tuple):将元组转换成新纪元到元组指定时间走过的秒数
    # 跟localtime正好相反
    print(time.mktime((dt)))
    print(time.localtime(1527233739))

    # asctime(tuple):将时间元组转换成字符串的形式
    print(time.asctime(dt))

    # ctime(秒):从新纪元走过的毫秒数,转换成本地时间str
    # 如果没有指定参数,可以返回当前时间的str形式
    print(time.ctime())
    print(time.ctime(1))

    # sleep让程序暂停指定的秒数
    # 指定的秒数不是确定的
    print("睡觉之前")
    time.sleep(1)
    print("睡觉之后")

    # clock(),在unix里面和window中返回的不一样,所以后来不太被使用
    # 在window,第一次调用,返回的是cpu的计算时间,
    # 第二次调用,返回的是距离第一次调用该函数所经历的时间
    print(time.clock())
    time.sleep(1)
    print(time.clock())
    time.sleep(1)
    print(time.clock())

    # time.perf_counter() 返回当前进程下,系统以及用户的cpu计算时间,包含调用sleep的时间
    start=time.perf_counter()
    time.sleep(1)
    end=time.perf_counter()
    print(end-start)

    # time.process_time():跟per_counter函数使用方式基本上一致,不包含sleep调用的时间
    start=time.perf_counter()
    time.sleep(1) # cpu在睡觉,所以不计入时间
    end=time.perf_counter()
    print(end-start) # 0.0计算时间一样

    # strftime:格式化时间,把时间元组按照format指定的格式转换成字符串
    print(time.strftime("%Y/%m/%d/%H:%M:%S",time.localtime()))
    # H:24小时 I:12小时 M:分钟 S:秒

    # strptime:按照指定的format格式,将string转换成时间元组
    print(time.strptime("2018/05/25 15:56:37","%Y/%m/%d/%H:%M:%S"))

    第六部分 datetime
    """
    datetime模块
    提供 date , time , datetime
    """
    from datetime import date
    一. date类
    # date(year,month,day):提供指定年月日参数,获得指定的日期
    # 构造器
    print(date(2019,5,6))

    # 实例属性
    # year:返回年 month:返回月, day:返回日
    d=date(2019,2,5)
    print(d.year,d.month,d.day)

    # 类属性
    # max min:最大/最小的date对象,相当于定义常量
    print(date.max,date.min)
    # resolution:两个不同date对象相差的最小距离
    print(date.resolution)

    # 实例方法
    d=date(2018,5,25)
    # 1. ctime():返回的是特定格式的字符串str
    print(date.ctime(d))

    # 2. replace(年,月,日),返回新的date对象, 不是就地改动
    print(d.replace(year=2019))
    print(d.replace(day=26))

    # 3. timetuple():返回时间元组 类似于time.localtime()
    print(d.timetuple())

    # 4. weekday(): 返回当前日期是星期几,0-6 0星期一 6星期天
    print(d.weekday())

    # 5. toordinal返回当前日期的序数, 1年1月1日
    print(d.toordinal())

    # 6. strftime:根据format格式显示当前的日期对象
    d=date(2018,5,26)
    date.strftime(d,"%Y/%m%d")

    # 7.today() 返回当前的日期
    print(date.today())

    # 8. fromtimestamp(timestamp):参数变成指定的时间戳(秒),变成日期
    print(date.fromtimestamp(0))
    print(date.fromtimestamp(3600*24))

    # 9.fromordinal:根据时间序数,返回对应的日期
    date.fromordinal(100)

    二、 time类 针对时间的操作
    # 1. 根据指定的时间返回真正的时间
    from datetime import time
    print(time(16,42,50))
    t=time(16,43,50)
    # 2. hour minute等属性
    print(t.hour)
    print(t.minute)
    print(t.second)
    print(t.microsecond)

    # 3. max/min 最大和最小的时间
    print(time.max)
    print(time.min)

    # 4. time.resolution:两个时间对象间隔的距离 微秒
    print(time.resolution)

    # 5. replace 可以对时分秒微秒进行替换
    t=time(3,4,5)
    print(t)
    print(t.replace(hour=6,minute=9))

    # 6. strftime(format):根据format格式化当前时间,把时间变成符号格式的字符串
    print(time.strftime(t,"%H:%M:%S"))

    三、 datetime 日期和时间
    from datetime import datetime
    1. 构造方法
    dt=datetime(1,2,3,4,5,6)
    print(dt)

    2. 属性
    print(dt.year)
    print(dt.month)
    print(dt.microsecond)

    3. max min
    print(datetime.max)
    print(datetime.min)

    4. resolution:两个时间对象的差距
    print(datetime.resolution)

    5. 实例方法
    dt=datetime(1,2,3,4,5,6)
    # date方法返回年月日
    print(dt.date())
    # time()返回时分秒
    print(dt.time())

    # ctime()
    print(dt.ctime())

    # replace() 替换年月日时分秒,微秒
    print(dt.replace(year=11,microsecond=11))

    # timetuple(),返回的是时间元组time.localtime()
    print(dt.timetuple())

    # weekday 返回的是星期
    print(dt.weekday())

    # dt.toordinal():返回的是日期的序数
    print(dt.toordinal())

    # strftime :指定时间转换成str(按照format)
    print(datetime.strftime(dt,"%Y-%m-%d %H:%M:%S"))

    # today
    print(datetime.today())

    # now() 返回当前的日期和时间
    print(datetime.now)

    # 显示的utc时间
    print(datetime.utcnow())

    # strptime
    datetime.strptime("2018-05-25 09:05:15","%Y-%m-%d %H:%M:%S")

    第七部分 日历
    import calendar
    cal=calendar.month(2018,5)
    print(cal)
    # w: 每日宽度间隔的字符
    # l: 每星期的行数
    # c: 3个月是一行,间隔距离
    calendar.calendar(2018,w=2,l=1,c=6)




  • 相关阅读:
    进程对象的属性或方法详解
    进程理论以及开启子进程的两种方式
    计算机发展史(多道技术)
    基于socketserver实现的并发(tcp和udp)
    基于udp协议的套接字及udp协议粘包问题
    模拟ssh的远程网络传输
    周考题目及答案
    c/s架构搭建
    网络编程基础
    10.16模拟赛(湖南集训)
  • 原文地址:https://www.cnblogs.com/ztx695911088/p/9090898.html
Copyright © 2011-2022 走看看