python學習日記(2) 運算式
運算式
//
#除,但只取整數部分
** #次方
pow(a,b)
# a的b次方
% #取餘數
abs()
#絕對值
round(a,2)
#顯示a參數並4捨5入至小數點第二位
divmod(a,b)
#a除以b的商以及餘數
輸出格式
%d #整數資料
%05d
#5位元的長度,空格補0
%-5d
#5位元的長度,向左對齊
%f #浮點數資料
%s #字串資料
例子:
jjjj=89543
print("my telephone number is %d"
%(jjjj))
my telephone number is 89543
與系統相關的內建函數
dir()
#查詢物件的方法
type()
#查詢物件的類型
匯入模組
import 模組名稱 as 綽號 #整個模組都匯入
from 模組名稱 import 模組裡的函數 as 綽號
#只匯入函數
例子:
雖只匯入函數tan,但如程式中要呼喚模組中其他的函數(如sqrt)時,可以以下列方式呼喚:
from math import tan as ta
math.sqrt(9)
3.0
留言
張貼留言