Detailed Explanation of Knowledge Points of python Remainder Operator

  • 2021-07-03 00:37:22
  • OfStack

What is the python remainder operator?

The python remainder operator is%, which means modulo and returns the remainder of division.

Hypothetical variables: a=10, b=20:

Then b% a output result 0

Note:

The Python language supports the following types of operators:

Arithmetic operator Comparison (relation) operator Assignment operator Logical operator Bitwise operator Member operator Identity operator Operator priority

python rounding and remainder rules

1) The//operation preserves the lower bound of the integer when rounding, that is, it favors the smaller integer

2) int is to cut out the decimal part and keep only the previous integer

3) The round function follows the rule of 4 rounding and 5 entering

> > > 5//3
1
> > > -5//3
-2
> > > int(5.3)
5
> > > int(5.6)
5

> > > int(-5/3)
-1

> > > round(5.3)
5
> > > round(5.6)
6

4)% operator, the remainder sign is determined by the dividend when taking the remainder

> > > When-5% 3 # has only 1 minus sign, find a number between negative infinity and-5 that can be integer by 3, and the number closest to-5 is-6, so-5-(-6) = 1
1
> > > 5%-3
-1
> > > -5%-3
-2
> > > 5%3


Related articles: