Numbers

Numbers

Numbers

Numbers:

In Python, there are three main types of numerical data types:

  1. Integer (int): This represents positive or negative whole numbers without any fractional part. For example: 5, -10, 100.
  2. Floating-point (float): This represents real numbers with a decimal point or exponential notation. For example: 14, 2.0e-2, 1.2E5.
  3. Complex (complex): This represents numbers with a real and imaginary part. The imaginary part is denoted by a suffix j or J. For example: 3 + 2j, 5 – 1.2j.

In Python, you can perform arithmetic operations on numerical data types using operators such as + (addition), (subtraction), * (multiplication), / (division), // (floor division), % (modulo), and ** (exponentiation).

Here are a few examples:

				
					a = 10
b = 3
c = 3.14
d = 2 + 3j

# addition
print(a + b)   # 13
print(c + a)   # 13.14
print(d + 2)   # (4+3j)

# subtraction
print(a - b)   # 7
print(c - a)   # -6.86
print(d - 2)   # (0+3j)

# multiplication
print(a * b)   # 30
print(c * a)   # 31.4
print(d * 2)   # (4+6j)

# division
print(a / b)   # 3.3333333333333335
print(c / a)   # 0.314
print(d / 2)   # (1+1.5j)

# floor division
print(a // b)  # 3
print(c // a)  # 0.0
# print(d // 2)  # TypeError: can't take floor of complex number.

# modulo
print(a % b)   # 1
# print(c % a)  # TypeError: unsupported operand type(s) for %: 'float' and 'int'.
# print(d % 2)  # TypeError: can't mod complex numbers.

# exponentiation
print(a ** b)  # 1000
print(c ** a)  # 314159265358.9793
print(d ** 2)  # (-5+12j)
				
			

Note that when performing arithmetic operations on numerical data types of different types (e.g., an integer and a floating-point number), Python automatically converts the operands to the widest type before performing the operation.

Join To Get Our Newsletter
Spread the love