Learning the value type of Python step by step (1) (1)

Source: Internet
Author: User

Bkjia.com exclusive Article]This document describes a basic data type in Python: Numeric. First, we will introduce in detail various numeric types of the Python language, corresponding arithmetic operations, their priority, and their associativity. Then, we will explain the bitwise operations of integer types in depth.

I. numeric type

There are four numeric types in Python: integer, long integer, floating point, and plural. Next we will first introduce integer data.

1. Integer

The integer type in Python is equivalent to the long type in C language. on 32-bit machines, the bit width of the integer type is 32 bits, and the value range is-231 ~ 231-1, that is,-2147483648 ~ 2147483647; in 64-bit systems, the bit width of an integer is usually 64-bit, and the value range is-263 ~ 263-1, that is,-9223372036854775808 ~ 9223372036854775807.
Integers in Python can be expressed in decimal or octal or hexadecimal notation. When an integer is expressed in octal notation, a prefix "0" must be added before the value. When an integer is expressed in hexadecimal notation, the prefix 0X or 0x must be added before the number. For example, here we assign the integer 15 in octal and hexadecimal format to the integer variables a and B, and then output them in decimal format:

#-*-Coding: cp936-*-a = 017b = 0 xfprint 'the decimal form of the value of variable a is % d' % aprint'. The decimal form of the value of variable B is % d' %

When we run this program in IDEL, the results are as follows:

Figure 1: Input and Output integers in different numbers
Here we use the print statement
Print 'variable a value in decimal format: % d' %
Make a simple explanation. The meaning of this statement is to output variable a in the form of signed integers. The percent sign (%) is a format operator in Python. It can insert a variable value into the string. The left side of the formatting operator is a string, which is represented in blue below:
Print 'variable a value in decimal format: % d' %

The string on the left of the formatting operator can contain one or more conversion indicators. In this example, there is only one conversion indicator, namely % d. As we can see here, conversion indicators are usually headers with a percent sign followed by a string formatted character. Note that, the percent sign in the conversion indicator is used as the leading character of the string formatting character, rather than the formatting operator. String format character d indicates that an integer is placed at the specified position of the current string. When printing the output, the conversion indicator in the string will be replaced by the specified value, so the conversion indicator also plays a placeholder role. The right side of the formatting operator, that is, the part represented in red, specifies who will replace the placeholder in the string. In this example, we use variable a to replace the placeholder % d in the string.

The formatted Characters Related to the value and their functions are as follows:

D: A signed decimal integer.
 U: Unsigned decimal integer
O: Unsigned octal integer
 X: Unsigned hexadecimal integer, ~ F in lower case
 X: Unsigned hexadecimal integer, ~ F in upper case
Raise f: Floating Point Number
 E, E: Floating Point Number, using scientific notation
 G, G: Floating Point Number, use the lowest valid Digit

2. Long Integer

Different from the C language, the Length Integer of Python does not specify the Bit Width. That is to say, Python does not limit the size of the length integer value. However, due to limited machine memory, the length integer value we use cannot be infinitely large.
How can we distinguish between long and integer values during use? Generally, an uppercase or lowercase letter L is appended to the end of a number to indicate that the integer is a long integer. For example:

a = 6812347598Lb = 6812347598l

Readers may have discovered that it is difficult to distinguish the lower-case letters (l) from the numbers (1). Therefore, we recommend that you use the upper-case letters (L.

Note: Since Python2.2, if an overflow occurs, Python will automatically convert the integer data to a long integer. Therefore, without the letter L after the long integer data, it will not cause serious consequences.

3. Floating Point Type

Float is used to process real numbers, that is, numbers with decimal places. The Python float type is equivalent to the double precision float type of the C language. A real number can be expressed in decimal form. It consists of digits and decimal points. The decimal points are indispensable, such as 1.23, 123.0, and 0.0; the other is an exponential form. For example, 789e3 or 789E3 represents 789 × 103, and the letter e or E must have digits, letters e or E) and can have a positive and negative signs, it indicates the symbol of the index. If not, it indicates the positive number. In addition, the index must be an integer.

4. Plural type

A complex number is composed of a real number and a virtual number. Generally, x + yj is used. x is the real number of a complex number, and y is the virtual number of a complex number. Here, x and y are both real numbers. Note that the uppercase and lowercase letters of the imaginary part are acceptable. For example, 5.6 + 3.1j and 5.6 + 3.1J are equivalent.
For Variable n of the plural type, we can also use n. real to extract the actual number, using n. imag to extract its virtual part, using n. conjugate returns the complex number of n, as shown in:

Figure 2: Obtain the real part, virtual part, and bounded plural part of a complex number


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.