Numbers in Python
Python encompasses diverse numeric types to handle different mathematical scenarios. Understanding these numeric types is pivotal for seamless mathematical operations and computational tasks.
1. Integers (int) :
- Whole numbers without any decimal points.
a = 123b = 9876543210c = -951
-Scientific numbers denoted with a "e" to represent the power of ten can also represent float.
# Regular floating numbers
a = 15.0b = 456.45c = -986.45# Scientific floating numbersd = 19e7e = 55E85f = -28E15
3. Complex Numbers (complex) :
- Numbers with both real and imaginary parts, expressed as a + bj.
a = 8 + 9jb = 11jc = -7j
Numbers Casting :
1. Casting to Integer ( int() ):
a = 6.84 # Type of a is float
b = int(a) # Type of b is intprint(b) # b = 6
2. Casting to Float ( float() ):
a = 3 # Type of a is intb = float(a) # Type of b is floatprint(b) # b = 3.0
3. Casting to Complex ( complex() ):
a = 5 # Type of a is intb = complex(a) # Type of b is complexprint(b) # b = 5 + 0j