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?
|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:-
a = 10 #Signed Integer
b = 345L #Long integers
c = 45.67 #Floating point real values
d = 3.14J #Complex number
We can use type() function to get the type of number like
a = 200
print(a, "is of type",type(a))
x = 3 + 5j
print(x, "is of type",type(x))
(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.
|Binary||‘0b’ or ‘0B’|
|Octal||‘0o’ or ‘0O’|
|Hexadecimal||‘0x’ or ‘0X’|
Let us have a look at an example
This will output
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.
What are Mathematical Functions supported in Python?
Following are the mathematical functions that are provided by Python.
|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: ex|
|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.
|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: ex|
|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:-
|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:-
# random number between 0 to 10
print(random.choice(['red', 'black', 'green']))