Python——入门级(import 模块)

import 的各种方法

1. 直接引入

import timeimport time 模块,这个模块可以python自带,也可以是自己安装的,比如以后会用到numpy这些模块,需要自己安装。

import time
print(time.localtime())  #这样就可以print 当地时间了
""""
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自己的模块

  • 自建一个模块
    模块写好后保存在默认文件夹: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)

-- 调用自己的模块
新开一个脚本,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
""""
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容