 Python

# Python Numbers and Arithmetic Operations

Python is a powerful, efficient, and modern high-level programming language. When developing software systems, it is necessary to use numerical and arithmetic operations for performing calculations. Python provides a variety of numbers and arithmetic operations for this purpose. In this article, we will teach you about Python numbers, conversion of one data type into another data type, and arithmetic operations. The Spyder3 editor is used to create and run the Python script.

### Numbers in Python

Numbers are specified by their datatypes. Python has three types of numbers:

1. Integers
2. Floating point numbers
3. Complex numbers

These numbers are defined as integers, float, and complex classes in Python. The integers and the floating-point numbers are differentiated by the decimal point. The floating-point number has the decimal points, whereas the integers are without the decimal point. For instance, the 4 is an integer, while the number 4.0 is a floating-point number. The third type of number is complex numbers. A complex number is made up of a real and imaginary part. We will see the difference between the real and imaginary part later in this article. The complex numbers are written in the form of x + yj.

### Using Numbers in Python

Numbers can be used in Python in the following ways:

To declare a simple integer, write the variable name and assign it a number, like this:

num = 5

Similarly, to declare and use a floating-point number, write the variable name and assign it a number, as follows:

num = 5.5

Meanwhile, complex numbers are declared like this:

cNumber = 3+7j

In the given complex number, 3 is the real part, and 7 is the imaginary part. So, x is always the real part, and y is always the imaginary part.

We can also determine the type of number by using the type () function. The type () function returns the class of the variable or number. Let us look at some examples:

### Determining Integer Type

The variable x has value 5. This means that x belongs to the integer class. Output

The output is presented in the Python console. The output shows that the variable x belongs to the integer class. ### Determining Floating-Point Number Type

As discussed previously, a floating-point number is identified by a decimal point. An example of floating-point number is shown below: Output

The output is presented in the Python console. The output shows that the variable x belongs to the float class. ### Determining Complex Number Type

Now, we will declare a complex number and check its type. Output

The output is presented in the Python console. We will now find out the real and imaginary parts of the complex number, respectively. To find the real part of the number, use the ‘real’ keyword with the variable. Output

The output is presented in the Python console. To find the imaginary part of the complex number, we use the imag keyword with the variable name, as follows: Output

The output is presented in the Python console. ### Type Conversion of Numbers

In Python, we can convert one type of number into another. Sometimes, while performing the calculation, we may need to convert float into integer, and vice versa. We can even convert strings into numbers. The type conversion of the number can be done as follows: Output

The output is presented in the Python console. ### Arithmetic Operations

Mathematical operations, such as addition, subtraction, multiplication, and division, are called arithmetic operations. Python provides arithmetic operators that allow you to perform arithmetic operations. The following include arithmetic operations in Python, along with their meaning:

 Operator Meaning + Addition of two operands. – Subtraction of the first operand (left operand) from the second operand (right operand). * Multiplication of two operands. / Division of two operands. % Modulus. The remainder of the division of two operands. // Floor division. It always returns the floor value for integers and floats. ** Exponent. The first operand is raised to the power of the second operand.

### Using Arithmetic Operators in Python

Let us now see an example of using arithmetic operators in Python. We will perform an arithmetic operation using the arithmetic operator. In the given example, we have two variables: num1 and num2. We are performing an arithmetic operation on these two variables. Output

The output is presented in the Python console. ### Conclusion

This article explains how to use numbers and arithmetic operations in Python. There are different types of numbers in Python, and each number type is specified by a class. We used some simple examples to demonstrate the arithmetic operation to help Python learners learn more about numbers and arithmetic operations. 