Python core programming: digital type, python core programming

Source: Internet
Author: User
Tags floor division cmath

Python core programming: digital type, python core programming
1. Introduction to numeric types

  • The numeric types in Python include integer, long integer, Boolean, double-precision floating point, decimal floating point, and plural. These numeric types are all unchangeable. That is to say, a new object is generated when the numeric value is changed.
  • To delete a numeric object in Python, use the following statements: del aInt, aLong, aFloat, and aComplex.
2. Integer
  • Boolean

    The value ranges from True to False. They correspond to 1 and 0 in mathematical operations.

    The Boolean value of any number or empty set (empty list, empty tuples, empty dictionary, etc.) with a value of 0 in Python is False.

  • Long Integer

    In other programming languages, for example, in C ++, int occupies 32 bits, and long int depends on the machine font length. long int is 64 bits. In Python, standard integers usually occupy 32 bits, but if Python is compiled with a 64-bit compiler on a 64-bit machine, the integer occupies 64 bits.

    Python long integer can support a large range, only depends on the virtual memory size supported by the machine. Long Integer Declaration: add L: 12345L directly after the number

    In Python, integer and long integer are gradually unified, so you do not need to worry about integer or long integer during use.

3. Double Precision Floating Point

The float type in Python is similar to the double type in C. It is a double precision float type and occupies 64 bits.

4. Plural

Complex Type in Python:

  • The complex number consists of the real number and the virtual number: real + imagj
  • The imaginary part cannot exist independently. It must be in the plural together with the real part of 0.0.
  • The real and virtual parts of imag are of the floating point type.
  • The complex type has three built-in attributes: real, imag, conjugate)
>>> aComplex = 1.0 + 2.0j>>> aComplex.real1.0>>> aComplex.imag2.0>>> aComplex.conjugate()(1-2j)
5. Operators

Automatic type conversion

When two different types of numbers are operated, Python automatically converts the types. The basic principle is to convert an integer to a floating point type, and convert a non-plural value to a plural value.

Real division vs floor Division

In the current Python version, the Division code "/" is called to execute real Division:

>>> 1/20.5>>> 1.0/2.00.5

There is also a division called floor division, which always removes the fractional part regardless of the type of the operand. It can be implemented by conforming:

>>> 1.0//2.00.0

Merge operation

You can use the ** operator to pay attention to the priority of various symbols, or use parentheses () to eliminate ambiguity caused by priority:

>>> -2**4-16>>> (-2)**416

Bitwise operators

Python integer supports the standard bitwise operation: Get inverse ~ , Bitwise AND &, bitwise OR |, bitwise XOR or ^, left shift <, right shift>

6. Factory Functions
  • Standard type functions such as cmp (), str (), type ()
  • Numeric Functions
    • Conversion factory functions: int (), long (), float, conplex ()
    • Function:
      • Abs (num), returns the absolute value
      • Corece (num1, num2) converts num1 and num2 to the same type, and then returns
      • Divmod (num1, num2), return (num1/num2, num1 % num2)
      • Pow (num1, num2), implement num1 ** num2
      • Round (flt, ndig = 0), round the floating point flt, save ndig decimal places, the default is 0.
  • Integer functions only
    • Hexadecimal conversion functions, oct (), hex ()
    • ASCII conversion functions
      • Chr () accepts a single-byte integer value and returns the corresponding character
      • Ord () accepts one character and returns the corresponding integer value.
>>> chr(97)'a'>>> ord('a')97
7. Related modules
  • Decimal Decimal floating point operation
  • Array efficient numeric array
  • Math/cmath Standard C library mathematical operation functions, regular mathematical operations in math, complex operations in cmath
  • Operator mathematical operator function implementation, such as operator. sub () is equivalent to 2-1
  • Random Multiple pseudo-random number generators
>>> import operator>>> operator.sub(2,1)1>>> import random>>> random.randint(0,10)3>>> random.randint(0,10)7

Reprinted please indicate the source: http://blog.csdn.net/u012162613/article/details/44286655

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.