在熟悉了Python的基本安裝與環境配置之后,我們來看看Python的基本運算操作。
>>>6 # 這里的‘#'是注釋符號,不參與運算6>>>666666666666666 #整數類型,原樣輸出666666666666666>>>3.14 #浮點數類型3.14>>>id(6) #id()函數用于查看內存地址1409471616>>>help(id) #help()函數可用于查看函數文檔Help on built-in function id in module builtins:id(obj, /) Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (CPython uses the object's memory address.)>>> 5+16>>>5.0+1 #這里運算結果會自動轉換為浮點型6.0>>>10/25.0>>>10/3 #這里由于計算機是將數字轉換為二進制進行計算時,浮點數轉換偏差造成的3.3333333333333335>>>2.5*25.0>>>2.5**2 #符號**用指數計算,例如這里計算2.5的2次方6.25>>>5//2 # 符號//可用于計算相除的結果再進行取整2>>>5%2 #取余,沒啥好說的1>>>5.0%2 #浮點數的取余運算,同理1.0>>>(5 + 6) * 2 - 2 ** 3 + 5//2 - 5 % 3 #綜合計算(表達式計算)14
>>>a=6 #變量定義與賦值>>>a6>>>b = 3*a #變量運算與賦值>>>b18>>>type(a) #type函數用于檢測變量類型<class 'int'>>>> b = True #布爾類型<class 'bool'>>>> c = 3.14 #浮點數類型>>> type(c)<class 'float'>>>> d = 'www.jb51.net'>>> type(d)<class 'str'>>>> e = ['a','b','c'] #列表類型>>> type(e)<class 'list'>>>> f = ('x','y','z') #元組類型>>> type(f)<class 'tuple'>>>> g = {'a':'1','b':'2','c':'3'} #字典類型>>> type(g)<class 'dict'>>>>
sin(x) | 求x的正弦 |
cos(x) | 求x的余弦 |
asin(x) | 求x的反正弦 |
acos(x) | 求x的反余弦 |
tan(x) | 求x的正切 |
atan(x) | 求x的余切、反正切 |
hypot(x,y) | 求直角三角形的斜邊長 |
fmod(x,y) | 求x/y的余數 |
ceil(x) | 取不小于x的最小整數(向上取整) |
floor(x) | 取不大于x的最大整數(向下取整) |
fabs(x) | 求絕對值 |
exp(x) | 求e的x次冪 |
pow(x,y) | 求x的y次冪 |
log10(x) | 求x以10為底的對數 |
sqrt(x) | 求x的平方根 |
pi | 圓周率π的值(常量) |
新聞熱點
疑難解答