import 的各種方法
1. 直接引入
import time
指import time
模塊样屠,這個模塊可以python自帶,也可以是自己安裝的雪标,比如以后會用到numpy
這些模塊,需要自己安裝讼渊。
import time
print(time.localtime()) #這樣就可以print 當(dāng)?shù)貢r間了
""""
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=14, tm_min=12, tm_sec=48, tm_wday=4, tm_yday=358, tm_isdst=0)
""""
2. import time as __
import time as __
,__
下劃線縮寫部分可以自己定義,在代碼中把time
定義成 t
.
import time as t
print(t.localtime()) # 需要加t.前綴來引出功能
""""
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=14, tm_min=12, tm_sec=48, tm_wday=4, tm_yday=358, tm_isdst=0)
""""
3. from time import time,localtime ,只import自己想要的功能.
from time import time, localtime
print(localtime())
print(time())
""""
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=14, tm_min=41, tm_sec=38, tm_wday=4, tm_yday=358, tm_isdst=0)
1482475298.709855
""""
4. from time import * 輸入模塊的所有功能
from time import *
print(localtime())
""""
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=14, tm_min=41, tm_sec=38, tm_wday=4, tm_yday=358, tm_isdst=0)
""""
import自己的模塊
-
自建一個模塊
模塊寫好后保存在默認(rèn)文件夾:balance.py
d=float(input('Please enter what is your initial balance: \n'))
p=float(input('Please input what is the interest rate (as a number): \n'))
d=float(d+d*(p/100))
year=1
while year<=5:
d=float(d+d*p/100)
print('Your new balance after year:',year,'is',d)
year=year+1
print('your final year is',d)
-- 調(diào)用自己的模塊
新開一個腳本恃鞋,import balance
import balance
""""
Please enter what is your initial balance:
50000 # 手動輸入我的本金
Please input what is the interest rate (as a number):
2.3 #手動輸入我的銀行利息
Your new balance after year: 1 is 52326.45
Your new balance after year: 2 is 53529.95834999999
Your new balance after year: 3 is 54761.14739204999
Your new balance after year: 4 is 56020.653782067144
Your new balance after year: 5 is 57309.12881905469
your final year is 57309.12881905469
""""