What are the python division operators?

青灯夜游
Release: 2023-01-05 16:14:17
Original
58925 people have browsed it

Python division operator: 1. "/" operator, just get the result directly according to the conventional mathematical division calculation method; 2. "//" operator, take integer division and return the integer part of the quotient (Round down); 3. The "%" operator returns the remainder of division.

What are the python division operators?

The operating environment of this tutorial: windows7 system, python3.7 version, DELL G3 computer

python division operator

1. "/" operator

Divide - x is divided by y, and the result can be obtained directly according to the conventional mathematical calculation method .

2. Integer division operator '//'

#1) If both the dividend and the divisor are If the number is positive or both are negative, just like programming languages ​​​​such as C, just take the quotient directly;

2) If one of the dividend or divisor is a positive number and the other is a negative number, the result needs to be Take the remaining . The focus here is on the downward modulus. Beginners will definitely confuse it with other programming languages. To give a simple example, in the C language, the result of -5 dividing 2 is -2, but in the python program, the result of the operation needs to be closer to negative infinity, that is, the remainder is taken downwards, and the result of -5//2 is - 3.

Let’s give a few examples to make it clear to everyone.

3. Remainder operator '%'

This is also different from the remainder operator in C language program open.

Example mathematical formula: a÷b=c....d (a is the dividend, b is the divisor, c is the quotient, d is the remainder)

1) in python , if a and b are both positive numbers or both negative numbers, the result is obtained according to normal operations.

2) In python, if one of a and b is a positive number and one is a negative number, the remainder is calculated according to d=a-b*[a//b], .

For example: 5%-2 The running result is -1. According to the formula, the result of 5//-2 is -3, and the final remainder result is -1. When we usually calculate mathematical operations, the quotient of 5%-2 is -2, and the remainder is 1. However, in python, when it comes to the // operation, the remainder needs to be taken downwards, so in python the quotient should be -3, and the result will be remainder -1. .

[Related recommendations: Python3 video tutorial]

The above is the detailed content of What are the python division operators?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template