How to find the remainder of division in Python?

Python is a simple, modern language for writing code. It has powerful libraries that can evaluate any expression. Python is the main competitor for Matlab and Octave. By launching Python interactively, the remainder of the division can be easily found by the user. But that is not all! Python can be a powerful calculator.

python division remainder

Operator concept

To easily find the remainder of a division in Python, you need to deal with some definitions. Operator - a sign or line that allows you to perform mathematical, bitwise, logical and other calculations. Expressions or numbers entered by the user to find the remainder of division in Python 3, the identity of a combination or comparison, are called operands.

The following types of operators are divided:

  • arithmetic;
  • bitwise;
  • brain teaser;
  • assignment operators;
  • comparisons
  • membership
  • identity.

Simply put, in the example "15 - 5" the operator is the "-" sign, the operands are 15 and 5. This is an arithmetic operation with integers. If we consider the expression “True and True”, then the operator here is “and”, and the operands are “True” and “True”. This example can be attributed to the logical type.

Integers and real numbers. Mathematical operations and output

If we consider mathematical operations on integers and fractional numbers, then the operators are +, -, *, /, **, //,%. With the first three, everything is clear. They denote, respectively, addition, subtraction, multiplication. The operator "**" indicates the need for exponentiation.

The signs of single (/) and double (//) division are different. If the first one gives a real number in the solution, then the second is necessary to find the integer part of the division. For example, 9 // 4 = 2. This operator corresponds to the div function in Turbo Pascal. But there is a pattern. The “/” sign will output an integer as a result if both the divisor and the dividend are also integers. To find the remainder of division in Python, you need to use the “%” operator. By analogy with the same "Turbo Pascal" "%" is comparable to the mod function. For example, 9% 2 = 1, i.e. in Python, the remainder of division in this case is 1. Consider more examples.

To produce division without a remainder, Python suggests using the divmod (x, y) function. In this case, x is a dividend, y is a divisor. For the expression divmod (9.3), the program will produce the following result (3.0). This means that the integer part of the division is 3, and the remainder is 0.

python no division

Mathematical operations can be performed without assigning a value to a variable. Then the result is issued automatically. If the code contains variable assignment, then you can display the result on the screen using the print statement.

Math module

For the convenience of users, developers offer a powerful math module that can work with any type of number and perform additional functions.

To connect the library, you need to write the following line at the beginning of the program code: import math. Such a command will allow loading all the functions available in the math module into the program code. Then, to connect a specific block from the library, you need to constantly prescribe it. For example, x = math.ceil (5.6).

If the program will often use the same block, then you can import only it. For example, you need to make a series of rounding to the nearest whole number in a larger direction. Then the code is written as follows: from math import ceil or from math import *. In both cases, the further code for rounding the result will not change.

python 3 remainder of division

Standard arithmetic functions in Python

To compute the remainder of integer division in Python, you do not always need to load the math library. Some features are built-in.

Built-in functions

Their purpose

Examples

int (x)

Turns a real number into an integer, i.e. the fractional part is "cut off".

int (5.3) >>> 5

int (5.6) >>> 5

int (5.987) >>> 5

round (x)

The expression is rounded to the nearest integer.

round (5.4) >>> 5.0

round (5.7) >>> 6.0

round (5.5) >>> 6.0

round (5.987) >>> 6.0

round (x, n)

Used to round the fractional part to n decimal places

round (5.8776,2) >>>

5.88

round (5.9876,3) >>>

5.988

abs (x)

Finds an expression module

abs (-7) >>> 7

abs (7.8) >>> 7.8

abs (-66.55) >>> 66.55

Functions for which you need to connect the library (you must initially enter from math import *) can be seen in the following table.

Functions

Their purpose

ceil (x)

The function is needed to round a number to a larger integer ("up")

floor (x)

Function is required to round a number to a smaller integer (“down”)

sqrt (x)

Computes the root of a number

log (x)

Needed to find the logarithm. If you specify the basis, then the calculation will be appropriate.

e

Displays the base of the natural logarithm

sin (x)

Calculation of trigonometric functions, where x is expressed in radians

cos (x)

tan (x)

asin (x)

acos (x)

atan (x)

atan2 (x, y)

Finds the polar angle of the point whose coordinates are given by x and y

degrees (x)

Needed to convert an angle from radians to degrees

radians (x)

Function needed to convert the angle specified in degrees to radians

pi

Displays the value of the constant π

As an example, below is the code using mathematical operators.

python integer division remainder

The result is shown as follows.

python division remainder

The math module has many more features. Here are the most common.


All Articles