- Learn Programming in Python with Cody Jackson
- Cody Jackson
- 444字
- 2021-06-10 19:06:08
Python numbers
Python can handle normal long integers (the maximum length is determined based on the operating system, just like C), Python long integers (the maximum length is dependent on available memory), floating-point numbers (just like C doubles), octal and hexadecimal numbers, and complex numbers (numbers with an imaginary component).
Here are some examples of these numbers:
- Integer: 12345, -32
- Python integer: 999999999L (in Python 3.x, all integers are Python integers)
- Float: 1.23, 4e5, 3e-4
- Octal: 012, 0456
- Hexadecimal: 0xf34, 0X12FA
- Complex: 3+4j, 2J, 5.0+2.5j
Historically, integers were 16-bit numbers while longs were 32-bit. This could cause problems when using compiled languages, such as C, because trying to store a number that was too big for its data type could cause errors. The largest 16-bit number available is 65535, so trying to store 999999999 in a regular integer would fail.
As advances in computing created 32-bit, then 64-bit, processors and operating systems, the number of bits used has changed, but they are still defined by the OS and the particular programming language compiler used to make the program. In Python 3, all integers are the longest value they can be.
Python has the normal built-in numeric tools you'd expect: expression operators (*, >>, +, <, and so on), math functions (pow, abs, and so on), and utilities (rand, math, and so on). For heavy number-crunching, Python has the Numeric Python (NumPy) (http://www.numpy.org) extension which has such things as matrix data types. It's heavily used in science and mathematical settings, as its power and ease of use make it equivalent to Mathematica, Maple, and MatLab. It's included with the Anaconda Python distribution, and due to the number of other tools included with Anaconda, that's probably the easiest way to obtain NumPy.
Though this probably doesn't mean much to non-programmers, the expression operators found in C have been included in Python; however, some of them are slightly different. Logic operators are spelled out in Python rather than using symbols; for example, logical AND is represented by the word and in Python, not by &&, as in C; logical OR is represented by the word or in Python, not the double-pipe symbol ||; and logical NOT uses the word not instead of !. More information can be found in the Python documentation.
Operator level-of-precedence is the same as C, but using parentheses is highly encouraged to ensure the expression is evaluated correctly and enhances readability. Mixed types (float values combined with integer values) are converted up to the highest type before evaluation; that is, adding a float and an integer will cause the integer to be changed to a float value before the sum is evaluated.
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- Mastering Articulate Storyline
- TypeScript圖形渲染實戰:基于WebGL的3D架構與實現
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- Mastering Google App Engine
- Modular Programming in Java 9
- Procedural Content Generation for C++ Game Development
- JavaScript程序設計(第2版)
- IoT Projects with Bluetooth Low Energy
- 關系數據庫與SQL Server 2012(第3版)
- Android應用開發攻略
- HTML5+CSS3+jQuery Mobile+Bootstrap開發APP從入門到精通(視頻教學版)
- 熱處理常見缺陷分析與解決方案
- Implementing DevOps with Ansible 2
- Java EE應用開發及實訓