Python number Type

Source: Internet
Author: User
Tags floor division

In Python, the data takes the form of objects, whether Python built-in objects or Python tools and objects created by themselves like C.

Python Numeric type tool:

    1. Integers and floating-point numbers

    2. Plural

    3. Decimal number for fixed precision

    4. Rational Fractions

    5. Collection

    6. Boolean type

    7. Infinite integer type

    8. Various digital built-in functions and modules


One of the ways in which the Python number type is displayed in a program is as a constant (there is also a call to the module using the function):

Numeric constants

12,-12 integer

1.23 floating point

0o177,0x9ff,0b0000100 octal, 16 binary, binary

3+4j plural


Built-in Math tools and extensions:

An expression operator

+-*/>>**&

Built-in math functions

Pow,abs,round,int, etc.

Public module

Random,math, etc.

Expressions are the basic tool for most numeric types.


Numbers in the actual application:

Variables and basic expressions

Variables and objects are different, no need to declare

Variable is created when it is assigned the first time

Variables used in an expression will be replaced with their values

Variables need to be assigned before they are used in an expression, otherwise they will be an error.

Common Expressions 1, simple subtraction:

>>> a=4

>>> b=5

>>> A+b,a-b

(9,-1)

>>> A*3,B/2

(12, 2.5)

>>> a%3,b**2

(1, 25)

>>> a<b

True

>>> a>b

False

>>> a==b

False


Common Expressions Two, Division: TRUE Division, Floor Division

X/Y: True division in python3.0, which retains fractional parts regardless of type .

>>> 5/1

5.0

>>> 9/3

3.0

x//y: Truncate remainder and preserve type for numeric type

>>> 5//3

1

>>> 9//2

4

>>> 5.0//3

1.0

>>> 5//2.0,5//-2.0

(2.0,-3.0)

Integer precision, in python3.0 integers can use an infinite length, as long as the memory is large enough.


Common Expressions 36 or 16 binary, octal, binary count

>>> 0o7,0o11,0o377

(7, 9, 255)

>>> 0x01,0xf,0xff

(1, 15, 255)

>>> 0b1,0b1000,0b11111111

(1, 8, 255)

>>> Oct (+), Hex (+), Bin (64) Convert decimal to octal, 16 binary, binary function

(' 0o100 ', ' 0x40 ', ' 0b1000000 ')

The int function transforms a numeric string into an integer, and the second number is the binary of the specified number.

>>> int (' Max '), int (' + ', 8), int (' + ', +), int (' 1000000 ', 2)

(64, 64, 64, 64)


Common expressions Four, plural

>>> 2+2j

(2+2J)

The above describes the core data types: integers, floating point, complex numbers, they are created by constant expressions, and other data types are described next.

1, decimal number, is created by importing the module call function, using a decimal number has a fixed precision floating point value.

>>> from decimal Import decimal

>>> Decimal (' 0.2 ') +decimal (' 0.01 ')

Decimal (' 0.21 ')

A. Set global precision

>>> Import Decimal

>>> Decimal. Decimal (1)/decimal. Decimal (7)

Decimal (' 0.1428571428571428571428571429 ')

>>> Decimal.getcontext (). prec=4 #通过调用decimal模块的getcontext函数指定保留小数位数, to specify precision

>>> Decimal. Decimal (1)/decimal. Decimal (7)

Decimal (' 0.1429 ')

B. Set temporary precision

>>> Import Decimal

>>> Decimal. Decimal (' 1.00 ')/decimal. Decimal (' 7.00 ')

Decimal (' 0.1428571428571428571428571429 ')

>>> with Decimal.localcontext () as CTX: #通过调用decimal模块的localcontext函数临时保留小数位数

... ctx.prec=2

... decimal. Decimal (' 1.00 ')/decimal. Decimal (' 7.00 ')

...

Decimal (' 0.14 ')

>>> Decimal. Decimal (1)/decimal. Decimal (7)

Decimal (' 0.1428571428571428571428571429 ')

2. Fraction type (fraction)

Similar to fractional numbers, it is also created by the post-import function of the module.

>>> from fractions import fraction

>>> x=fraction (1,4)

>>> x

Fraction (1, 4)

>>> Print (x)

1/4

You can also create a floating-point number

>>> fraction (. 25)

Fraction (1, 4)

>>> fraction ('. 25 ')

Fraction (1, 4)

3. Collection

The collection is unordered and does not match the key to a value, neither a sequence nor a mapping type. Use the built-in function set to create the collection type.

Note: An item can only appear once in the collection. one of the most important reasons to use:

>>> set (' Sspa ')

{' A ', ' s ', ' P '}

Here's how to create a collection:

Common Way

>>> x=set (' ABCDE ')

>>> y=set (' Efghj ')

>>> x, y

({' E ', ' B ', ' d ', ' C ', ' a '}, {' J ', ' F ', ' h ', ' e ', ' G '})

>>> X-y

{' C ', ' d ', ' A ', ' B '}

>>> X|y

{' J ', ' F ', ' h ', ' e ', ' B ', ' d ', ' G ', ' C ', ' a '}

>>> X&y

{' E '}

3.0 Ways to add:

>>> set ([1,2,3,4])

{1, 2, 3, 4}

>>> set ([' Spam '])

{' Spam '}

>>> set (' spam ')

{' m ', ' a ', ' s ', ' P '}

>>> set () #空集合必须使用set创建

Set ()

>>>

4. Boolean type

True and False

Python number Type

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.