Friday, June 19, 2020

Python Numbers

Hello guys! Welcome back to another section of my tutorial on Python. In this tutorial guide, we are going to discuss about Numbers in Python Programming.

Number types are used in storing numeric values. They are fixed data types, meaning that changing the values of a number data type will result to a newly allocated object.

Number objects are created when you assign a value to them. For example -

var1 = 5
var2 = 50

You can also delete the reference to a number object by using the del statement. The syntax for using the del statement is as follows -

del  var1[, var2[, var3[...., varN]]]]

The del statement can be used to delete a single object or multiple objects. For example -

del var
del var_a, var_b

RECOMMENDED POST: Python Pass Statement

Python supports four different numerical types -

  • int (signed integers) - They are often called just integers or ints. They are positive or negative whole numbers with no decimal point.
  • long (long integers) - Also called longs. This are integers with unlimited size, written like integers and followed by an uppercase or a lowercase L. 
  • float (floating point real values) - They are also called floats, they represent real numbers and are written with decimal point that can be used to divide the integer and fractional parts. Floats may also be in scientific notation, with either E or e indicating the power of 10.
  • complex (complex numbers) - They are often of the form a + bJ, which a and b are floats and then J or (j) represents the square root of -1 (which is an imaginary number). a is the real part of the number, and b is the imaginary part. Complex numbers are not used much in Python.


Below are some examples of numbers -


  • Python allows you to use a lowercase L with the long integer, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. It displays long integers with an uppercase L.
  • A complex number consists of an ordered pair of real floating point numbers that is denoted by a + bj, where a is the real part of the complex number and b is the imaginary part.

Number Type Conversion

Python converts numbers internally in an expression that contains mixed data types to a common type for evaluations. But sometimes, you will need to coerce a number explicitly from one type to another to satisfy the requirements of an operator or function parameter.

  • Type int(x) to convert x to a plain integer.
  • Type long(x) to convert x to a long integer.
  • Type float(x) in order to convert x to a floating point number.
  • Type complex(x) to convert the x to a complex number with real part x and imaginary part zero.
  • Type complex(x, y) to convert both x and y to a complex number with x as the real part and y as the imaginary part. 

Mathematical Functions

Python Lang. includes the following functions that are used to perform mathematical calculations.

Sr.No.Function & Returns ( description )
The absolute value of x: the (positive) distance between x and zero.
The ceiling of x: the smallest integer not less than x
3cmp(x, y)
-1 if x < y, 0 if x == y, or 1 if x > y
The exponential of x: ex
The absolute value of x.
The floor of x: the largest integer not greater than x
The natural logarithm of x, for x> 0
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 smallest of its arguments: the value closest to negative infinity
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.
The square root of x for x > 0

In our subsequent tutorials, we are going to be studying about the above listed functions.

Random Number Functions

Random numbers in Python are used for games, privacy, security, testing and stimulation applications. Python includes the following functions that are commonly used -

Sr.No.Function & Description
A random item from a list, tuple, or string.
2randrange ([start,] stop [,step])
A randomly selected element from range(start, stop, step)
A random float r, such that 0 is less than or equal to r and r is less than 1
Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.
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

In our subsequent tutorials, we are going to be studying about the above listed functions.

Trigonometric Functions 
Python Lang. includes the following functions that are used to perform trigonometric functions.

Sr.No.Function & Description
Return the arc cosine of x, in radians.
Return the arc sine of x, in radians.
Return the arc tangent of x, in radians.
4atan2(y, x)
Return atan(y / x), in radians.
Return the cosine of x radians.
6hypot(x, y)
Return the Euclidean norm, sqrt(x*x + y*y).
Return the sine of x radians.
Return the tangent of x radians.
Converts angle x from radians to degrees.
Converts angle x from degrees to radians.

In our subsequent tutorials, we are going to be studying about the above listed functions.

Mathematical Constants
A Python program also defines two mathematical constants -

Sr.No.Constants & Description
The mathematical constant pi.
The mathematical constant e.

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Mathematical Functions in Python.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.


Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.