Home » Python Numbers

Python Numbers

The number data types are used to store the numeric values inside the variables. Number objects are created when some value is assigned to a variable. For example, a = 5 will create a number object a with value 5.

Python also allows us to delete the reference of a number by using the del keyword. For example, del a will delete the object a declared above.

In Python, there are four types of numbers that can be assigned to a variable given as follows.

  • Int (signed Integer object) : they are the negative or non-negative numbers with no decimal point. There is no limit on an integer in python. However, the size of the integer is constrained by the amount of the memory our system has.
    Python also provides the support for various number systems like binary, hexadecimal, and octal values. To store values in different number systems, consider the following pattern.
Prefix Interpretation Base Example
0b
0B
Binary 2 0b10 = 2 (decimal)
0b1010 = 10 (decimal)
0B101 = 5 (decimal)
0o
0O
Octal 8 0o10 = 8
0o12 = 10
0O132 =decimal
0x
0X
Hexadecimal 16 0xA = 10
0xB = 11
0XBE = 190
  • float (floating point numbers) :
    The float type is used to store the decimal point (floating point) numbers. In python, float may also be written in scientific notation representing the power of 10. for example, 2.5e2 represents the value 250.0.
  • Complex (complex numbers)
    Complex numbers are of the form a+bj where a is the real part of the number and bj is the imaginary part of the number. The imaginary i is nothing but the square root of -1. It is not as much used in the programming.

Number type conversion

The various types of numbers like int, float, complex are also as the functions in python. It is similar to the wrapper classes of Java which is mostly used for typecasting. However, python internally perform type casting in the expression evaluation and binds the values to their actual types but there are some cases where we may have to define the data type of a value explicitly.

For example, while accepting the value from the user using the input() function, we have to convert the entered string by the user to integer explicitly if we need an integer value.

There are the following functions which are used to perform type conversion.

  • int (a) converts a to integer.
  • float(a) converts a to float.
  • complex(a) converts a to complex.
  • complex (a,b) converts a and b to complex numbers with real part a and imaginary part b.

Example

Output:

<class 'str'>  123456  <class 'int'>  190  

Python Number functions

There are various in-built functions which can be directly used to perform various calculations on the numbers defined in the program.

Function Description
abs(x) The (positive) distance between x and 0.
ciel(x) The ceiling value of x, i.e., the smallest integer that is not less than x.
cmp(x,y) Compares x and y. It returns 0 if x == y, -1 if x<y, 1 if x > y.
exp(x) The exponent of x that is ex.
fabs(x) The absolute value of x.
floor(x) The floor value of x, i.e., the greatest integer that is less than x.
log(x) The natural log value of x.
log10(x) The base 10 log of x is returned.
max(x1,x2, ……) The maximum of the sequence is returned.
min(x1,x2,…..) The minimum of the sequence is returned.
modf(x1,x2,……..) A tuple is returned containing the fractional and integer parts of a floating point number. The integer part is also returned as a float.
pow(x,y) Returns x ** y.
round(x[,n]) The value of x is rounded to n digits.
sqrt(x) The square root of x is retuned.

Random number functions

Random number functions are mainly used in games to create some uncertainty. Python provides various in-built functions to generate and manipulate random numbers. There are the following random number functions.

Function Description
choice(seq) A random item is returned from the passed list, tuple, or dictionary.
randrange(start,stop,step) A random integer is returned selected randomly from a list.
random() A random value is returned which is greater than or equal to 0 and less then 1.
seed([x]) It is used to set the starting value used in generating the random numbers. It returns none.
shuffle(lst) It shuffles the passed list object.
uniform(x,y) Generates a random integer which is greater than or equal to x and less than y.
Next Topic

You may also like