**What is Python Numbers?**

The **Python Numbers** data types store numeric values. Because they are immutable, changing the value of a number data type results in a freshly allocated object. Python Number objects are produced when a value is assigned to them.

Python supports three types of numbers: complex numbers, integers and floating point numbers (decimals).

Every **numbers in Python** is an object of the **first class**. It implies that each numeric data type is implemented via a class definition. Using the `type()`

method, we may determine the data type of a number or the class to which a number belongs.

For instance, Python programming defines entire numbers as integers. The number’s data type can be confirmed as follows.

```
num1 = 1234
num2 = 12.34
print(type(num1))
print(type(num2))
```

**Output:**

```
<class 'int'>
<class 'float'>
```

The first number is an **integer**, thus the returned data type is `class 'int'`

. The second number is a **decimal**, therefore the returned data type is `class 'float'`

.

Now, we will examine each native **Python number data types individually**.

**Number Data Types in Python**

There are **three number data types in Python**:

**Integer****Float****Complex**

**1. Integer Data Type in Python**

Python’s **Integer Data Types** represent whole numbers like `0`

and negative integers like `-1`

. Defining an integer variable is as simple as assigning it a number.

```
var1 = 751
var2 = -8712
print (var1)
print ("The var1 data type is {}".format(type(var1)))
print (var2)
print ("The var2 data type is {}".format(type(var2)))
```

**Output:**

```
751
The var1 data type is <class 'int'>
-8712
The var2 data type is <class 'int'>
```

**Integer data types in Python** do not have a maximum value, unlike other computer languages. You can define a number of any size you choose, but the size is limited by the capabilities of your computer. For example, here we have built a number that contains 90 digits.

```
# 90 digit integer
var1 = 963021548778451203698745632103698521478521548915820542014778621036985102587695184747474714
print ("Here is the 99 digit integer with {} type".format(type(var1)))
print (var1)
```

**Output:**

```
Here is the 99 digit integer with <class 'int'> type
963021548778451203698745632103698521478521548915820542014778621036985102587695184747474714
```

Additionally, Python offers integer data types for various number systems, such as **binary, octal, and hexadecimal**.

We can represent binary numbers in Python by prefixing the number with `0b`

, as in `0b1110`

, hexadecimal numbers by prefixing the number with `0x`

, as in `0x1117`

, and octal numbers by prefixing the number with `0o`

, as in `0o1117`

.

```
var1 = 1117
var2 = 0x1117
var3 = 0o1117
var4 = 0b1110
print("Data Type of 1117 is {}".format(type(var1)))
print("Data Type of 0x1117 is {}".format(type(var2)))
print("Data Type of 0o1117 is {}".format(type(var3)))
print("Data Type of 0b1110 is {}".format(type(var4)))
```

**Output:**

```
Data Type of 1117 is <class 'int'>
Data Type of 0x1117 is <class 'int'>
Data Type of 0o1117 is <class 'int'>
Data Type of 0b1110 is <class 'int'>
```

You may note that the data type of every number, regardless of the number system, is int. Each binary, octal, and hexadecimal number is an instance of the `int`

class.

**2. Float Data Type in Python**

**Float Data Types in Python** is also known as** floating-point number**. Python employs double-precision floating-point numbers to represent decimal or real numbers. A number with floating-point precision may be positive, negative, or zero. Floating-point numbers contain one or more digits after the decimal point, much as decimal numbers.

```
var1 = 123.14
print (var1)
print ("The var1 data type is {}".format(type(var1)))
```

**Output:**

```
123.14
The var1 data type is <class 'float'>
```

Floats also representing real numbers, are represented with a decimal point separating the integer and fractional components. Floats may alternatively be expressed in scientific notation, where `E`

or `e`

represents the power of `10 (2.5e2 = 2.5 x 102 = 250)`

.

```
var1 = 35e3
var2 = 12E4
var3 = -87.7e100
print(type(var1))
print(type(var2))
print(type(var3))
```

**Output:**

```
<class 'float'>
<class 'float'>
<class 'float'>
```

**3. Complex Numbers in Python**

The **complex numbers** data type is also supported by Python. In Python, a complex number is represented as **a + i b**, where `a`

and `b`

are numeric values representing the real and imaginary parts of the complex number, respectively. Any English letter may be substituted for the letter `i`

.

```
var1 = 6+5j
var2 = 9j
var3 = -7j
print(type(var1))
print(type(var2))
print(type(var3))
```

**Output:**

```
<class 'complex'>
<class 'complex'>
<class 'complex'>
```

As seen in the above example, the returned data type is “**complex**“.

Using the `complex()`

** **function is another method for defining complex numbers. The `complex()`

** **function receives the real and imaginary components of the needed complex number and returns a complex number. The actual component will be passed as the first argument, while the imaginary part will be passed as the second parameter. Additionally, the real part is required while the imaginary part is optional.

```
var1= complex(3,2)
print(var1,type(var1))
```

**Output:**

`(3+2j) <class 'complex'>`

The `"real"`

and `"imag"`

characteristics of a complex number provide access to the real and imaginary components, respectively.

```
var1= complex(3,2)
realnum = var1.real
imagnum = var1.imag
print(var1)
print("Real number is ", realnum)
print("Imaginary number is ", imagnum)
```

**Output:**

```
(3+2j)
Real number is 3.0
Imaginary number is 2.0
```

