Python Numbers

Hii Folks, Now we are going to drive more under python. In this module, we will be learning What are python numbers. Today we will be learning one of the most required and important topics which are very useful and helps you to play with variable data types very easily. So let’s start.

Python Numbers

Python Numbers are the number data types that store numeric values. By changing the value of the number datatype it results in a memory allocated object. Therefore, they are said to be immutable data types.

Types of Number Data Types in Python

There are three types of number datatype which is as follows:

  • Int
  • Float
  • Complex

Let’s see more about each type

Python Numbers

Int Type Data in Python

These data types include all positive and negative numbers but not the fractions. In Python, there are no such limitations on the length of integers.

n=1234
type(n)

//Output
//<class 'int'>

Float Type

These Data types represent the float or decimal value. It can be either positive or negative. Float can also ‘’E’’which is the scientific tool used to indicate the power of 10.

Note: The accuracy on float numbers will be up to 15 decimal places. In sixteenth place, it will give inaccurate results.

n=12.34
type(n)

//Output
//<class float'>

Complex Type in Python

This is the type of data type which consists of a real and imaginary number.
For Example,3+5j is a complex number that consists of 3 as a real number and 5 is multiplied by an imaginary number (j).

n=234+123j
type(n)

//Output
//<class 'complex'>

Number type conversion

Various number types like int, float, and complex are also functions in Python. This function wraps one data type into another data type. Python performs typecasting internally and binds the values to their actual types. In some cases, we need to define data types explicitly.

For example, To input integer values we first use int typecasting in the input function.
int(input()):Converts string data type to integer data type.
Following functions are used to perform type conversion are:

Functions Descriptions
int (n) converts n to integer
float(n) converts n to float.
complex(n) converts n to complex.
complex (x,y) converts x and y to complex numbers with real part (x) and imaginary part(y).
num="9876"
type(num)
num1=int(num)
type(num1)
num2=float(num)
type(num2)

//Output
//<class 'int'>
//<class 'strt'>
//<class 'float'>

Python Number functions

There are various in-build functions that are used to perform specific operations.
Some function is as follows:

Functions Descriptions
abs(a) Positive distance between a and 0.
ciel(a) Ceiling value of a, smallest number that is not less than a.
cmp(a,b) It compares the values between a and b.

  • If a==b : returns 0
  • If a<b: return -1
  • If a>b: return 1
exp(a) Exponential of x i.e. ea.
fabs(a) The absolute value of a.
floor(a) It is known as the floor value of a. It gives the greatest integer value which is less than a.
log(a) It gives natural log value as a result.
log10(a) Log of x base 10 is returned.
max(a1,a2, ……,an) It returns the maximum value from the nth series as a result.
min(a1,a2,….,an) It returns the minimum values from the nth series as result.
modf(a1,a2,……,an) It returns the fractional and integer part on a floating-point number. These integer parts can also be returned as a float.
pow(a,b) It gives power to two values which are a and b.
round(a[,b]) The value of int Val a should be a round of b digits.
sqrt(y) It returns the square root of y.

Random number function

These functions are generally used in building gaming and to create uncertainty. It provides in-built functions to manipulate and generate random numbers. It can select a random element from the list or a sequence in Python.

The following Random numbers are as follows

Functions Descriptions
choice(s) Random numbers are given in return from a list, tuple, or dictionary.
randrange(start,stop,step) Random numbers are returned up to some particular range from the list.
random() The random value which is greater or equal to zero and less than one is returned.
seed([x]) This function is used to set the starting value that is used for generating random numbers.
shuffle(lis) This function is used to shuffle the passed list object.
uniform(a,b) It generates the random value which is greater than a and less than or equal to b.

Hope you enjoyed reading our content, In the Python Numbers module, we get to know the importance of typecasting and how it can be done. Using various functions we can typecast the element and by using various random numbers functions we can get any random number from the sequence of a list. To get the basic knowledge of Python you can go through Python Syntax.