Numbers in Python

I. Numeric data type

Python includes three numeric types to represent numbers: integers, floating point numbers, and complex numbers.

  • int represents integers, ex. 5
  • float represents real numbers, ex. 3.27
  • complex number has a “j” as the imaginary part, ex. 5j

II. Number conversion in Python

Sometimes we need to convert a number from one type to another to satisfy the requirements of an operator or a function.

  • To convert the number x to an integer: int(x).
  • To convert x to real number, you type float(x).
  • To convert the number x to a complex number with the real part x and the imaginary part 0, you type complex(x).
  • To convert the numbers x and y to complex numbers with the real part x and the imaginary part y, type complex(x, y).

III. Arithmetic Operators

In this section, we’ll take a look at how to do basic arithmetic operations.

We can do simple calculations with Python by entering a calculation directly into the Python console.

OperationResult
x + ysum of x and y
x - ydifference of x and y
x * yproduct of x and y
x / yquotient of x and y
x // yfloored quotient of x and y
x % yremainder of x / y
pow(x, y)x to the power y
x ** yx to the power y

Below are more examples.

1. Addition

We can use the + operator to add 2 numbers. The numbers don’t have to be the same type.

>>> 2 + 3.0
5.0

Adding two integers together always results in an int. If one of the operands is a float, the result is also a float.

2. Substraction

To subtract two numbers, we use a - operator between them:

>>> 5 - 2
3

>>> 9.0 - 4
5.0

Simmilar to adding 2 numbers, substracting two integers together always results in an int. If one of the operands is a float, the result is also a float.

The minus sign indicates a negative number.

>>> -10
-10

3. Multiplication

We can multiply numbers in Python using an asterisk * operator.

Multiplying two integers will result in an int, and multiplying a number with a float results in a float.

The parentheses determine which operations are performed first.

>>> 2 * (3 + 4)
14

4. Division

We use a forward slash / to indicate division. Division with the / operator always returns a float.

>>> 4 / 2
2.0

We can use int() to convert the result to interger:

>>> int(9 / 3)
3

If you try to divide by zero, Python produces an error.

>>> 10/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

5. Integer Division

Python has an integer division operator (//), also known as the floor division operator. It first divides the number and then rounds down to an integer.

If one of the operands is a float, the result returns a float.

>>> 4 // 2
2

>>> 3 // 2
1

>>> 5.0 // 2
2.0

6. Exponentiation

Besides addition, subtraction, multiplication, and division, we can also use exponentiation in Python.

The ** operator in Python is used to raise the number on the left to the power of the exponent of the right.

>>> 2**3
8

>>> 2 ** -1
0.5

7. Remainder

To calculate remainder, we use % operator

>>> 6 % 4
2

You can use the modulo function (%) with 10 to find the ones’ place of an integer.

>>> 2%10
2

IV. Shortern operator

In-place operators allow you to shorten code from x = x + 5 to x += 3. We can apply with other operators such as -, *, / and % as well.

These operators can be used on types other than numbers such as strings.

>>> x = "Hello"
>>> print(x)
Hello
>>> x += " World"
>>> print(x)
Hello World

V. Math Functions and Number Methods

Python provides an extensive list of built-in functions that wecan use to work with numbers. Here are some common functions:

OperationDescription
abs(x)Absolute value of x
ceil(x)Smallest integer that is not less than x
floor(x)Largest integer that is not more than x
log(x)lnx, with x> 0
log10(x)log10(x), với x> 0 .
max(x1, x2,…)max number
min(x1, x2,…)min number
round(x, n)rounding numbers to some number of decimal places
sqrt(x)square root of x, where x > 0
pow(x, y)x to the power y
abs()the absolute value of a number

1. Round numbers

We use round() to round a number to the nearest integer:

>>> round(5.2)
5

>>> round(5.8)
6

We can also round a number to a number of decimal places by passing a second argument to round():

>>> round(5.23456, 3)
5.235

>>> round(8.82132, 2)
8.82

2. Find min and max number

We use min() and max() to find min and max.

>>> min(2, 5)
2

max (4,5,6)
6

3. Find absolute value

To find the absolute value of a number in Python, we use abs():

>>> abs(3)
3

>>> abs(-5.0)
5.0

abs() function always return a positive number of the same type as its argument. So a the absolute value of an integer is a positive, and float is a positive float.

VI. Random numbers

random.randint(a,b) will return an integer between a and b (inclusive).

For example, random.randint(5, 10) could return any integer between 5 and 10 including both 5 and 10. We need to import random to use this method.

import random
num = random.randint(5, 10)
print(num)