码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习之模块

时间:2019-10-05 18:46:51      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:日期   mon   文件包含   for   sed   alt   choice   %x   min   

模块与包

  • 模块的概念
#在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护。
#为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,
很多编程语言都采用这种组织代码的方式。在Python中,一个.py文件就称之为一个模块(Module)。
  • 模块的好处
#最大的好处是大大提高了代码的可维护性。其次,编写代码不必从零开始。当一个模块编写完毕,就可以被其他地方引用。
我们在编写程序的时候,也经常引用其他模块,包括Python内置的模块和来自第三方的模块。
  • 模块的操作
技术图片
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

 a test module 

__author__ = Michael Liao

import sys

def test():
    args = sys.argv
    if len(args)==1:
        print(Hello, world!)
    elif len(args)==2:
        print(Hello, %s! % args[1])
    else:
        print(Too many arguments!)

if __name__==__main__:
    test()
使用模块

 常用模块

  • time模块

技术图片

技术图片
 1 import time
 2  
 3 # 1 time() :返回当前时间的时间戳
 4 time.time()  #1473525444.037215
 5  
 6 #----------------------------------------------------------
 7  
 8 # 2 localtime([secs])
 9 # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
10 time.localtime() #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=11, tm_hour=0,
11 # tm_min=38, tm_sec=39, tm_wday=6, tm_yday=255, tm_isdst=0)
12 time.localtime(1473525444.037215)
13  
14 #----------------------------------------------------------
15  
16 # 3 gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
17  
18 #----------------------------------------------------------
19  
20 # 4 mktime(t) : 将一个struct_time转化为时间戳。
21 print(time.mktime(time.localtime()))#1473525749.0
22  
23 #----------------------------------------------------------
24  
25 # 5 strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和
26 # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个
27 # 元素越界,ValueError的错误将会被抛出。
28 print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56
29  
30 # 6 time.strptime(string[, format])
31 # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
32 print(time.strptime(2011-05-05 16:37:06, %Y-%m-%d %X))
33  
34 #time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
35 #  tm_wday=3, tm_yday=125, tm_isdst=-1)
36  
37 #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。
时间转化1

 

 技术图片

技术图片
 1 import time
 2 
 3 # 1 asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:‘Sun Jun 20 23:21:05 1993‘。
 4 # 如果没有参数,将会将time.localtime()作为参数传入。
 5 print(time.asctime())#Sun Sep 11 00:43:43 2016
 6  
 7 #----------------------------------------------------------
 8  
 9 # 2 ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
10 # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
11 print(time.ctime())  # Sun Sep 11 00:46:38 2016
12  
13 print(time.ctime(time.time()))  # Sun Sep 11 00:46:38 2016
时间转化2
  • datetime模块
#时间加减
import datetime

# print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
#print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
# print(datetime.datetime.now() )
# print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
# print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
# print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
# print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分


#
# c_time  = datetime.datetime.now()
# print(c_time.replace(minute=3,hour=2)) #时间替换
  • random模块
 1 import random
 2  
 3 print(random.random())#(0,1)----float    大于0且小于1之间的小数
 4  
 5 print(random.randint(1,3))  #[1,3]    大于等于1且小于等于3之间的整数
 6  
 7 print(random.randrange(1,3)) #[1,3)    大于等于1且小于3之间的整数
 8  
 9 print(random.choice([1,23,[4,5]]))#1或者23或者[4,5]
10  
11 print(random.sample([1,23,[4,5]],2))#列表元素任意2个组合
12  
13 print(random.uniform(1,3))#大于1小于3的小数,如1.927109612082716 
14  
15  
16 item=[1,3,5,7,9]
17 random.shuffle(item) #打乱item的顺序,相当于"洗牌"
18 print(item)
技术图片
import random
def make_code(n):
    res=‘‘
    for i in range(n):
        s1=chr(random.randint(65,90))
        s2=str(random.randint(0,9))
        res+=random.choice([s1,s2])
    return res

print(make_code(9))
随机生成验证码

 

Python学习之模块

标签:日期   mon   文件包含   for   sed   alt   choice   %x   min   

原文地址:https://www.cnblogs.com/layblogs/p/11624925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!