天天看點

Ptython入門學習:子產品導入自定義函數與 時間子產品練習Python 日期和時間:

目錄

         Python 日期和時間:​

Python 第三方子產品:

Python 日期和時間:

      Python 的 time 子產品下有很多函數可以轉換常見日期格式。

      如函數time.time()用于擷取目前時間戳

import time                        #
import datetime                   #


 #擷取目前日期和時間
now1=datetime.datetime.now()
print(type(now1),now1)

now2= time.strftime("%Y-%m-%d-%H-%M-%S")     # 年 月 日 時 分 秒
print(type(now2), now2)


print(date.today())                          #當天日期


#擷取一個指定的日期
d=datetime.datetime(2016,2,29,5,23,12)
print(d)


 #日期轉字元串
now=datetime.datetime.now()
strnow=now.strftime("%Y-%m-%d %H:%M:%S")             
print(type(strnow),strnow)
           

Ptython入門學習:子產品導入自定義函數與 時間子產品練習Python 日期和時間:

Python 第三方子產品:

PIL(Python Image Library)是python的第三方圖像處理庫,但是由于其強大的功能與衆多的使用人數,幾乎已經被認為是python官方圖像處理庫了

from PIL import Image,ImageFilter              #引入 PIL子產品
pic_in=input('請導入圖檔位置:')

im=Image.open(pic_in)

im2=im.filter(ImageFilter.BLUR)               #執行 模糊濾鏡

im2.save('f:/001.jpg')                       # 儲存檔案
 
           

自定義函數:把

math.sqrt()

的函數定義儲存為

math.sqrt.py

檔案,那麼,可以在該檔案的目前目錄下啟動Python解釋器,用

from math.sqrt import quadratic

來導入

math.sqrt()

函數,注意

abstest

是檔案名(不含

.py

擴充名):

import math

#求解 ax^2+bx+c=0
def quadratic(a,b,c):
    q1=b**2-4*a*c
    if q1>0:
        s1=math.sqrt(q1)
        x1=(-b+s1)/(2*a)
        x2=(-b-s1)/(2*a)
        return x1,x2
    else:
        print('條件不滿足')
           
#from math_sqr import quadratic  #第一種方法導入庫函數
#import math_sqr                 #第二種方法導入庫函數

res=quadratic(2, 10, 4)
print(res)
           
Ptython入門學習:子產品導入自定義函數與 時間子產品練習Python 日期和時間:

繼續閱讀