Division in Python

Source: Internet
Author: User

The division of the number of shapes in the C + + language is performed by dividing the floor (the number of decimal parts). such as int a=15/10; The result of a is 1.

The same is true in Java, so it is necessary to force type conversions when dividing data for two int to return a floating-point data, such as float a = (float) b/c, where B and c are int data.

Python is divided into 3 divisions: traditional Division, Precision division, and floor removal.

Traditional Division

If it is an integer except the law performs a floor divide, if it is a floating-point number, the law performs a precise division.

>>>1/2  0  >>>1.0/2.0  0.5  

Exact division

Division always returns the real quotient, regardless of whether the operand is shaped or floating. This can be done by executing the From __FUTURE__ Import Division directive.

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

Floor removal

Starting with Python2.2, an operator//is added to perform the floor division://Divide regardless of the numeric type of the operand, the fractional part is always removed, returning the nearest number in the sequence of numbers that is smaller than the true quotient.

>>>1//20>>>1.0//20>>>-1//2.0-1

Built-in function divmod ()
Divmod (A, B), return (A//B,A%B)

>>>divmod (  0,1)  >>>divmod (3.14159,1.5)  (2.0,0.4159000000000002)  > >>5+6j//3+2j2+0j>>>5+6j%3+2j-1+2j>>>divmod (5+6J,3+2J)  ((2+0j), ( -1+2J))  

Division in Python

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.