Python entry notes (3): BASICS (II)

Source: Internet
Author: User
Tags floor division integer division
I. Numbers

When I read "Python core programming", I have some questions. Why didn't I put Python numbers in the basic scope? Maybe the author should first go through Python objects and then go deep into numbers, this makes it easier to understand. Here is a point of view:"Everything in Python is an object" PrincipleThis principle can be deeply embodied through the assignment of learning variables in the previous section. For details, refer to the learning entry notes in the next section.

Since there are not many numbers used in Python, just like learning JavaScript digital types, I think it is best to put them in the basics. This will pave the way for objects in the next section.

This section describes Python's numeric types, operations, and built-in functions related to numbers.

1. digit type

As mentioned in the previous section, Python variables do not need to define types because they are dynamic language and are dynamically determined based on the assigned values in the memory. If you want to classify variables in python, they can be divided:Variable type and immutable type.

Here we have sorted out the mutable and immutable knowledge:
(Maybe we can have a deep understanding of this after learning the next section. If we have learned C, it is like the C pointer art. If we have learned C #, it is similar to the nature of C # value type and reference type. if Javascript is learned, it is basically the same as Python. In general, all programming languages are centered around the basic core, it's just that they are different. We need to learn how to integrate them to give a different picture... But it feels so difficult .) Here we recommend an article: how to learn more than 600 programming languages immutable: whether the value in the memory can be changed, if it is immutable, when operating on the object itself, you must apply for another area in the memory (because the old area # immutable #), the old zone is discarded (if there are other ref, the ref number is reduced by 1, similar to hard-link in unix ).

Mutable: when you operate an object, you do not need to apply for memory elsewhere. You only need to apply for (+/-) continuously after this object, that is, its address will remain unchanged, but the region will become longer or shorter.

Unchangeable types include: string, interger, and tuple:

1 >>> var1 = 1 2 >>> var2 = var1 3 >>> var1, var2 4 (1, 1) 5 >>> id (var1), id (var2) # Same refers to the same memory area, with the same id of 6 (21200672,212 00672) 7 >>> var1 + = 1 8 >>> var1, var2 9 (2, 1) 10 >>> id (var1), id (var2)
# Because var1 and var2 data are immutable, after applying for another region for var1 + 1, id (var1) changes and id (var2) remains unchanged, ref number for this region-111 (21200660,212 00672) 12 >>>

Variable type: list, dict

1 >>> lst1 = [1, 3] 2 >>> lst2 = lst1 3 >>> lst1, lst2 4 ([1, 2, 3], [1, 2, 3]) 5 >>> id (lst1), id (lst2) 6 (28497360,284 97360) 7 >>> lst1.append (4) # variable type, after append, address will remain unchanged 8 >>> lst1, lst2 9 ([1, 2, 3, 4], [1, 2, 3, 4]) 10 >>> id (lst1), id (lst2) # lst1, lst2 always points to the same region 11 (28497360,284 97360) 12 >>> 13 be especially careful when operating on this type. The key of dict does not need to be of a variable type, and the key of a function does not need to be of a variable type when passing parameters. 14 eg: 15 >>> def dis (arg = []): 16... arg. append ('1') 17... print arg18... 19 >>> dis () 20 ['1'] 21 >>> dis () 22 ['1', '1'] 23 >>>> id (dis ()) 24 ['1', '1', '1'] 25 50524661526 >>> id (dis () 27 ['1', '1', '1', '1 ', '1'] 28 505246615

Reference: http://blog.chinaunix.net/uid-26249349-id-3080279.html

A number is an unchangeable type, that is, changing the value of a number will generate a new object. Of course, we do not need to consider this.

Python numeric types include integer, long integer, Boolean, double-precision floating-point, decimal floating-point, and plural.

(1) integer:

For example: 0101, 8,-3, 0X80,-0X78

(2) Boolean

True and False

(3) Long Integer

L (or l) is added after an integer. Currently, the integer and long integer are gradually unified. Only when you call the repr () function on it can you see L. If you call the str () function, you cannot see L:

>>> ALong = 11111111111111111111 >>> aLong (directly enter the variable name to display the value in the command line. In this case, the repr () function is actually called) 111111111111111111l> print aLong (in the command line, call the str () function if print is used before the variable) 11111111111111111111 >>>

(4) Double Precision Floating Point

Similar to double in C. The actual accuracy depends on the Python interpreter compiler.

(5), plural

 1 >>> aComplex=2.1+3j 2 >>> aComplex 3 (2.1000000000000001+3j) 4 >>> aComplex.real 5 2.1000000000000001 6 >>> aComplex.imag 7 3.0 8 >>> aComplex.conjugate() 9 (2.1000000000000001-3j)10 >>> 

More common python core programming

2. Operators

They are: +,-, *,/, %, **, // (floor Division)

It is very interesting that the division operation of python.

Implement the following code in js:

1 <script type="text/javascript">2 var a=1,b=2;3 var c=0.1,d=0.2;4 var result1=a/b;5 var result2=c/d;6 alert(result1);#0.5    7 alert(result2);#0.58 </script>

If we implement the following in Python:

>>>1/20>>>0.1/0.20.5

It's a bit strange. Python handles it like this;

Traditional Division: If it is an integer division, the traditional division will discard the decimal point and return an integer (floor Division). If one of the operands is a floating point, the actual division will be executed.

Real Division: Returns the real vendor, which will become the unified standard in the future Python version, but now we can implement it through from _ ure _ import division.

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

Floor Division: Starting from Python2.2, the new operator // is called floor division. No matter what type the operand is, the fractional part is discarded and an integer is returned.

>>> 1//20>>> 1.0//2.00.0>>> -1//2-1

If you are interested in other operators, go to Python core programming and the official documentation.

3. Number-related built-in functions

For more built-in functions, I will learn more about the built-in functions of numbers. They include:

(1) Standard built-in functions:

Cmp (), str (), type () can be used for any type

>>> Cmp () # Compare size-1 >>>> cmp () 1 >>> cmp () 0 >>> str (0xFF) # convert to the string '000000' >>> type (0xFF) # Return object type <type 'int' >>>> type ('str') <type 'str'>

(2) Conversion factory functions:

Int (), long (), float (), bool (), and complex () are all numeric functions. For example:

>>> '111''111'>>> int('222')222>>> long('222')222L>>> float('222')222.0>>> bool(1)True>>> complex(2)(2+0j)>>> bool(True)True>>> bool(454)True>>> bool(0)False

(3) functional functions

Returns the absolute value of a given parameter with abs:

>>> abs(-3)3

The coerce () Data Type converts two parameters:

>>> coerce(1,2)(1, 2)>>> coerce(1,-2)(1, -2)>>> coerce(1,123L)(1L, 123L)>>> coerce(1.3,123L)(1.3, 123.0)>>> coerce(1j,123L)(1j, (123+0j))

Divmod () returns the ancestor of the quotient and remainder by taking the remainder:

>>> divmod(10,2)(5, 0)>>> divmod(10,3)(3, 1)

Both pow () and ** can perform exponential operations:

>>> 5**225>>> pow(5,2)25

Round:

>>> round(3)3.0>>> round(3.45)3.0>>> round(3.999999,1)4.0

For more built-in functions, see the Python documentation.

 

 

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.