๐Ÿ“˜ MODULE II ยท SECTION 2.1

Section 2.1
NUMBERS

Text: Taming Python by Programming - Jeeva Jose
2.1 NUMBERS

Number data types store numeric values. Number objects are created when you assign a value to them. For example a = 1, b = 20. You can also delete the reference to a number object by using the del statement. The syntax of the del statement is as follows.

del variable1[,variable2[,variable3[...,variableN]]]]

You can delete a single object or multiple objects by using the del statement. For example

del a
del a,b

Python supports four different numerical types.

Integers can be of any length, it is only limited by the memory available. A floating point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points. 1 is integer, 1.0 is floating point number. Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.

Example Program

a,b,c,d=1,1.5,231456987,2+9j

print("a=",a)
print("b=",b)
print("c=",c)
print("d=",d)

Output

a=1 b=1.5 c=231456987 d=(2+9j)

2.1.1 Mathematical Functions

Python provides various built-in mathematical functions to perform mathematical calculations. The following Table 2.1 provides various mathematical functions and its purpose. For using the below functions, all functions except abs(x), max(x1,x2,..., xn), min(x1,x2,..., xn), round(x[n]) and pow(x,y) need to import the math module because these functions reside in the math module. The math module also defines two mathematical constants pi and e. Modules will be discussed in Chapter 5.

Table 2.1 Mathematical Functions

FunctionDescription
abs(x)Returns the absolute value of x.
sqrt(x)Finds the square root of x.
ceil(x)Finds the smallest integer not less than x.
floor(x)Finds the largest integer not greater than x.
pow(x,y)Finds x raised to y.
exp(x)Returns e^x ie exponential of x.
fabs(x)Returns the absolute value of x.
log(x)Finds the natural logarithm of x for x>0.
log10(x)Finds the logarithm to the base 10 for x>0.
max(x1,x2,...,xn)Returns the largest of its arguments.
min(x1,x2,...,xn)Returns the smallest of its arguments.
round(x,[n])In case of decimal numbers, x will be rounded to n digits.
modf(x)Returns the integer and decimal part as a tuple. The integer part is returned as a decimal.

Example Program

import math

print("Absolute value of - 120:", abs(- 120))
print("Square root of 25:", math.sqrt(25))
print("Ceiling of 12.2:", math.ceil(12.2))
print("Floor of 12.2:", math.floor(12.2))
print("2 raised to 3:", pow(2,3))
print("Exponential of 3:", math.exp(3))
print("Absolute value of - 123:", math.fabs(- 123))
print("Natural Logarithm of 2:", math.log(2))
print("Logarithm to the Base 10 of 2:", math.log10(2))
print("Largest among 10, 4, 2:", max(10, 4, 2))
print("Smallest among 10, 4, 2:", min(10, 4, 2))
print("12.6789 rounded to 2 decimal places:", round(12.6789, 2))
print("Decimal part and integer part of 12.090876:", math.modf(12.090876))

Output

Absolute value of - 120: 120 Square root of 25: 5.0 Ceiling of 12.2: 13 Floor of 12.2: 12 2 raised to 3: 8 Exponential of 3: 20.085536923187668 Absolute value of - 123: 123.0 Natural Logarithm of 2: 0.6931471805599453 Logarithm to the Base 10 of 2: 0.3010299956639812 Largest among 10, 4, 2: 10 Smallest among 10, 4, 2: 2 12.6789 rounded to 2 decimal places: 12.68 Decimal part and integer part of 12.090876: (0.09087599999999973, 12.0)

2.1.2 Trigonometric Functions

There are several built-in trigonometric functions in Python. The function names and its purpose are listed in Table 2.2.

Table 2.2 Trigonometric Functions

FunctionDescription
sin(x)Returns the sine of x radians.
cos(x)Returns the cosine of x radians.
tan(x)Returns the tangent of x radians.
asin(x)Returns the arc sine of x, in radians.
acos(x)Returns the arc cosine of x, in radians.
atan(x)Returns the arc tangent of x, in radians.
atan2(y,x)Returns atan(y / x), in radians.
hypot(x,y)Returns the Euclidean form, sqrt(x*x + y*y).
degrees(x)Converts angle x from radians to degrees.
radians(x)Converts angle x from degrees to radians.

Example

import math

print("Sin(90):",math.sin(90))
print("Cos(90):",math.cos(90))
print("Tan(90):",math.tan(90))
print("asin(1):",math.asin(1))
print("acos(1):",math.acos(1))
print("atan(1):",math.atan(1))
print("atan2(3,2):",math.atan2(3,2))
print("Hypotenuse of 3 and 4: ",math.hypot(3,4))
print("Degrees of 90: ",math.degrees(90))
print("Radians of 90: ",math.radians(90))

Output

Sin(90): 0.893996663601 Cos(90): - 0.448073616129 Tan(90): - 1.99520041221 asin(1): 1.57079632679 acos(1): 0.0 atan(1): 0.785398163397 atan2(3,2): 0.982793723247 Hypotenuse of 3 and 4: 5.0 Degrees of 90: 5156.62015618 Radians of 90: 1.57079632679

2.1.3 Random Number Functions

There are several random number functions supported by Python. Random numbers have applications in research, games, cryptography, simulation and other applications. Table 2.3 shows the commonly used random number functions in Python. For using the random number functions, we need to import the module random because all these functions reside in the random module.

Table 2.3 Random Number Functions

FunctionDescription
choice(sequence)Returns a random value from a sequence like list, tuple, string etc.
shuffle(list)Shuffles the items randomly in a list. Returns none.
random()Returns a random floating-point number which lies between 0 and 1.
randrange([start,]) stop [step])Returns a randomly selected number from a range where start shows the starting of the range, stop shows the end of the range and step decides the number to be added to decide a random number.
seed([x])Gives the starting value for generating a random number. Returns none. This function is called before calling any other random module function.
uniform(x,y)Generates a random floating-point number n such that n > x and n < y.

Example

import random
s = 'abcde'
print("choice(abcde):", random.choice(s))
list = [10,2,3,1,8,19]
print("shuffle(list):", random.shuffle(list))
print("random.seed(20):", random.seed(20))
print("random():", random.random())
print("uniform(2,3):", random.uniform(2,3))
print("randrange(2,10,1):", random.randrange(2,10,1))

Output

choice(abcde): b shuffle(list): None random.seed(20): None random(): 0.905639676175 uniform(2,3): 2.68625415703 randrange(2,10,1): 8

๐Ÿ“Œ Summary of Section 2.1