[C014] Python data type-numerical type

Source: Internet
Author: User

Python supports built-in data types, including numbers, strings, lists, dictionaris, and tuples.

Generally, data types are combined with operators. The size and accuracy of a data type, the declaration and initialization of variables, and priority and combination, type conversion, and precision loss must be considered for operators. The variable name is a combination of letters, numbers, and underscores. All functions and external variables are defined in the _ main _ () function.

Description of data types: variables do not need to be declared in python. You only need to assign values when using them (based on the dynamic type model during python runtime ), check whether the variables and values and objects are referenced.

 

1. numbers

Numbers mainly includes Integer, float, octal hexadecimal, and complex numbers ).

>>> Type (3.0)

<Type 'float'>

>>> Type (3)

<Type 'int'>

>>> Type (111111111111111111111111111)

<Type 'long'>

>>> Type (True)

<Type 'bool '>

>>> Type (1 + 2j)

<Type 'compute'>

Python

Python does not limit the size of real numbers, as long as you have enough memory. Multiple operation operations and built-in methods are supported, including arithmetic operations (+-*/**), logical operations (and, or, not), and relational operations (>=<>! ====...), Bitwise (| ^ & >>< ).

 

Five Python built-in functions for Numerical Calculation

1 abs () returns the absolute value of a given Parameter

2 coerce () data type conversion function returns a tuple containing two numeric elements after type conversion.

 

>>> Coerce (1, 2)

(1, 2)

>>> Coerce (1.3, 3)

(1.3, 3.0)

>>> Coerce (1j, 12)

(1j, (12 + 0j ))

 

3 divmod () combines division and remainder operation to return a tuple containing the quotient and remainder.

 

>>> Divmod (10, 3)

(3, 1)

>>> Divmod (3, 10)

(0, 3)

>>> Divmod (10, 2.5)

(4.0, 0.0)

>>> Divmod (2.5, 10)

(0.0, 2.5)

 

4 pow () and ** are the same as exponential operations.

 

>>> Pow (2, 5)

32

>>> Pow (3.141592, 2)

9.8696002944640018

>>> Pow (1 + 1j, 3)

(-2 + 2j)

>>>

 

5 round () is used to round a floating point number.

 

>>> Round (3)

3.0

>>> Round (3.45)

3.0

>>> Round (3.5)

4.0

>>> Round (3.4566656, 1)

3.5

>>> Round (3.4566656, 2)

3.46

>>> Import math

>>> For eachNum inrange (10 ):

Print round (math. pi, eachNum)

 

 

3.0

3.1

3.14

3.142

3.1416

3.14159

3.141593

3.1415927

3.14159265

3.141592654

>>>

 

Built-in functions for Integers only:

Hexadecimal conversion functions:

 

1. hex (num) accepts an integer and returns the corresponding hexadecimal format.

>>> Hex (1, 255)

'0xff'

 

2. oct (num)

>>> Oct (255)

'123'

 

ASCII conversion functions

 

1ord (): enter a character and the output is its corresponding ASCII code.

 

>>> Ord ('A ')

97

>>> Ord ('')

32

>>> Ord ('A ')

65

>>> Ord ('0 ')

48

 

2. Enter an ASCII number in chr () and the output is a corresponding character.

 

>>> Chr (97)

'A'

>>> Chr (32)

''

>>> Chr (65)

'A'

>>> Chr (48)

'0'

>>>

Type conversion, like c, is generally oriented to high precision (as long as a + bj is involved, it should also be included ). The print statement automatically saves extra precision.

>>> A = 1

>>> B = 2

>>> A/B

0

>>> A = 1.0

>>> A/B

0.5

>>> A = 1 + 2j

>>> A + B

(3 + 2j)

>>>

Of course, you can also perform forced type conversion.

>>> A = 5

>>> Long ()

5L

>>> Int ()

5

>>> Float ()

5.0

>>> Complex ()

(5 + 0j)

>>>

Source:

Python supports built-in data types, including numbers, strings, lists, dictionaris, and tuples.

Generally, data types are combined with operators. The size and accuracy of a data type, the declaration and initialization of variables, and priority and combination, type conversion, and precision loss must be considered for operators. The variable name is a combination of letters, numbers, and underscores. All functions and external variables are defined in the _ main _ () function.

Description of data types: variables do not need to be declared in python. You only need to assign values when using them (based on the dynamic type model during python runtime ), check whether the variables and values and objects are referenced.

 

1. numbers

Numbers mainly includes Integer, float, octal hexadecimal, and complex numbers ).

>>> Type (3.0)

<Type 'float'>

>>> Type (3)

<Type 'int'>

>>> Type (111111111111111111111111111)

<Type 'long'>

>>> Type (True)

<Type 'bool '>

>>> Type (1 + 2j)

<Type 'compute'>

Python

Python does not limit the size of real numbers, as long as you have enough memory. Multiple operation operations and built-in methods are supported, including arithmetic operations (+-*/**), logical operations (and, or, not), and relational operations (>=<>! ====...), Bitwise (| ^ & >>< ).

 