The** **`conjugate()`

method can also be used to locate the conjugate of a complex number. The `conjugate()`

method returns the conjugate complex of a complex integer in the following format:

```
var1= complex(3,2)
conjugateNum= var1.conjugate()
print(var1)
print("Conjugate is", conjugateNum)
```

**Output:**

```
(3+2j)
Conjugate is (3-2j)
```

**Fraction Numbers in Python**

Using the **fraction numbers** module, fractions have been explicitly implemented in Python. Import the fractions module using the import statement below to use the fraction data type in Python.

`import fractions`

The `Fraction()`

function of the fractions module can be used to define a fraction object using integers in Python. **Two integers are required as input**. The first input is regarded as the numerator, while the second input is regarded as the denominator. After execution, the `Fraction()`

function returns a numerator/denominator fraction object.

```
import fractions
var1 = fractions.Fraction(1,2)
print("var1 answer is",var1)
print(type(var1))
```

**Output:**

```
var1 answer is 1/2
<class 'fractions.Fraction'>
```

Using the `Fraction()`

method, you may also convert a floating-point number to a fraction.

```
import fractions
var1 = fractions.Fraction(0.5)
print("var1 answer is",var1)
print(type(var1))
```

**Output:**

```
var1 answer is 1/2
<class 'fractions.Fraction'>
```

**Mathematical Functions**

The following list below is the **Mathematical Built-in Functions** that perform Mathematical calculations in Python.

No. | Function & Returns ( description ) |
---|---|

1 | The absolute value of `x:` the (positive) distance between `x` and zero. |

2 | The ceiling of `ceil(x)` `x:` the smallest integer not less than `x` |

3 | -1 if `cmp(x, y)` `x: < y` , `0` if `x == y` , or `1` if `x > y` |

4 | The exponential of `exp(x)` `x:e` |

5 | The absolute value of `fabs(x)` `x` . |

6 | The floor of `floor(x)` `x:` the largest integer not greater than `x` |

7 | The natural logarithm of `log(x)` `x` , for `x > 0` |

8 | The base-10 logarithm of `log10(x)` `x` for `x > 0` . |

9 | The largest of its arguments: the value closest to positive infinity`max(x1, x2,...)` |

10 | The smallest of its arguments: the value closest to negative infinity`min(x1, x2,...)` |

11 | The fractional and integer parts of `modf(x)` `x` in a two-item tuple. Both parts have the same sign as `x` . The integer part is returned as a `float` . |

12 | The value of `pow(x, y)` `x**y` . |

13 | `round(x [,n])` rounded to `n` digits from the decimal point. Python rounds away from zero as a tie-breaker: `round(0.5)` is `1.0` and `round(-0.5)` is `-1.0` . |

14 | The square root of `sqrt(x)` `x` for `x > 0` |

If you require additional information, please refer to the Python Mathematical Functions Documentation.

**Trigonometric Functions**

The following list below is the **Trigonometric Functions** that perform Trigonometric calculations in Python.

No. | Function and Description |
---|---|

1 | Return the arc cosine of `acos(x)` `x` , in radians. |

2 | Return the arc sine of `asin(x)` `x` , in radians. |

3 | Return the arc tangent of `atan(x)` `x` , in radians. |

4 | Return `atan2(y, x)` `atan(y / x)` , in radians. |

5 | Return the cosine of `cos(x)` `x` radians. |

6 | Return the Euclidean norm, `hypot(x, y)` `sqrt(x * x + y * y)` . |

7 | Return the sine of `sin(x)` `x` radians. |

8 | Return the tangent of `tan(x)` `x` radians. |

9 | Converts angle `degrees(x)` `x` from radians to degrees. |

10 | Converts angle `radians(x)` `x` from degrees to radians. |

Check the Python Documentation for additional details about Trigonometric Functions.

**Mathematical Constants**

The list below is the two **Mathematical constants in Python**.

No | Constant and Description |
---|---|

1 | The mathematical constant `pi` . |

2 | The mathematical constant `e` . |

**Random Numbers Functions in Python**

The list below is the most commonly used **Random Numbers Functions in Python**. This is used in simulations, games, security, testing, and privacy applications.

No | Function and Description |
---|---|

1 | A random item from a `choice(seq)` , `tuple` , or `string` . |

2 | A randomly selected element from `randrange ([start,] stop [,step])` `range(start, stop, step)` |

3 | A random `random()` `float r` , such that `0` is less than or equal to `r` and `r` is less than `1` |

4 | Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.`seed([x])` |

5 | Randomizes the items in a list without moving them. Returns None.`shuffle(lst)` |

6 | A random `uniform(x, y)` `float r` , such that `x` is less than or equal to `r` and `r` is less than `y` |

Python Documentation has additional information on Random Numbers.

**Summary**

In summary, you learned about **Type conversion of Python Numbers, Mathematical Functions as well as Trigonometric Functions, and Random Numbers functions** and you can do a few exercises to put what you learned into practice using our **Python Compiler**. I hope that this tutorial helped you understand how to use **Numbers in Python**.

You are one step closer to creating stronger, more effective codes now that you are aware of what **Python Numbers** are. In your code, start implementing all the functions you have studied today.

In the next post, “**Python Strings**,” you’ll learn about the different functions of Python strings and their uses.

Python Loops

Python Strings