Numbers:
In Python, there are three main types of numerical data types:
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.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.