Datatype derives type of data had ben stored in a variable.
Python provides several built-in numeric data types to handle various kinds of numerical operations. The primary numeric data types in Python are:
The whole numbers(positive, negative, zero) which lay on number line are called integers.
Example of Integer datatype:
a = 10
b = -5
x = 10
y = 3
# Addition
print(x + y) # Output: 13
# Subtraction
print(x - y) # Output: 7
# Multiplication
print(x * y) # Output: 30
# Division
print(x / y) # Output: 3.3333333333333335 (float division)
# Floor Division
print(x // y) # Output: 3 (integer division)
# Modulus
print(x % y) # Output: 1
# Exponentiation
print(x ** y) # Output: 1000
Python provides several built-in functions that work with integers.
Examples:
# Absolute value
print(abs(-10)) # Output: 10
# Power
print(pow(2, 3)) # Output: 8
# Maximum
print(max(1, 5, 3)) # Output: 5
# Minimum
print(min(1, 5, 3)) # Output: 1
# Sum
print(sum([1, 2, 3, 4, 5])) # Output: 15
# Convert string to integer
print(int("123")) # Output: 123
# Convert float to integer
print(int(9.99)) # Output: 9
You can convert other data types to integers using the int() function.
Examples:
# String to integer
s = "123"
n = int(s)
print(n) # Output: 123
# Float to integer
f = 3.14
n = int(f)
print(n) # Output: 3
It is a type which includes all decimal numbers. [ . ] is used to symbolize decimal point.
Example of Float datatype:
p = 3.14
g = 9.8
Python supports various operations on floats.
Basic Arithmetic Operations:
x = 7.5
y = 2.3
# Addition
print(x + y) # Output: 9.8
# Subtraction
print(x - y) # Output: 5.2
# Multiplication
print(x * y) # Output: 17.25
# Division
print(x / y) # Output: 3.260869565217391
# Floor Division
print(x // y) # Output: 3.0 (float division, returns float)
# Modulus
print(x % y) # Output: 0.5999999999999996
# Exponentiation
print(x ** y) # Output: 1793.1721886045393
You can convert other data types to floats using the float() function.
Examples:
# Integer to float
a = 10
b = float(a)
print(b) # Output: 10.0
print(type(b)) # Output: <class 'float'>
# String to float
s = "3.14"
c = float(s)
print(c) # Output: 3.14
print(type(c)) # Output: <class 'float'>
The numbers which are not present on number line is called imaginary number.
Example of Complex datatype:
c = 2 + 3j
d = complex(1, 2)
You can access the real and imaginary parts of a complex number using the .real and .imag attributes.
Examples:
z = 3 + 4j
real_part = z.real
imaginary_part = z.imag
print(real_part) # Output: 3.0
print(imaginary_part) # Output: 4.0
Python supports various arithmetic operations on complex numbers.
Examples:
z1 = 3 + 4j
z2 = 1 - 2j
# Addition
print(z1 + z2) # Output: (4+2j)
# Subtraction
print(z1 - z2) # Output: (2+6j)
# Multiplication
print(z1 * z2) # Output: (11-2j)
# Division
print(z1 / z2) # Output: (-1.4+1.8j)
You can convert other numeric types to complex using the complex() function.
Examples:
# Integer to complex
a = 10
z = complex(a)
print(z) # Output: (10+0j)
print(type(z)) # Output: <class 'complex'>
# Float to complex
f = 3.14
z = complex(f)
print(z) # Output: (3.14+0j)
print(type(z)) # Output: <class 'complex'>