Numbers in Python
In this article, we will see different Numerical Types, conversion of them and other functions available on Numbers. This article will be very brief on the numerical types. Once you get familiar with what all data types are and what methods available for them you can play with them and explore it in more details. Numeric values are stored using Numbers in Python. They are immutable data types.
Numerical Data Types:
- int (signed integers) − Int are positive or negative whole number without any decimal points. They also called integers or ints.
- long (long integers ) − The integers with unlimited size called long. They are written as integers and followed by an uppercase or lowercase L.
- float (floating point real values) − Integers with decimal point represented with float. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 10^{2} = 250).
- complex (complex numbers) − are of the form a + bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number). The real part of the number is a, and the imaginary part is b.
Below are some of the Number type conversions methods method available in python:
Number Type Conversion Methods:
- long(x) : To convert x to long
- int(x) : To convert x to int.
- float(x) : To convert x to a floating-point number.
- complex(x) : To convert x to a complex number x + 0j
- complex(x, y) : To convert x and y to a complex number x + yj
Mathematical methods supported by Numbers in Python:
abs(x) : The absolute value of x: the (positive) distance between x and zero. Ex: abs(-5) will be 5
ceil(x) : The ceiling of x: the smallest integer not less than x Ex: ceil(5.5) will be 6
cmp(x, y) : -1 if x < y, 0 if x == y, or 1 if x > y
exp(x) : The exponential of x wiz e^x
fabs(x) : The absolute value of x.
floor(x) : The floor of x: the largest integer not greater than x. Ex: floor(5.5) will be 5
log(x) : The natural logarithm of x, for x> 0
log10(x) : The base-10 logarithm of x for x> 0.
max(x1, x2,…) : The largest of its arguments: the value closest to positive infinity
min(x1, x2,…) : The smallest of its arguments: the value closest to negative infinity
pow(x, y) : The value of x**y.
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.
sqrt(x) : The square root of x for x > 0
In addition to this function, python also support trigonometric functions(Ex: acos(x), sin(x), radians(x)), random numbers functions( Used to generate random number. Ex: randrange ([start,] stop [,step]), random(), choice(seq), uniform(x,y)). Just play around this functions and get familiar with all of them.
That's it for now. We will see more in next article.
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
About the Author
Tags: python
Comments and Queries
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.