PYTHON NUMBERS

There are three types of numeric numbers in python:

  • int
  • float
  • complex

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

a = 1    #int
b = 2.8  #float
c = 1j   #complex

To verify the type of any object in Python, use the type() function:

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

Integer Numbers(int)

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
In Python, integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision,
e.g. 0, 100, -10. The followings are valid integer literals in Python.
Integers can be binary, octal, and hexadecimal values.
All integer literals or variables are objects of the int class.
Examples

a = 1
b = 34732817
c = -28354

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

Float Numbers(float)

Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
The float type in Python represents the floating point number.
Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts.
For example, 97.98, 32.3+e18, -32.54e100 all are floating point numbers.
Example

x = 236.4
y = 2137.3564
z = -33.4013

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

Complex Numbers(complex)

Complex numbers are written with a "j" as the imaginary part:
A complex number is created from real numbers.
Python complex number can be created either using direct assignment statement or by using complex () function.
Complex numbers which are mostly used where we are using two real numbers.
Example

i = 3+7k
j = 27l
k = 32f

print(type(i))
print(type(j))
print(type(k))

Types of Conversions:

You can convert from one type to another with the int(), float(), and complex() methods:
Example

a = 372        #int
b = 34.2345    #float
c = 37j        #complex


#convert from int to float:
x = float(a)

#convert from float to int:
y = int(b)

#convert from int to complex:
z = complex(a)

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

print(type(a))
print(type(b))
print(type(c))
note:You cannot convert complex numbers into another number type.

Random Numbers

Python does not have a random() function to make a random number,
But Python has a built-in module called random that can be used to make random numbers:
Example
Import the random module, and display a random number between 1 and 9:

import random
print(random.randrange(1,10))