Five Python built-in functions for Numerical Calculation

1 abs () returns the absolute value of a given Parameter

2 coerce () data type conversion function returns a tuple containing two numeric elements after type conversion.

 

>>> Coerce (1, 2)

(1, 2)

>>> Coerce (1.3, 3)

(1.3, 3.0)

>>> Coerce (1j, 12)

(1j, (12 + 0j ))

 

3 divmod () combines division and remainder operation to return a tuple containing the quotient and remainder.

 

>>> Divmod (10, 3)

(3, 1)

>>> Divmod (3, 10)

(0, 3)

>>> Divmod (10, 2.5)

(4.0, 0.0)

>>> Divmod (2.5, 10)

(0.0, 2.5)

 

4 pow () and ** are the same as exponential operations.

 

>>> Pow (2, 5)

32

>>> Pow (3.141592, 2)

9.8696002944640018

>>> Pow (1 + 1j, 3)

(-2 + 2j)

>>>

 

5 round () is used to round a floating point number.

 

>>> Round (3)

3.0

>>> Round (3.45)

3.0

>>> Round (3.5)

4.0

>>> Round (3.4566656, 1)

3.5

>>> Round (3.4566656, 2)

3.46

>>> Import math

>>> For eachNum inrange (10 ):

Print round (math. pi, eachNum)

 

 

3.0

3.1

3.14

3.142

3.1416

3.14159

3.141593

3.1415927

3.14159265

3.141592654

>>>

 

Built-in functions for Integers only:

Hexadecimal conversion functions:

 

1. hex (num) accepts an integer and returns the corresponding hexadecimal format.

>>> Hex (1, 255)

'0xff'

 

2. oct (num)

>>> Oct (255)

'123'

 

ASCII conversion functions

 

1ord (): enter a character and the output is its corresponding ASCII code.

 

>>> Ord ('A ')

97

>>> Ord ('')

32

>>> Ord ('A ')

65

>>> Ord ('0 ')

48

 

2. Enter an ASCII number in chr () and the output is a corresponding character.

 

>>> Chr (97)

'A'

>>> Chr (32)

''

>>> Chr (65)

'A'

>>> Chr (48)

'0'

>>>

Type conversion, like c, is generally oriented to high precision (as long as a + bj is involved, it should also be included ). The print statement automatically saves extra precision.

>>> A = 1

>>> B = 2

>>> A/B

0

>>> A = 1.0

>>> A/B

0.5

>>> A = 1 + 2j

>>> A + B

(3 + 2j)

>>>

Of course, you can also perform forced type conversion.

>>> A = 5

>>> Long ()

5L

>>> Int ()

5

>>> Float ()

5.0

>>> Complex ()

(5 + 0j)

>>>

Source: http://blog.sina.com.cn/s/blog_4b5039210100e9ya.html

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.