Python core Programming-fifth chapter-Personal notes

Source: Internet
Author: User
Tags abs ord pow square root

1. Delete the reference to the object with Del

>>> a = 123>>> adel  a>>> atraceback (most recent call last): c4/>"<stdin>" in <module>'a '  is  Not defined

2. Integral type

(1) Boolean type value range as long as two values: Boolean True and Boolean value false

(2) Standard integral type long integral type

The ①python standard integer value range is -231~231-1, which is 2 147 483 648~2 147 483 648.

②python Standard integral is equivalent to a long integer type in C

③ integers are generally represented in 10, but Python also supports octal, hexadecimal. octal integers start with 0 and hexadecimal integers start at 0x or 0X

④ with an uppercase L at the back of the integer to indicate that this is a long integer type

3. Floating-point type

Floating-point types usually have a decimal point and an optional suffix e for scientific notation, e followed by an exponent, E and exponent are positive or negative for the exponent, and positive numbers can be omitted.

4. plural

① complex numbers are composed of real and imaginary parts, real and imaginary parts are floating-point types, and imaginary parts must have suffixes

The properties of the ② complex number include:

>>> Acomplex = 2.22-1.33j>>> acomplex (2.22-1.33j)>>> acomplex.real 2.22>>> acomplex.imag1.33>>> acomplex.conjugate (2.22+1.33j)

5. Operators

(1) Mixed mode operator

When different types of numbers in Python are added together, the problem of inconsistent numeric types is resolved by using a numeric type cast. The conversion rule is:

If one operand is a complex number, the other operand is converted to a complex number;

Otherwise, if one is a floating-point number, the other is converted to a floating-point number;

Otherwise, if there is a long integer, the other is transformed into an integral type;

Otherwise, both are integral types and do not need to be converted

(2) Arithmetic operators

The ①python division includes several:

Traditional division, Operator "/". If the two operands of the traditional division are integer, then the traditional division will remove the fractional part, return an integer, and perform a true division if one of the operands is a floating-point number.

>>>0>>> 1.0/20.5

True division, by executing the From __FUTURE__ Import Division instruction, the manipulator "/" cannot be two operands of integer or float, all performing true division

 from __future__ Import Division>>>>>> 1/20.5>>> 1.0/2

Flooring In addition to the new operator "//" introduced in Python 2.2, the execution of the floor in addition, regardless of the number of operands is the type of value, always shed the fractional part, return than the real quotient of the nearest number of small

>>> 1//20>>> 1.0//20.0

6. Built-in functions and factory functions

(1) Converting factory functions

the int (), long (), float (), complex () functions are used to convert other numeric types to the corresponding numeric types, or they can return a numeric value represented by a string

Int (), long () can accept the second optional parameter, which is used to convert the given argument into the corresponding binary

Complex () can accept two parameters, the first parameter is the real part of the complex number, the second argument is the imaginary part of the complex number, and the second parameter defaults to 0.

>>>int(1.23)1>>>Long(123)123L>>>float(123)123.0>>> Complex (123)(123+0j)>>> Complex (123,456)(123+456j)>>> Complex (1.23e-2,1.23e3)(0.0123+1230J)

(2) Function function

Python has 5 built-in functions for numeric operations, including ABS () coerce () Divmod () Pow () round ()

①abs ()

ABS () returns the absolute value of the given parameter. If the argument is a complex number, it returns the modulus of the complex number, the positive square root of the sum of the real and imaginary parts of the complex (Math.sqrt (REAL2+IMAG2)). The parameter can be an expression.

>>> ABS ( -1)1>>> ABS (1.1)1.1>>> abs (3+4j)5.0>>> ABS ( 1.22-10.22)9.0

②coerce ()

Coerce () is a data type conversion function that accepts two parameters and returns a tuple that contains two values after the conversion type

>>> coerce ($) >>>>>> coerce(1.3,134l) (1.3,134.0) >>>>>> Coerce (1,12l) (1l,12l)>>>>>> (1j,12l) ( 1j, (12+0J))

