Python Numbers

Python Numbers, Data Types in Python, Python Programming Fundamentals, Python int Data Type, float Data Type, and Python complex Data Type.

Python Data Types – Numbers

Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.

There are three numeric types in Python:

1. int
2. float
3. complex

Variables of numeric types are created when you assign a value to them.

Example:

a = 123 # int
b = 12.3 # float
c = 123j # complex

We use type() function to verify the type of any object in Python.

Example:

print(type(a))
print(type(b))
print(type(c))

1. int

int, or integer, is a whole number, positive or negative, without decimals, of unlimited length (In Python, there is no limit to how long an integer value can be).

Example 1:

a = 1
b = 9871234561
c = -7687543

print(type(x))
print(type(y))
print(type(z))

Example 2: Performing arithmetic Operations on int type

a = 10
b = 5

# Addition
c = a + b
print(“Addition:”, c)

d = 20
e = 10

# Subtraction
f = d – e
print(“Subtraction:”,f)

g = 12
h = 3

# Division
i = g // h
print(“Division:”,i)

j = 3
k = 5

# Multiplication
l = j * k
print(“Multiplication:”,l)

m = 25
n = 5

# Modulus
o = m % n

print(“Modulus:”,o)

p = 6
q = 2

# Exponent
r = p ** q
print(“Exponent:”,r)

Output:

Addition: 15
Subtraction: 10
Division: 4
Multiplication: 15
Modulus: 0
Exponent: 36

2. float

float, or “floating-point number” is a number, positive or negative, containing one or more decimals.

Example:

a = 1.14
b = 12.1
c = -35.59

print(type(a))
print(type(b))
print(type(c))

float can also be scientific numbers with an “e” to indicate the power of 10.

Example:

x = 35e3
y = 12E4
z = -87.7e100

print(type(x))
print(type(y))
print(type(z))

Example : Performing arithmetic Operations on float type

a = 5.5
b = 3.2

# Addition
c = a + b
print(“Addition:”, c)

# Subtraction
c = a-b
print(“Subtraction:”, c)

# Division
c = a/b
print(“Division:”, c)

# Multiplication
c = a*b
print(“Multiplication:”, c)

Output:

Addition: 8.7
Subtraction: 2.3
Division: 1.71875
Multiplication: 17.6


3. complex

complex numbers are written with a “j” as the imaginary part.

Example:

a = 3+5j
b = 5j
c = -5j

print(type(a))
print(type(b))
print(type(c))


Python Step by Step Tutorial
Python Video Tutorial
Follow me on social media: