国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python入門教程1. 基本運算【四則運算、變量、math模塊等】

2020-01-04 14:12:14
字體:
來源:轉載
供稿:網友

在熟悉了Python的基本安裝與環境配置之后,我們來看看Python的基本運算操作。

1. 基本運算

>>>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

2. 變量與變量類型

>>>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'>>>>

3. 專業計算模塊:math

 

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~

未完待續~~歡迎討論!!


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莆田市| 新兴县| 宝兴县| 游戏| 沁阳市| 清原| 利辛县| 连州市| 永靖县| 泊头市| 安岳县| 界首市| 枣庄市| 呼和浩特市| 黄骅市| 玉屏| 临澧县| 沁源县| 昭苏县| 保德县| 满洲里市| 静宁县| 大洼县| 横山县| 旬阳县| 运城市| 康平县| 仁寿县| 昂仁县| 岱山县| 万荣县| 古田县| 青铜峡市| 汪清县| 恭城| 田阳县| 翁牛特旗| 类乌齐县| 宜黄县| 永寿县| 永州市|