③divmod ()

The Divmod () function combines the division and the remainder, accepts two parameters, is divisor and divisor, and returns a tuple containing the quotient and remainders. The division performed is the addition of the floor, and the remainder is obtained by the take-rest operation.

>>> divmod (11,3) (3,2)>>> divmod (3,11) (0,3)>>> Divmod (3,1.5) (2.0,0.0)>>> divmod (1.5,3) (0.0,1.5)>>> divmod (2+34j,1+17j) ((2+0j), 0j)                      #复数的商仅取实数部分的商

④pow ()

Pow () and double star * * Function Similarly, are exponential operations. POW () accepts three parameters, the first parameter is the base, the second parameter is the exponent, both are required, and the POW () can also accept the third optional parameter, if this argument is given, the POW () will first perform an exponential operation, then the result of the operation and the third parameter to take the remainder operation. This feature is primarily used for cryptographic operations and is more efficient than POW (x, y)% Z!

>>> Pow (2,3)8>>> pow (3,2)9>>> pow (2,3,3)2>>> Pow (1 + 2j,4) (-7-24j)

⑤round ()

The built-in function round () is used to round the floating-point type. Accept two parameters The first is the floating-point type to be rounded, and the second optional parameter tells the round () function to specify the number of digits after the decimal point. Does not specify the second argument, returns the integer that is closest to the first parameter, that is, 0 bits after the decimal point (still floating-point)

>>> round (3)3.0>>> round (3.45)3.0>>> round (3.4999)3.0> >> round (3.499999,1)3.5>>> round ( -3.5)-4.0>>> round ( -3.4)-3.0

⑥int () round () Math.floor () difference

the function int () truncates the fractional part directly, the return value is the integer type

The function Math.floor () Gets the integer that is closest to the original but less than the original number, and the return value is floating-point

The function round () is rounded by rounding rules to get the integer nearest to the original number, and the return value is floating-point

(3) Functions for integral type only

① Binary conversion function

Python built-in function Oct () accepts an arbitrarily-made integer object that returns a string object of the corresponding octal (beginning with 0)

The built-in function, Hex (), accepts an arbitrarily-entered integer object, returning the corresponding hexadecimal (beginning with 0 x) of the string object

>>> Oct (+)'031'>>> Oct (+)'037 '>>> hex (255)'0xff'>>> Hex (192) ' 0xc0 '

②ASCII conversion function

Python built-in function Chr () takes a single-byte integer value, returns a string with a string value corresponding to the character

The built-in function, Ord (), instead, accepts a character and returns its corresponding integer value

>>> Chr (65)'A'>>> Chr (97)'a'>>> Chr (48)'0'>>>>>> Ord ('a')97>>> Ord ('A')65>>> Ord ('0')48

③ Family Photo:

7. Other Number types

(1) Boolean type

Several properties:

① Boolean is actually a subclass of integral type, true corresponds to integral type 1,false corresponding to integral type 0

the Python object that ② has has a built- in true or False value. The Boolean value of the following object is false:

None;
False (Boolean type);

All the values are zero numbers;

0 (integral type);

0.0 (floating point type);

0L (Long integer type);

0.0+0.0j (plural );

"" (empty string);

[] (empty list);

() (empty tuple);

{} (empty dictionary);

The Boolean value of an object that is not a value other than the one listed above is True, such as Non-empty, Non-zero, and so on. A user-created class instance if nonzero (__nonzero__ ()) or length (__len__ ()) is defined and the value is 0.

Then their boolean value is False.

>>> bool (1) True>>> bool (true) True>>> bool (0) False>> > bool ("0") True>>> bool (0.0) False>>> bool ( 0L) false>>> bool ([]) False

8. Related Modules

①python common modules on numerical values

② Introduction to Random

Cases:

Import random>>> random.randint (12,23)16>>> random.randrange (12,23)18> >> random.uniform (12,23)

Python core Programming-fifth chapter-Personal notes

Related Article

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.