Python - Numbers

01-Feb-2024

Get to know the basics of working with numbers in Python. Learn about different numeric types, perform arithmetic operations, and discover the flexibility of numerical data in Python.

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 = 123
b = 9876543210
c = -951



2. Floating-Point Numbers (float) :

    - Numbers with decimal points or expressed in scientific notation.

    -Scientific numbers denoted with a "e" to represent the power of ten can also represent float.


# Regular floating numbers
a = 15.0
b = 456.45
c = -986.45

# Scientific floating numbers
d = 19e7
e = 55E85
f = -28E15



3. Complex Numbers (complex) :

   - Numbers with both real and imaginary parts, expressed as a + bj.



a = 8 + 9j
b = 11j
c = -7j



Numbers Casting :

1. Casting to Integer ( int() ):



a = 6.84 # Type of a is float
b = int(a) # Type of b is int

print(b) # b = 6



2. Casting to Float ( float() ):



a = 3 # Type of a is int
b = float(a) # Type of b is float

print(b) # b = 3.0



3. Casting to Complex ( complex() ):



a = 5 # Type of a is int
b = complex(a) # Type of b is complex

print(b) # b = 5 + 0j


Python supports a variety of mathematical operations and has built-in utilities for converting between different numeric types. Comprehending the unique attributes of every numerical format is essential for accurate and effective Python coding.

Comments