Python入門教程1. 基本運(yùn)算【四則運(yùn)算、變量、math模塊等】 原創(chuàng)
原創(chuàng) 更新時(shí)間:2018年10月28日 23:03:35 原創(chuàng) 作者:chenge
這篇文章主要介紹了Python教程的基本運(yùn)算,包括四則運(yùn)算、變量的使用與類型檢測、math模塊等,并附帶了相關(guān)說明,代碼備有較為詳盡的說明,便于理解,需要的朋友可以參考下
在熟悉了Python的基本安裝與環(huán)境配置之后,我們來看看Python的基本運(yùn)算操作。
1. 基本運(yùn)算
>>>6 # 這里的‘#'是注釋符號,不參與運(yùn)算 6 >>>666666666666666 #整數(shù)類型,原樣輸出 666666666666666 >>>3.14 #浮點(diǎn)數(shù)類型 3.14 >>>id(6) #id()函數(shù)用于查看內(nèi)存地址 1409471616 >>>help(id) #help()函數(shù)可用于查看函數(shù)文檔 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+1 6 >>>5.0+1 #這里運(yùn)算結(jié)果會(huì)自動(dòng)轉(zhuǎn)換為浮點(diǎn)型 6.0 >>>10/2 5.0 >>>10/3 #這里由于計(jì)算機(jī)是將數(shù)字轉(zhuǎn)換為二進(jìn)制進(jìn)行計(jì)算時(shí),浮點(diǎn)數(shù)轉(zhuǎn)換偏差造成的 3.3333333333333335 >>>2.5*2 5.0 >>>2.5**2 #符號**用指數(shù)計(jì)算,例如這里計(jì)算2.5的2次方 6.25 >>>5//2 # 符號//可用于計(jì)算相除的結(jié)果再進(jìn)行取整 2 >>>5%2 #取余,沒啥好說的 1 >>>5.0%2 #浮點(diǎn)數(shù)的取余運(yùn)算,同理 1.0 >>>(5 + 6) * 2 - 2 ** 3 + 5//2 - 5 % 3 #綜合計(jì)算(表達(dá)式計(jì)算) 14
2. 變量與變量類型
>>>a=6 #變量定義與賦值
>>>a
6
>>>b = 3*a #變量運(yùn)算與賦值
>>>b
18
>>>type(a) #type函數(shù)用于檢測變量類型
<class 'int'>
>>> b = True #布爾類型
<class 'bool'>
>>> c = 3.14 #浮點(diǎn)數(shù)類型
>>> type(c)
<class 'float'>
>>> d = 'www.dhdzp.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. 專業(yè)計(jì)算模塊: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的余數(shù) |
ceil(x) |
取不小于x的最小整數(shù)(向上取整) |
floor(x) |
取不大于x的最大整數(shù)(向下取整) |
fabs(x) |
求絕對值 |
exp(x) |
求e的x次冪 |
pow(x,y) |
求x的y次冪 |
log10(x) |
求x以10為底的對數(shù) |
sqrt(x) |
求x的平方根 |
pi |
圓周率π的值(常量) |
>>> abs(-2) #求絕對值(系統(tǒng)函數(shù)) 2 >>> pow(2,4) #計(jì)算2的4次方(系統(tǒng)函數(shù)) 16.0 >>> round(3.4) #round四舍五入運(yùn)算(系統(tǒng)函數(shù)) 3 >>> round(3.5) #round四舍五入運(yùn)算 4 >>> import math #使用import語句可以引入math模塊進(jìn)行運(yùn)算 >>> 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'] >>> pi Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> pi NameError: name 'pi' is not defined >>> math.pi 3.141592653589793 >>> from math import * >>> pi 3.141592653589793 >>>>>> sqrt(9) #sqrt計(jì)算開方 3.0 >>> ceil(3.1) #ceil向上取整 4 >>> floor(3.9) #floor向下取整 3 >>> fmod(7,4) # fmod取余數(shù) 3.0
簡單入門教程~
基本一看就懂~O(∩_∩)O~
未完待續(xù)~~歡迎討論?。?/p>
相關(guān)文章
Python編寫運(yùn)維進(jìn)程文件目錄操作實(shí)用腳本示例
這篇文章主要為大家介紹了Python編寫實(shí)用運(yùn)維進(jìn)程文件目錄的操作腳本示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
如何使用 Python和 FFmpeg 批量截圖視頻到各自文件夾中
wxPython 提供了一個(gè)簡單易用的界面,而 FFmpeg 則負(fù)責(zé)處理視頻幀的提取,這個(gè)工具不僅對視頻編輯工作有幫助,也為批量處理視頻文件提供了極大的便利,這篇文章主要介紹了使用 Python和 FFmpeg 批量截圖視頻到各自文件夾中,需要的朋友可以參考下2024-08-08
Python實(shí)現(xiàn)進(jìn)程同步和通信的方法
本篇文章主要介紹了Python實(shí)現(xiàn)進(jìn)程同步和通信的方法,詳細(xì)的介紹了Process、Queue、Pipe、Lock等組件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01

