#!/usr/bin/env python
# -- conding:utf-8 --python
import 模塊
四個方法:linux
import time 是指 import time 模塊:
這個模塊能夠是 python 自帶,也能夠是本身安裝的, 如 numpy 模塊,就是須要本身安裝;app
一、 improt timepython2.7
In [17]: import time // 能夠print 當地時間:
print(time.localtime())
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=1, tm_hour=13, tm_min=48, tm_sec=55, tm_wday=0, tm_yday=182, tm_isdst=0)rest
二、improt time as t // as 後面能夠自定義,表示把time縮寫成 t.
In [2]: import time as t
print(t.localtime())
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=1, tm_hour=13, tm_min=52, tm_sec=0, tm_wday=0, tm_yday=182, tm_isdst=0)orm
三、from time import time,localtime // 只 import 本身想要的功能, 中間用逗號隔開.
from time import time,localtime
print(localtime())
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=1, tm_hour=13, tm_min=56, tm_sec=12, tm_wday=0, tm_yday=182, tm_isdst=0)utf-8
四、form time import * // 輸入模塊的全部功能
from time import *rem
In [11]: tim
%%time %%timeit %time %timeit time timezoneget
In [13]: print(time())
1561960733.32input
In [14]: print(clock())
自建模塊:
[root@fenye2019 ~]# cat m1.py
#!/usr/bin/env python
# --* conding:utf-8 -*-
def printdata(data):
print('I am m1')
print(data)
調用:
improt m1
或者
from m1 import printdata
m1.printdata('python')
import m1
In [6]: m1.printdata('python')
I am m1
python
註釋: linux 使用import 導入模塊/老是錯誤: ImportError: No module named numpy
須要修改其包的路徑: // site-apckages
首先查看包/模塊的位置, // 經過 sys 模塊裏的 path 功能來實現:
import sys
sys.path
'/usr/lib64/python2.7/site-packages/gtk-2.0',
'/usr/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/cloud_init-0.7.6-py2.7.egg',
'/usr/lib/python2.7/site-packages/IPython/extensions',
臨時修改: 經過 path 功能下的 append 屬性來增長
sys.path.
sys.path.append sys.path.extend sys.path.insert sys.path.remove sys.path.sort
sys.path.count sys.path.index sys.path.pop sys.path.reverse
sys.path.append('/usr/lib/python2.7/site-packages')
永久修改:
[root@fenye2019 ~]# tail /etc/profile
export PYTHONPATH=/usr/lib64/python2.7/site-packages:/usr/lib/python2.7/site-packages/IPython/extensions
[root@fenye2019 ~]# source /etc/profile
再次手動寫一個模塊,計算五年的複利本息的模塊:
[root@fenye2019 ~]# cat balance.py
#!/usr/bin/env python
# --*conding:utf-8 -*-
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
Please enter what is your initial balance:
50000 // 手動輸入本金:
Please input what is the interest rate (as a number):
2.4 // 手動輸入銀行利息:
('Your new balance after year:', 1, 'is', 52428.8)
('Your new balance after year:', 2, 'is', 53687.0912)
('Your new balance after year:', 3, 'is', 54975.581388800005)
('Your new balance after year:', 4, 'is', 56294.9953421312)
('Your new balance after year:', 5, 'is', 57646.07523034235)
('your final year is', 57646.07523034235)
continue && break
continue 重複輸入
break 跳出本次循環,執行循環下面的內容
pass // 表示空字符,什麼也不作.
break //表示退出本次循環,執行循環外的內容:
如: 當b = 1, 至關於忽略 break 後面的循環內的那部分代碼,
直接運行 循環以外的 語句;
while True:
b = input('please input number: ')
if b == '1':
break
else:
pass
print('to run ......')
print('flnish run')
python 10.py
please input number: 3
to run ......
please input number: 4
to run ......
please input number: 1
flnish run
continue // 忽略 continue 後面的代碼,而後從新開始循環:
通常用戶從新輸入字符:
while True:
b = input('Please input number: ')
if b == '1':
continue
else:
pass
print('to run ......')
print('flnish run')
python 10.py
please input number: 2
jixunyunx......
please input number: 1
please input number: 1
try 異常處理
file = open('fenye','r') //文件不存在則會報錯
IOError: [Errno 2] No such file or directory: 'fenye'
加上try 後 // 輸出的錯誤能夠寫"自定義的內容"
try:
file = open('fenye',r)
except Exception as e:
print(e)
# print('there is no file name as eeeee') // 這樣自定義內容也能夠,直接打印內容也能夠。
[Errno 2] No such file or directory: 'fenye'
擴展後以下:
判斷這個文件是否存在並查看,不存在則由用戶選擇是否建立:
第一次運行時,文件不存在存在,則執行except 裏面的語句,由用戶選擇是否建立,選擇 y ,則建立這個文件,並退出:
第二次運行時,文件已存在,則直接執行最後一個else裏的語句:
try:
file = open('fenye',r+) //第二次運行寫入文件內容須要些權限 w.
except Exception as e:
print('There is not file named as fenye')
response = raw_input('do you want to create new file: ')
if response == 'y':
file = open('fenye','w')
else:
pass
else: //此處的else同 try 平行:
file.write('my name is fenye')
file.close()
cat fenye
my name is fenye
再次擴展:
假設用戶輸入的是 n ,或者是大寫的 Y/N ,或者是 yes/no Yes/No 這樣的,也或者是其餘字符如何判斷:
try:
...: file = open('fenye.txt','r+')
...: except Exception as e:
...: print('There is not file named as fenye.txt')
...: response = raw_input('do you want to create new file[Yes/No]: ')
...: re = response.lower()
...: if re == 'y' or re == 'yes':
...: file = open('fenye.txt','w')
...: elif re == 'n' or re == 'no':
...: exit
...: else:
...: print('Please input [Yes/No] ......')
...: else:
...: file.write('my name is fenye')
...: file.close()
There is not file named as fenye.txt
do you want to create new file[Yes/No]:
Please input [Yes/No] ......
There is not file named as fenye.txt
do you want to create new file[Yes/No]: y
In [8]: ls
1.sh* fenye.txt