在熟悉了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 = 'm.survivalescaperooms.com'>>> 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 | 圓周率π的值(常量) |
>>> abs(-2) #求絕對值(系統函數)2>>> pow(2,4) #計算2的4次方(系統函數)16.0>>> round(3.4) #round四舍五入運算(系統函數)3>>> round(3.5) #round四舍五入運算4>>> import math #使用import語句可以引入math模塊進行運算>>> dir(math) #查看庫中所有東西['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']>>> piTraceback (most recent call last): File "<pyshell#1>", line 1, in <module> piNameError: name 'pi' is not defined>>> math.pi3.141592653589793>>> from math import *>>> pi3.141592653589793>>>>>> sqrt(9) #sqrt計算開方3.0>>> ceil(3.1) #ceil向上取整4>>> floor(3.9) #floor向下取整3>>> fmod(7,4) # fmod取余數3.0
簡單入門教程~
基本一看就懂~O(∩_∩)O~
未完待續~~歡迎討論!!
新聞熱點
疑難解答