python知識整理(模組及continue brak及try異常處理)_6
#!/usr/bin/env python
# -- conding:utf-8 --
import 模組
四個方法:
import time 是指 import time 模組:
這個模組可以是 python 自帶,也可以是自己安裝的, 如 numpy 模組,就是需要自己安裝;
1、 improt time
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)
2、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)
3、from time import time,localtime // 只 import 自己想要的功能, 中間用逗號隔開.
from time import time,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)
4、form time import * // 輸入模組的所有功能
from time import *
In [11]: tim
%%time %%timeit %time %timeit time timezone
In [13]: print(time())
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','r+')
except Exception as e:
print('There is not file as fenye')
response = raw_input('do you want to create new file: ')
re = response.lower()
if re == 'y' or 'yes':
file = open('fenye','w')
elif re == 'n' or 'no':
exit
else:
print('Please input a [Yes/No]: ') continue
else:
file.write('my name is fenye')