current position:Home>On the built-in object type of Python -- number (one of the differences between py2 and PY3)

On the built-in object type of Python -- number (one of the differences between py2 and PY3)

2022-06-24 05:06:51Python advanced

 Python It's an object-oriented programming language , Everything in a program can be treated as an object .Python Built in objects can be divided into simple types and container types , Simple types are mainly numerical data , A container type is a collective that can contain other object types , Such as sequence 、 Tuples 、 Mapping, etc. .

         In simple type objects ,Python The numeric data of includes integers (int)、 Long integer (long)、 floating-point (float)、 The plural (complex) And Boolean (bool) etc. . In container type objects , A sequence is a class of objects whose elements are stored in sequence , Mainly including string (string)、 list (list)、 Tuples (tuple) Other types . A mapping is a structure that accesses values through keys ,Python The only mapping structure in is the dictionary (dict) object . In addition to simple type and container type objects , Another special object is None, Is an empty object .

         All objects have data properties and built-in methods . The method of an object is what we often call a function , For performing operations , Act on objects . The properties and methods of an object can be defined by the dot operator (.) To call .

         Today, Xiaobian will show you something about Python Simple type objects in built-in objects , That is, the number type .Python The values in can be various numbers , Include integers 、 long integer 、 Floating point numbers 、 The plural 、 Boolean type number and so on . I wrote an article the day before yesterday , About how to execute in an interactive environment Python Program , The following figure shows some examples , First, let's take a look at the number type objects .

One 、 Constant

        Python The numeric constants in support of multiple formats , Our common constants are integer and floating-point . Actually Python Scientific counting is also supported in , Floating point numbers are often represented by scientific notation , Such as 1.2e-3 or 1.2E-3 Express 1.2*10 Of -3 Power . In addition to the default decimal number , You can also use binary 、 octal 、 Hexadecimal to denote constants , However, prefix these numbers to distinguish them from decimal numbers , Binary system 、 octal 、 The hexadecimal prefixes are 0b、0、0x.Python The numeric constants in also support complex and fractional forms , But there is one thing to pay attention to when calculating fractions , Look at the example below .

                        py2 Fraction operation in version

                        py3 Fraction operation in version

         Do you feel a little confused ? stay py2 The result of the integer division operation in is still an integer , It's rounding , Instead of rounding . When one of the numbers involved in the operation is a floating-point number ,Python Will automatically convert another number implicitly to a floating-point number , So the result of the second operation is a floating point number . stay py3 in , The meaning of division is the same as what we usually see , Not rounding . let me put it another way , The divisor and dividend of division are in py3 Floating point number conversion has been done in , And then divide them , The resulting quotient is a floating point number .

        Python There is also a double slash in the operation of //, It also represents division , This division yields the maximum integer quotient , This division pattern is also called floor division . stay py2 This mode is supported in , When two integers are divided , The result is only the integer part of the quotient .% Represents the remainder operator , You can get the remainder . As shown in the figure below :

                        floor Division and remainder operations

Two 、 fraction

         Fractions consist of two parts, numerator and denominator . stay Python in , function Fraction(x, y) It's a score x/y. Before using this function , You need to import the score module fractions, After the score is built , Can be used in expressions . Of course , You can also convert a floating-point string to a fraction , As shown in the figure below :

                         fraction Fraction Function operation

3、 ... and 、 Boolean type

         Boolean objects have only two values , to (True) And wrong (False). For null data types , Its Boolean values are False. Generally speaking , Boolean values are often used in condition judgment , It is usually used as a test condition for a branch or loop of a program .

Four 、 The plural

         The plural number is composed of the rational part and the irrational part . The rational part is called the real part , This part is optional ; The irrational part is called the imaginary part , This part is required , The suffix of the imaginary part is generally j perhaps J Express . Complex numbers also support four operations in constants, etc , The result of running between complex numbers is still complex , As shown in the figure below :

                         Four operations in complex numbers

         Numbers are the most basic numeric type , No matter what kind of programming language is inseparable from it . I hope you guys can master it well , In the part of fractional operation, we should pay special attention to mastering py2 and py3 The difference in the version , This knowledge point is often encountered in the interview questions , In fact, this part is one of the differences between the two versions .

copyright notice
author[Python advanced],Please bring the original link to reprint, thank you.

Random recommended