Python Numbers

Table Of Contents:

  1. What is Python Number Data Type?
  2. Types Of Python Numbers.
  3. Number Type Conversion.
  4. Mathematical Functions.
  5. Random Number Functions.
  6. Mathematical Constants.

(1) What Is Python Number Data Type?

  • Number data types store numeric values like 45, 66.87, 0.452, etc.
  • They are immutable data types, which means that changing the value of a number of data types results in a newly allocated object in memory.
  • In Python you don’t have to declare the type of the variable, you only just need to assign the value to the variable.
  • Python will automatically assign it’s type.

Example-1

var1 = 100
var2 = 52.65
var3 = 5 + 0.8j

Note:

  • Here you don’t have to declare the type of the variables like Java, C, or C++.
  • Python will automatically take the type from its value.

Example-2

var1 = 100
var2 = 52.65
var3 = 5 + 0.8j

print(type(var1))
print(type(var2))
print(type(var3))
Output:
<class 'int'>
<class 'float'>
<class 'complex'>

(2) Types Of Numeric Data Type?

  • In Python, there are mainly 3 types of numeric data types.
  1. Integer
  2. Float
  3. Complex
  • They are defined as int, float, and complex classes in Python.

  • In order to find to which class the variable belongs you can use the type () function.
  • The isinstance() function is used to check if an object belongs to a particular class.

(a) Integer Data Types

  • Integer data types are used to store a whole number without decimals, like 35, -99, or 1345000.
  • The integer data type has two categories:
  1. Signed integers – can store both positive and negative values.
  2. Unsigned integers – can only store non-negative values

Example-1

a = 56

print('Type Of a : ',type(a))

b = -99

print('Type Of b : ',type(b))

c = 152365874566666466464

print('Type Of c : ',type(c))  
Output:
Type Of a :  <class 'int'>
Type Of b :  <class 'int'>
Type Of c :  <class 'int'>

(b) Float Data Types

  • The float data types are used to store decimal point values, like 35.3, -2.34, or 3597.34987.
  • It can store positive and negative decimal point values.

Example-1

a = 89.575

print('Type Of a : ',type(a))

b = -99.45

print('Type Of b : ',type(b))

c = 152365874566666466464.5464

print('Type Of c : ',type(c)) 
Output:
Type Of a :  <class 'float'>
Type Of b :  <class 'float'>
Type Of c :  <class 'float'>

(b) Complex Data Types

  • Complex numbers are written in the form, ( x + yj), where x is the real part and y is the imaginary part.
  • We can use complex numbers in scientific calculations.

Example-1

a = 3 + 7j

print('Type Of a : ',type(a))

b = -(5 + 9j)

print('Type Of b : ',type(b))

c = -10j

print('Type Of c : ',type(c))      

d = 0j

print('Type Of d : ',type(d))
Output:
Type Of a :  <class 'complex'>
Type Of b :  <class 'complex'>
Type Of c :  <class 'complex'>
Type Of d :  <class 'complex'>

(3) Number Types Conversion

  • When you accept input from a user it will always come as a string type.
  • Sometimes in an excel file, the numbers will be stored as a string type.
  • You need to explicitly convert it into a Numeric type.

  • At that time you can use the numeric functions.

Example-1

number = input('Enter A Number')

print(number)

print(type(number))
Output:
56
<class 'str'>

Note:

  • Here you can see that, even if you enter a number as an input, its type will be String.
  • Hence, we need to convert it into a Numeric type, to perform the numeric operation on it.

Types Of Numeric Converter

  1. int(x): to convert x to a plain integer.
  2. float(x): to convert x to a floating-point number.
  3. complex(x): to convert x to a complex number with real part x and imaginary part zero.

  4. complex(x, y): to convert x and y to a complex number with real part x and imaginary part y. x and y are numeric expressions.

Example-int(x)

number = '45'

print(type(number))

number = int(number)

print(type(number))
Output:
<class 'str'>
<class 'int'>

Example-float(x)

number = '99.99'

print(type(number))

number = float(number)

print(type(number))
Output:
<class 'str'>
<class 'float'>

Example-complex(x)

number = '9.05'

print(type(number))

number = complex(number)

print(type(number))
Output:
<class 'str'>
<class 'complex'>

Example-complex(x,y)

number1 = 5
number2 = 6

print(type(number))

number = complex(number1,number2)

print(type(number))
Output:
<class 'str'>
<class 'complex'>

(4) Mathematical Functions

  • Python provides inbuild mathematical functions.
  • That you can use it for doing mathematical operations.

(5) Random Number Functions

  • Random Number Functions are used to generate random numbers within a given range.

  • Random numbers are used for games, simulations, testing, security, and privacy applications.

  • Python includes the following functions that are commonly used.

(6) Mathematical Constants

  1. Python math.e constant: The math.e constant returns the Euler’s number: 2.71828182846.
  2. Python math.pi constant: The math.pi constant returns the value pi: 3.14159265359.
  3. Python math.tau constant: The math.tau constant returns the value tau: 6.283185307179586.
  4. Python math.inf constant: The math.inf constant returns of positive infinity.
  5. Python math.nan constant: The math.nan constant returns a floating-point nan (Not a Number) value.

Leave a Reply

Your email address will not be published. Required fields are marked *