Python Numbers

Python Numbers

Python numbers are different numeric values used in mathematical calculations. They are immutable data types because changing the value of number data type results in creation of a new object. 

 

 

Types of Python Numbers

  • int 
  • float
  • complex

int

Numbers of this category are positive and negative whole numbers which can be as long as needed because Python does not limit the size of int numbers.

Creating int Python numbers:

var_int=89
print(var_int," is of type ",type(var_int))

Output:

89  is of type  <class 'int'>

Arithmetic operations on int numbers:

# Python program to performs arithmetic operations on int numbers

num1=12
num2=23
# Arithmetic Addition
num3=num1+num2
print(num1," + ",num2," is ",num3)

num4,num5=45,13
# Arithmetic Subtraction
num6=num4-num5
print(num4," - ",num5," is ",num6)

num7,num8=12,5
num9=num7*num8
# Arithmetic Multiplication
print(num7," * ",num8," is ",num9)

num10,num11=12,3
num12=num10//num11
# Arithmetic Division
print(num10," // ",num11," is ",num12)

num13,num14=12,3
num15=num13%num14
# Arithmetic Modulus
print(num13," % ",num14," is ",num15)

num16,num17=2,5
num18=num16**num17
# Arithmetic Exponentiation
print(num16," ** ",num17," is ",num18)

Output:

12  +  23  is  35
45  -  13  is  32
12  *  5  is  60
12  //  3  is  4
12  %  3  is  0
2  **  5  is  32

 

float

This class has real numbers with floating point representation which contain a decimal point. It could also be appended with e or E followed by a positive or negative integer to represent scientific notation. Accuracy of a floating-point number is only up to 15 decimal places. 

Creating float Python numbers: 

# float type creation

var_float=3.4
print(var_float, " is of ",type(var_float))

Output:

3.4  is of  <class 'float'>

complex

This class contains numbers made up of real and imaginary parts. For example, 1+2j is a complex number where 1 is the real part or component of the complex number while 2 is the imaginary part or component of the complex number.

Creating complex Python number:

# complex number creation

var_complex=1+2j
print(var_complex," is of ",type(var_complex))

Output:

(1+2j)  is of  <class 'complex'>

 

 

Mathematical Functions for Python Numbers

Name Description
abs(num) The absolute value of num: the (positive) distance between num and zero.
ceil(num) The ceiling of num: the smallest integer not less than num.
cmp(num1,num2) -1 if num1 < num2, 0 if num1 == num2, or 1 if num1 > num2.
exp(num) The exponential of num: e**num.
fabs(num) The absolute value of num.
floor(num) The floor of num: the largest integer not greater than num.
log(num) The natural logarithm of num, for num> 0.
log10(num) The base-10 logarithm of num for num> 0.
max(num1,num2,…) The largest of its arguments: the value closest to positive infinity.
min(num1,num2,…) The smallest of its arguments: the value closest to negative infinity.
modf(num) The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.
pow(num1,num2) The value of num1**num2.
round(num[,n]) num rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.
sqrt(num) The square root of num for num > 0.

 

Share this post

Leave a Reply

Your email address will not be published.