Python core programming-Chapter 5-personal notes, Chapter 5

Source: Internet
Author: User
Tags floor division

Python core programming-Chapter 5-personal notes, Chapter 5

1. Use del to delete references to objects

>>> a = 123>>> a123>>> del a>>> aTraceback (most recent call last):  File "<stdin>", line 1, in <module>NameError : name 'a' is not defined

2. Integer

(1) Boolean value range of this type: Boolean value True and Boolean value False

(2) Standard integer long integer

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

② Python standard integer is equivalent to long integer in C

③ Integer types are generally expressed in decimal notation, but python also supports octal and hexadecimal notation. The octal integer starts with 0, and the hexadecimal integer starts with 0x or 0X.

④ Adding an uppercase L to the end of an integer indicates that this is a long integer.

3. Floating Point Type

The floating point type usually has a decimal point and an optional suffix e to indicate the scientific notation. e is followed by an index, and e is used between the positive and negative numbers to indicate the positive and negative numbers of the index. Positive numbers can be omitted.

4. Plural

① A complex number consists of a real number and a virtual number. The real number and the virtual number are both floating-point. The virtual number must have a suffix.

② Attributes of a complex number include:

>>> acomplex = 2.22-1.33j>>> acomplex(2.22-1.33j)>>> acomplex.real2.22>>> acomplex.imag1.33>>> acomplex.conjugate()(2.22+1.33j)

5. Operators

(1) Mixed Mode Operators

When adding numbers of different types in python, the numerical type is forcibly converted to solve the problem of inconsistent numeric types. Conversion rules are as follows:

If one operand is a plural value, the other operand is converted to a plural value;

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

Otherwise, if one is a long integer and the other is converted to a growth integer;

Otherwise, both are Integer type and do not need to be converted.

(2) Arithmetic Operators

① Division in python includes the following types:

The traditional division operator "/". If the two operands of the traditional division are integer, the traditional division will remove the fractional part and return an integer. If one of the operands is a floating point, the real division will be executed.

>>> 1 / 20>>> 1.0 / 20.5

Real division: by executing the from _ future _ import division command, the operator "/" cannot be two operands: integer or floating-point.

>>> from __future__ import division>>>>>> 1 / 20.5>>> 1.0 / 2

Floor Division: the new operator "//" introduced by python 2.2 executes floor division. The fractional part is always removed regardless of the type of the operand, returns the nearest number smaller than the real quotient.

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

6. Built-in and factory Functions

(1) Conversion factory Functions

Int (), long (), float (), and complex () functions are used to convert other numeric types to corresponding numeric types, or return values represented by strings.

Int () and long () can accept the second optional parameter, which is used to convert the given real parameter to the hexadecimal

Complex () can accept two parameters. The first parameter is the real part of the complex number, the second parameter is the virtual part of the complex number, and the second parameter is 0 by default.

>>> 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

Python has five built-in functions for numerical operations, including abs () coerce () divmod () pow () round ()

① Abs ()

Abs () returns the absolute value of a given parameter. If the parameter is a complex number, the modulo of the complex number is returned, that is, the square root of the sum of the real and virtual numbers (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 containing two values after the conversion type.

>>> coerce(1,2)(1,2)>>>>>> coerce(1.3,134L)(1.3,134.0)>>>>>> coerce(1,12L)(1L,12L)>>>>>> (1j,12L)(1j,(12+0j))

③ Divmod ()

The divmod () function combines division and remainder. It accepts two parameters, divisor and divisor, and returns a tuple containing quotient and remainder. The Division is implemented on the floor, and the remainder is obtained through the remainder operation.

>>> Divmod (1.5) (2.0) >>>> divmod (0.0) () >>> divmod (3,) (,) >>> divmod (1.5, 3) (0.0, 1.5) >>> divmod (2 + 34j, 1 + 17j) (2 + 0j), 0j) # The operator of the plural number takes only the operator of the real number.

④ Pow ()

The pow () and double star ** functions are similar to exponential operations. Pow () accepts three parameters. The first parameter is the base number, and the second parameter is the index. These two parameters are required. pow () can also accept the third optional parameter, if this parameter is given, pow () first performs an exponential operation, and then performs a remainder operation on the operation result and the third parameter. This feature is mainly used for password calculation 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. The first parameter is the floating point type to be rounded to. The second optional parameter tells the round () function to specify the number of digits after the decimal point. If the second parameter is not specified, an integer closest to the first parameter is returned, that is, the decimal point is retained to 0 (still float type)

>>> 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 int () function directly truncates the fractional part. The returned value is an integer.

The math. floor () function returns an integer that is closest to the original number but smaller than the original number. The returned value is a floating point type.

The round () function is rounded to an integer nearest to the original number. The return value is a floating point type.

(3) only integer Functions

① Hexadecimal conversion function

The python built-in function oct () accepts an integer in any hexadecimal format and returns the corresponding octal (starting with 0) String object.

The built-in function hex () accepts an integer in any hexadecimal format and returns a String object starting with 0X.

>>> oct(25)'031'>>> oct(31)'037'>>> hex(255)'0xff'>>> hex(192)'0xc0'

② ASCII conversion functions

The python built-in function chr () accepts a single-byte integer value and returns a string whose value is a corresponding character.

The built-in function ord (), on the contrary, 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 happiness:

7. Other numeric types

(1) Boolean

Several properties:

① The Boolean value is actually a subclass of an integer. True corresponds to integer 1, and False corresponds to integer 0.

② All python objects have a built-in value of True or False. The Boolean value of the following objects is False:

None;
False (boolean type );

All values are zero;

0 (integer );

0.0 (floating point type );

0L (long integer );

0.0 + 0.0j (plural );

"" (Null String );

[] (Empty list );

() (Null tuples );

{} (Empty dictionary );

The boolean values of objects whose values are not listed above are True, such as non-empty and non-zero. If the user-created class instance defines nonzero (_ nonzero _ () or length (_ len _ () 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

① Common python modules on numerical values

② Introduction to random

Example:

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

 

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.