Python. Mathematical (arithmetic) operators. Examples




Mathematical (arithmetic) operators. Examples


Contents


Search other websites:

1. Which operators (operations) are used for calculations in Python?

The Python language has a number of mathematical (arithmetic) operators for performing calculations in arithmetic expressions. The list of these operations in descending order of priority is as follows:

  • ** – exponentiation;
  • x – unary minus;
  • /, // – normal division, division with rounding down (the same priority);
  • % – remainder of the division;
  • * – multiplication;
  • – subtraction;
  • + – addition.

 

2. For which categories of numeric types can you use mathematical operators?

Mathematical operators can be applied to any number types, namely:

  • integers;
  • reals;
  • complex.

 

3. How is the type of the result of an operation calculated if the operation is binary and contains operands of various types?

Each operation returns a result of some type. If the operation is binary, then the type of result depends on the type of operands.

Two cases are possible here:

  • operands have the same type. In this case, the type of the result of the operation is the same type. An exception is the division / integer operands operation. If both operands are of integer type, then the result of the division operation will be of real type (floating-point number);
  • operands have different types. In this case, the type of the result of the operation will be determined in the following sequence: first, the type of both operands is converted to the type of a more complex operand; then for the operands, mathematics specific to this type are applied.

The Python interpreter determines the complexity of types in this sequence (from simple to more complex):

  • integer type;
  • real type;
  • complex type.

In Python 2.6 (if necessary) regular integers can be converted to long integers. In Python 3.0 and above, this ranking no longer exists and all integers are represented as long integers.

Example. When using two operands of different types, the following situations are possible. If one operand is an integer type and the other is a real type, then the result type will be a real type (since the real type is considered more complex). If one operand is of real type and the other is of complex type, then the result will be of complex type.



 

4. Operation exponentiation **. Example

The operation of exponentiation is denoted ** and is used for operands of any number types (integer, real, complex).

# Operation ** - exponentiation
# 1. For integers
a = 3
b = 2
c = a**b # c = 9
print("c = ", c)

# 2. For real numbers
a = 2.5
b = 3
c = a**b # c =   15.625
print('c = ', c)

# 3. For complex numbers
x = 1 - 2j
y = -4j
z = x**y # z =   (-0.011895629765035814+0.0009211666704635043j)
print('z = ', z)

# 4. In the 16th number system
a = 0xA1
b = 0x03
c = a**b # c =   4173281 - result in the 10th number system
d = hex(c) # d =   0x3fade1 - result in the 16th number system
print('c = ', c)
print('d = ', d)

# 5. In the 16th number system
a = 0o356
b = 0o2
c = a**b # c = 56644
d = oct(c) # d = 0o156504
print('c = ', c)
print('d = ', d)

# 6. In the 2th number system
a = 0b101
b = 0b110
c = a**b # c = 15625
d = bin(c) # d = 0b11110100001001
print('c = ', c)
print('d = ', d)

 

5. The operations of addition (+), subtraction (). Examples

The operations of addition + and subtraction are binary and are used for any number type.

# Operation addition +, subtraction -
# 1. Addition, substraction of integers
a=3
b=5
c = a+b # c = 8
d = a-b # d = -2

# 2. Addition, subtraction of integer and real numbers
a = 2.5
b = 7
c = a + b # c = 9.5
d = a - b # d = -4.5

# 3. Addition, subtraction of real numbers
x = 8.99902
y = 9.112356
z = x+y # z = 18.111376
v = x-y # v = -0.11333600000000033

# 4. Addition, subtraction of complex numbers, the result is a complex number
a = 7+8j
b = 3-2j
c = a + b # c = (10+6j)
d = a - b # d = (4+10j)

# 5. Addition, subtraction of numbers in hexadecimal notation
a = 0xFF5
b = 0xC9
c = a + b # c =   4286
d = a - b # d =   3884

# 6. Addition, subtraction of numbers in octal notation
a = 0o730
b = 0o50
c = a + b # c = 512
d = a - b # d = 432

# 7. Addition, subtraction of numbers in binary system
a = 0b0110110
b = 0b0001010
c = a + b # c = 64
d = a - b # d = 44

 

6. The operations of multiplication (*) and ordinary division (/). Example

