python启蒙视频_python启蒙阶段
數(shù)學(xué)運(yùn)算
Python使用與C、Java類似的運(yùn)算符,支持整數(shù)與浮點(diǎn)數(shù)的數(shù)學(xué)運(yùn)算。同時(shí)還支持復(fù)數(shù)運(yùn)算與無(wú)窮位數(shù)(實(shí)際受限于計(jì)算機(jī)的能力)的整數(shù)運(yùn)算。除了求絕對(duì)值函數(shù)abs()外,大多數(shù)數(shù)學(xué)函數(shù)處于math和cmath模塊內(nèi)。前者用于實(shí)數(shù)運(yùn)算,而后者用于復(fù)數(shù)運(yùn)算。使用時(shí)需要先導(dǎo)入它們,比如:
>>> import math
>>> print(math.sin(math.pi/2))
1.0
fractions模塊用于支持分?jǐn)?shù)運(yùn)算;decimal模塊用于支持高精度的浮點(diǎn)數(shù)運(yùn)算。
Python定義求余運(yùn)行a % b的值處于開(kāi)區(qū)間[0, b)內(nèi),如果b是負(fù)數(shù),開(kāi)區(qū)間變?yōu)?b, 0]。這是一個(gè)很常見(jiàn)的定義方式。不過(guò)其實(shí)它依賴于整除的定義。為了讓方程式:b * (a // b) + a % b = a恒真,整除運(yùn)行需要向負(fù)無(wú)窮小方向取值。比如7 // 3的結(jié)果是2,而(-7) // 3的結(jié)果卻是-3。這個(gè)算法與其它很多編程語(yǔ)言不一樣,需要注意,它們的整除運(yùn)算會(huì)向0的方向取值。
Python允許像數(shù)學(xué)的常用寫(xiě)法那樣連著寫(xiě)兩個(gè)比較運(yùn)行符。比如a < b < c與a < b and b < c等價(jià)。C++的結(jié)果與Python不一樣,首先它會(huì)先計(jì)算a < b,根據(jù)兩者的大小獲得0或者1兩個(gè)值之一,然后再與c進(jìn)行比較。
幫助
編輯
1. 列出模塊中的函數(shù)
用import導(dǎo)入模塊后,可使用函數(shù)dir(m)列出模塊的所有函數(shù),import是導(dǎo)入模塊的命令,m是模塊名。
例子:
>>>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']
這個(gè)例子列出math模塊的一些函數(shù),以雙下劃線( _ _ )開(kāi)頭的名稱用于較復(fù)雜的python編程。
2.查看完整的python內(nèi)置函數(shù)清單
查看完整的python內(nèi)置函數(shù)清單,可在提示符后輸入 >>>dir(_ _builtins_ _)。
例子:
>>>?dir(__builtins__)
['ArithmeticError',?'AssertionError',?'AttributeError',?'BaseException',?'BlockingIOError',?'BrokenPipeError',?'BufferError',?'BytesWarning',?'ChildProcessError',?'ConnectionAbortedError',?'ConnectionError',?'ConnectionRefusedError',?'ConnectionResetError',?'DeprecationWarning',?'EOFError',?'Ellipsis',?'EnvironmentError',?'Exception',?'False',?'FileExistsError',?'FileNotFoundError',?'FloatingPointError',?'FutureWarning',?'GeneratorExit',?'IOError',?'ImportError',?'ImportWarning',?'IndentationError',?'IndexError',?'InterruptedError',?'IsADirectoryError',?'KeyError',?'KeyboardInterrupt',?'LookupError',?'MemoryError',?'ModuleNotFoundError',?'NameError',?'None',?'NotADirectoryError',?'NotImplemented',?'NotImplementedError',?'OSError',?'OverflowError',?'PendingDeprecationWarning',?'PermissionError',?'ProcessLookupError',?'RecursionError',?'ReferenceError',?'ResourceWarning',?'RuntimeError',?'RuntimeWarning',?'StopAsyncIteration',?'StopIteration',?'SyntaxError',?'SyntaxWarning',?'SystemError',?'SystemExit',?'TabError',?'TimeoutError',?'True',?'TypeError',?'UnboundLocalError',?'UnicodeDecodeError',?'UnicodeEncodeError',?'UnicodeError',?'UnicodeTranslateError',?'UnicodeWarning',?'UserWarning',?'ValueError',?'Warning',?'WindowsError',?'ZeroDivisionError',?'_',?'__build_class__',?'__debug__',?'__doc__',?'__import__',?'__loader__',?'__name__',?'__package__',?'__spec__',?'abs',?'all',?'any',?'ascii',?'bin',?'bool',?'bytearray',?'bytes',?'callable',?'chr',?'classmethod',?'compile',?'complex',?'copyright',?'credits',?'delattr',?'dict',?'dir',?'divmod',?'enumerate',?'eval',?'exec',?'exit',?'filter',?'float',?'format',?'frozenset',?'getattr',?'globals',?'hasattr',?'hash',?'help',?'hex',?'id',?'input',?'int',?'isinstance',?'issubclass',?'iter',?'len',?'license',?'list',?'locals',?'map',?'max',?'memoryview',?'min',?'next',?'object',?'oct',?'open',?'ord',?'pow',?'print',?'property',?'quit',?'range',?'repr',?'reversed',?'round',?'set',?'setattr',?'slice',?'sorted',?'staticmethod',?'str',?'sum',?'super',?'tuple',?'type',?'vars',?'zip']
3. 查看某個(gè)函數(shù)的文檔幫助信息
可以用函數(shù)help(函數(shù))來(lái)查看某個(gè)函數(shù)的文檔幫助信息。
例子:
>>>help(sum)
Help?on?built-in?function?sum?in?module?builtins:
sum(iterable,?start=0,?/)
Return?the?sum?of?a?'start'?value?(default:?0)?plus?an?iterable?of?numbers??????When?the?iterable?is?empty,?return?the?start?value.
This?function?is?intended?specifically?for?use?with?numeric?values?and?may????reject?non-numeric?types.
可以直接在提示符下輸入help(),然后輸入某個(gè)模塊或函數(shù)名得到詳細(xì)的幫助信息。
總結(jié)
以上是生活随笔為你收集整理的python启蒙视频_python启蒙阶段的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 公司起名一般多少字?
- 下一篇: 特别沙雕的网名 比较特别的网名