Python Numbers

Number data types in Python stores numeric values. They are created when a certain value is assigned to them. Python provides four different kinds of Numbers to work on the numbers in a program:-

  • int (signed integers)
  • long (long integers, they can also be represented in octal and hexadecimal)
  • float (floating point real values)
  • complex (complex numbers)

What are different formats for Python Numbers?

TypeFormatDescription
inta = 10Signed Integer
longa = 345LLong integers, they can also be represented in octal and hexadecimal
floata = 45.67Floating point real values
complexa = 3.14JA number in the form of a+bj

Example of Python Numbers:-

We can use type() function to get the type of number like

Output:-

200 is of type <class ‘int’>
(3+5j) is of type <class ‘complex’>

While integers can be of any length, a floating point number is accurate only up to 15 decimal places (the 16th place is inaccurate). For computer programmers, Python can help with the representation in other base formats too.

Number SystemPrefix
Binary‘0b’ or ‘0B’
Octal‘0o’ or ‘0O’
Hexadecimal‘0x’ or ‘0X’

Let us have a look at an example

This will output

106
243
21

How to Convert between Different Number Types in Python?

Python provides a set of functions to convert in between different number types. Here are some of the list of operations:-

  • int(x) to convert x to a plain integer.
  • long(x) to convert x to a long integer.
  • float(x) to convert x to a floating-point number.
  • complex(x) to convert x to a complex number with real part x and imaginary part zero.
  • 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

How to work with Fractions in Python?

Python provides fractions module that you can import and use in your program. A fraction consists of numerator and denominator which are both integers. Let’s go through a piece of code to get a better understanding.

Output:-

5/2
2
1/3

What are Mathematical Functions supported in Python?

Following are the mathematical functions that are provided by Python.

S.No.FunctionDescription
1abs(x)The absolute value of x: the (positive) distance between x and zero.
2ceil(x)The ceiling of x: the smallest integer not less than x
3cmp(x, y)results -1 if x < y, 0 if x == y, or 1 if x > y
4exp(x)The exponential of x: ex
5fabs(x)The absolute value of x.
6floor(x)The floor of x: the largest integer not greater than x
7log(x)The natural logarithm of x, for x> 0
8log10(x)The base-10 logarithm of x for x> 0.
9max(x1, x2,…)The largest of its arguments: the value closest to positive infinity
10min(x1, x2,…)The largest of its arguments: the value closest to positive infinity
11modf(x)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.
12pow(x, y)The value of x**y.
13round(x [,n])x 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.
14sqrt(x)The square root of x

What are Trigonometric Functions supported in Python?

Following are the trigonometric functions that are provided by Python. For using trigonometic methods, do import math module before using it. For example, math.cos(x) print the cosine value of x.

S.No.FunctionDescription
1acos(x)The absolute value of x: the (positive) distance between x and zero.
2asin(x)The ceiling of x: the smallest integer not less than x
3atan(x)results -1 if x < y, 0 if x == y, or 1 if x > y
4atan2(y, x)The exponential of x: ex
5cos(x)The absolute value of x.
6hypot(x, y)The floor of x: the largest integer not greater than x
7sin(x)The natural logarithm of x, for x> 0
8tan(x)The base-10 logarithm of x for x> 0.
9degrees(x)The largest of its arguments: the value closest to positive infinity
10radians(x)The largest of its arguments: the value closest to positive infinity

How to work with Random Number in Python?

Python provides random module to work with random numbers in Python. Random number finds useful application in various programs including security, games, privacy, simulation etc. Following are the random number methods available in Python:-

S.No.FunctionDescription
1choice(seq)A random item from a list, tuple, or string.
2randrange ([start,] stop [,step])A randomly selected element from range(start, stop, step)
3random()A random float r, such that 0 is less than or equal to r and r is less than 1
4seed([x])Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.
5shuffle(list)Randomizes the items of a list in place. Returns None.
6uniform(x, y)A random float r, such that x is less than or equal to r and r is less than y

Let us have a look at an example random numbers:-

0.09099355418641308
0.2134112147897207
0.4875695623935453
7
red
SHARE Python Numbers

You may also like...

Leave a Reply

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

Share