The operations of multiplication * and division / are binary. These operations are used with operands of any numerical type (integer, real, complex).

# The operations of multiplication * and ordinary division /
# 1. For integers
a = 7
b = 4
c = a*b # c = 28 - integer result
d = a/b # d = 1.75 - real result

# 2. For reals
a = 7.5
b = 3.2
c = a*b # c =   24.0 - real result
d = a/b # d =   2.34375

# 3. For complex numbers
x = 2 + 2j
y = 3 - 1j
z = x*y # z =   (8+4j)
v = x/y # v =   (0.4+0.7999999999999999j)

# 4. In hexadecimal notation
a = 0xaff
b = 0x5b
c = a*b # c = 256165
d = a/b # d = 30.934065934065934

# 5. In octal number system
a = 0o356
b = 0o2
c = a*b # c =   476
d = a/b # d =   119.0

# 6. In binary number system
a = 0b111 # a = 35
b = 0b101 # b = 5
c = a*b # c = 35
d = a/b # d = 1.4

 

7. Features of the division operation / for various types of numbers. Example

If in the division operation / one of the operands is of real type, then the result will also be of real type (fractional number).

When using the division operation for integer operands in languages such as C/C++, C#, Java, the result of the operation will be integer. In versions of Python 2.6 and below, the operation of division of integer operands also gives an integer result, that is, after division

a = 9/6 # a = 1 - versions of Python 2.6 and below

variable a = 1.

In Python 3.0 and later, the result of the division operation / will always be a real number (regardless of the type of operands), that is, after division

a = 9/6 # a = 1.5 - versions of Python 3.0 and later

the variable a is of real type (a = 1.5). If in Python version 3.0 and higher, when integer operands are divided, an integer must be returned, then the division operation with rounding down // is used for this (see section 8).

Example.

# For integers
a = 7
b = 4
c = a/b # c = 1.75 - real result

a = 9
b = 3
c = a/3 # c = 3.0 - the result is also a real

 

8. Division operation with rounding down //. Example

Python 2.2 and later introduced the // division operation with rounding down, which in the case of integer operands returns an integer type result. In versions of Python 3.0 and later, this operation replaces integer division with the return of an integer result.

Example.

# Division operation // with rounding down, Python 3.7.0
# 1. For integers
a = 8
b = 5
c = a // b # c = 1

a = 17
b = 3
c = a//b # c = 5 - integer result with rounding down

# 2. For real number and integer number
x = 6.0
y = 2
z = x//y # z = 3.0 - real result

# 3. For real numbers
x = 5.5
y = 2.5
z = x//y # z = 2.0 - real result with rounding down

x = 13.7
y = 2.3333
z = x//y # z =   5.0 - real result with rounding down

 

9. The unary minus (–x) operation. Example

The operation “unary minus” makes a positive number negative, and a negative number positive. Operation is applied to any numeric type.

# unary minus -x
a = 15
b = -a # b = -15 - unary minus

a = -12.35
b = -a # b = 12.35

a = 0xFF
b = -a # b = -255 - decimal number system
c = hex(b) # c = -0xff

a = 0o775 # octal number system
b = -a
c = oct(b) # c = -0o775

a = 0b1111001
b = -a # b = -121 - decimal number system
c = bin(b) # c = -0b1111001 - binary number system

# complex number
z = 2 - 8j
z = -z # z = (-2+8j)

 

10. The operation of taking the remainder of the division (%). Example

 

# The operation of taking the remainder of the division
# 1. For integers
a = 7
b = 4
c = a%b # c = 3

a = 5
b = 9
c = a % b # c = 5

# 2. For real numbers
a = 5.5
b = 2.1
c = a%b # c =   1.2999999999999998

a = -8.1
b = -1.01
c = a%b # c = -0.019999999999999574

# 3. For hexadecimal numbers
a = 0xA8
b = 0x0F
c = a%b # c = 3 - decimal result
d = hex(c) # d = 0x3 - hexadecimal result

# 4. For octal numbers
a = 0o15
b = 0o25
c = a % b # c =   13
d = oct(c) # d =   0o15

# 5. For binary numbers
a = 0b10011
b = 0b101
c = a%b # c = 4
d = bin(c) # d =   0b100

 


Related topics