Python Python Tutorial File Handling NumPy Tutorial NumPy Random NumPy ufunc Pandas Tutorial Pandas Cleaning Data Pandas Correlations Pandas Plotting SciPy Tutorial



Python Numbers

Python is a high-level programming language that supports various data types, including numbers. In Python, numbers are used to represent numerical values. There are three types of numbers in Python: integers, floating-point numbers, and complex numbers.

Integers

Integers are whole numbers that can be positive, negative, or zero. In Python, integers are represented by the int data type. Integers can be created by assigning a value to a variable or by using the int() function.

Here is an example of creating an integer:

x = 5 
print(x) 

This code creates an integer variable named x and assigns it the value of 5. The print() function is then used to display the value of x.

Floating-Point Numbers

Floating-point numbers are decimal numbers that can be positive, negative, or zero. In Python, floating-point numbers are represented by the float data type. Floating-point numbers can be created by assigning a value to a variable or by using the float() function.

Here is an example of creating a floating-point number:

y = 3.14 
print(y) 

This code creates a floating-point variable named y and assigns it the value of 3.14. The print() function is then used to display the value of y.

Complex Numbers

Complex numbers are numbers that have a real part and an imaginary part. In Python, complex numbers are represented by the complex data type. Complex numbers can be created by assigning a value to a variable or by using the complex() function.

Here is an example of creating a complex number:

z = 2   3j 
print(z) 

This code creates a complex variable named z and assigns it the value of 2 3j. The print() function is then used to display the value of z.

Arithmetic Operations

Python supports various arithmetic operations on numbers, including addition, subtraction, multiplication, division, and modulus. These operations can be performed using the , -, *, /, and % operators, respectively.

Here is an example of performing arithmetic operations:

a = 10 b = 5 
print(a   b) 

# addition print(a - b) 
# subtraction print(a * b) 
# multiplication print(a / b) 
# division print(a % b) 
# modulus

This code creates two integer variables named a and b and performs various arithmetic operations on them using the print() function.

Conversion Functions

Python provides various conversion functions to convert one data type to another. These functions include int(), float(), and complex().

Here is an example of using conversion functions:

x = 5.6 
y = int(x) 

# convert float to int z = complex(x) 
# convert float to complex print(y) print(z) 

This code creates a floating-point variable named x and uses the int() and complex() functions to convert it to an integer and a complex number, respectively. The print() function is then used to display the converted values.

Conclusion

Python provides various data types to represent different types of values. Numbers are one of the most commonly used data types in Python. In this article, we have discussed the three types of numbers in Python: integers, floating-point numbers, and complex numbers. We have also discussed various arithmetic operations and conversion functions that can be performed on numbers in Python.

References

Activity