What Is Number Data Type in Python?

//

Angela Bailey

The number data type in Python is used to represent numerical values. Python supports different types of numbers, including integers, floating-point numbers, and complex numbers. In this tutorial, we will explore each of these types and learn how to work with number data in Python.

Integers

An integer is a whole number without any decimal point. It can be either positive or negative. In Python, we can define an integer by assigning a value to a variable.


number = 10
print(number)  # Output: 10

We can perform various operations on integers, such as addition, subtraction, multiplication, and division.


x = 5
y = 3

# Addition
result = x + y
print(result)  # Output: 8

# Subtraction
result = x - y
print(result)  # Output: 2

# Multiplication
result = x * y
print(result)  # Output: 15

# Division (returns float)
result = x / y
print(result)  # Output: 1.6666666666666667

# Floor Division (returns integer)
result = x // y
print(result)  # Output: 1

# Modulus (remainder of division)
result = x % y
print(result)  # Output: 2

# Exponentiation
result = x ** y
print(result)  # Output: 125

Floating-Point Numbers

A floating-point number is a number that contains a decimal point or an exponent representation using the letter ‘e’ or ‘E’. In Python, we can assign a floating-point value to a variable.


number = 3.14
print(number)  # Output: 3.14

Similar to integers, we can perform arithmetic operations on floating-point numbers.

Complex Numbers

A complex number is a number that consists of a real part and an imaginary part. In Python, we can define a complex number using the syntax a + bj, where a is the real part and b is the imaginary part.


number = 2 + 3j
print(number)  # Output: (2+3j)

We can perform various operations on complex numbers, such as addition, subtraction, multiplication, and division.


x = 2 + 3j
y = 1 - 2j

# Addition
result = x + y
print(result)  # Output: (3+1j)

# Subtraction
result = x - y
print(result)  # Output: (1+5j)

# Multiplication
result = x * y
print(result)  # Output: (8-1j)

# Division
result = x / y
print(result)  # Output: (-0.4+1.8j)

Conclusion

In this tutorial, we have learned about the number data type in Python. We explored integers, floating-point numbers, and complex numbers.

We also learned how to perform arithmetic operations on these types of numbers. Having a good understanding of number data types is essential for working with mathematical operations and calculations in Python.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy