Numbers
In Python, numbers are a core data-type essential for performing arithmetic operations and calculations. Python supports three types of numbers, including integers, floating-point numbers and complex numbers. Here’s an overview of each:
x = 5 # A positive integer
y = -23 # A negative integer
z = 0 # Zero is also considered an integer
a = 10.92 #Float
c = 4j # complex number
Sample Code for Arithmetic Operators
# Addition
a = 15
b = 4
res = a + b # Output: 19
# Subtraction
res = a - b # Output: 11
# Multiplication
res = a * b # Output: 60
# Division
res = a / b # Output: 3.75
# Floor Division
res = a // b # Output: 3
# Modulus (%)
res = a % b # Output: 3 (because 15 divided by 4 gives remainder 3)
# Exponentiation (**)
res = 2 ** 3 # Output: 8 (because 2 raised to the power of 3 is 8)
# Absolute Value (abs)
res = abs(-10) # Output: 10 (absolute value of -10)
# Round (round)
res = round(3.14159, 2) # Output: 3.14 (rounds to 2 decimal places)