# 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)

Contents

## What are different formats for Python Numbers?

Type | Format | Description |

int | a = 10 | Signed Integer |

long | a = 345L | Long integers, they can also be represented in octal and hexadecimal |

float | a = 45.67 | Floating point real values |

complex | a = 3.14J | A number in the form of a+bj |

Example of Python Numbers:-

1 2 3 4 5 6 7 8 9 | a = 10 #Signed Integer b = 345L #Long integers c = 45.67 #Floating point real values d = 3.14J #Complex number print(a) print(b) print(c) print(d) |

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

1 2 3 4 5 | a = 200 print(a, "is of type",type(a)) x = 3 + 5j print(x, "is of type",type(x)) |

Output:-

(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 System | Prefix |

Binary | ‘0b’ or ‘0B’ |

Octal | ‘0o’ or ‘0O’ |

Hexadecimal | ‘0x’ or ‘0X’ |

Let us have a look at an example

1 2 3 | print(0b1101010) print(0xF3) print(0o25) |

This will output

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.

1 2 3 4 | import fractions print(fractions.Fraction(2.5)) print(fractions.Fraction(2)) print(fractions.Fraction(1,3)) |

Output:-

2

1/3

## What are Mathematical Functions supported in Python?

Following are the mathematical functions that are provided by Python.

S.No. | Function | Description |

1 | abs(x) | The absolute value of x: the (positive) distance between x and zero. |

2 | ceil(x) | The ceiling of x: the smallest integer not less than x |

3 | cmp(x, y) | results -1 if x < y, 0 if x == y, or 1 if x > y |

4 | exp(x) | The exponential of x: e^{x} |

5 | fabs(x) | The absolute value of x. |

6 | floor(x) | The floor of x: the largest integer not greater than x |

7 | log(x) | The natural logarithm of x, for x> 0 |

8 | log10(x) | The base-10 logarithm of x for x> 0. |

9 | max(x1, x2,…) | The largest of its arguments: the value closest to positive infinity |

10 | min(x1, x2,…) | The largest of its arguments: the value closest to positive infinity |

11 | modf(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. |

12 | pow(x, y) | The value of x**y. |

13 | round(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. |

14 | sqrt(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. | Function | Description |

1 | acos(x) | The absolute value of x: the (positive) distance between x and zero. |

2 | asin(x) | The ceiling of x: the smallest integer not less than x |

3 | atan(x) | results -1 if x < y, 0 if x == y, or 1 if x > y |

4 | atan2(y, x) | The exponential of x: e^{x} |

5 | cos(x) | The absolute value of x. |

6 | hypot(x, y) | The floor of x: the largest integer not greater than x |

7 | sin(x) | The natural logarithm of x, for x> 0 |

8 | tan(x) | The base-10 logarithm of x for x> 0. |

9 | degrees(x) | The largest of its arguments: the value closest to positive infinity |

10 | radians(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. | Function | Description |

1 | choice(seq) | A random item from a list, tuple, or string. |

2 | randrange ([start,] stop [,step]) | A randomly selected element from range(start, stop, step) |

3 | random() | A random float r, such that 0 is less than or equal to r and r is less than 1 |

4 | seed([x]) | Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None. |

5 | shuffle(list) | Randomizes the items of a list in place. Returns None. |

6 | uniform(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:-

1 2 3 4 5 6 7 8 9 10 | import random print(random.random()) print(random.random()) print(random.random()) # random number between 0 to 10 print(random.randint(0, 10)) print(random.choice(['red', 'black', 'green'])) |

0.2134112147897207

0.4875695623935453

7

red