You are on page 1of 11

Practice set based on math functions and number systems

Roll No: __AU2140139______ Name: ____SUJAL VIRANI___

Topics covered in the practice set are as under:


 Python frequently used math (Mathematical) functions
 Number representations and conversions (Binary, Octal, Hexadecimal)

Take three suitable examples for Sr. No. 1 to 19 and write corresponding output
below it.

1. abs(x) The method abs() returns absolute value of x.


>>> print "abs(-5) : ", abs(-5)
Output: abs(-5): 5

>>> print (abs(0))


Output: 0

>>> print(abs(7))
Output: 7
>>>

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


import math
math.ceil(10.3)
11
import math
print(math.ceil(2.6))

Output
3
>>>
import math
print(math.ceil(-5.4))

Output
-5
>>>

3. floor(x) The floor of x: the largest integer not greater than x


math.floor(10.7)
10
import math
print(math.floor(-5.4))

Output
-6
>>>
Import math
Print(math.floor(11.3))
Output
11
4. log(x) The natural logarithm of x, for x> 0
import math
print(math.log(2))

0.6931471805599453
>>>
import math
print(math.log(4))

1.3862943611198906
>>>
Import math
print(math.log(10))

2.302585092994046
>>>
5. log10(x) The base-10 logarithm of x for x> 0.
import math
print(math.log10(10))
1.0
>>>
import math
print(math.log10(100))
2.0
>>>
Import math
Print(math.log10(1000))
3.0
>>>
6. max(x1, x2,...) The largest of its arguments:
max(5,6,7,8,0)
8
>>> max(1,2,3,4)
4
>>> max(5,6,7,8)
8
>>> max(2,3,5,8)
8
>>>

7. min(x1, x2,...) The smallest of its arguments:


>>> min(2,3,4,5)
2
>>> min(232,34)
34
>>> min(90,56)
56
>>>
8. pow(x, y) The value of x**y.
pow(5,2)
25
>>> pow(9,0.5)
3.0
>>> pow(4,4)
256
>>> pow(3,5)
243
>>>

9. round(x [,n]) x rounded to n digits from the decimal point.


round(10.56789,2)
10.57
>>> round(12.34563,2)
12.35
>>> round(2.45667,3)
2.457
>>> round(3.567,0)
4.0
>>>
10. sqrt(x) The square root of x for x > 0
math.sqrt(25)
5.0
>>> math.sqrt(16)
4.0
>>> math.sqrt(8)
2.8284271247461903
>>> math.sqrt(15)
3.872983346207417
>>>

11. Random()
import random
>>> random.random()
0.22726385865134568
>>> random.random()
0.47022612121603313
>>> random.random()
0.8462540060772397
>>> random.random()
0.6273887977069514
>>>

12. math.factorial(x)
Return x factorial as an integer. Raises ValueError if x is not integral or is
negative.
>>> math.factorial(3)

>>> math.factorial(6)

720

>>> math.factorial(5)

120

>>>

13. math.gcd(*integers)
Return the greatest common divisor of the specified integer arguments. If any of
the arguments is nonzero, then the returned value is the largest positive integer
that is a divisor of all arguments. If all arguments are zero, then the returned
value is 0. gcd() without arguments returns 0.

>>> import math

>>> math.gcd(9,3)

>>> math.gcd(15,27)

>>> math.gcd(35,70)

35

>>>
14. math.exp(x) Return e raised to the power x, where e = 2.718281… is the
base of natural logarithms
>>> math.exp(2)
7.38905609893065
>>> math.exp(3)
20.085536923187668
>>> math.exp(-2)
0.1353352832366127
>>>.

15. math.cos(x) Return the cosine of x radians.

>>> math.cos(30)
0.15425144988758405
>>> math.cos(0)
1.0
>>> math.cos(1)
0.5403023058681398
>>>

16. math.sin(x) Return the sine of x radians.

>>> math.sin(3.14)

0.0015926529164868282

>>> math.sin(60)

-0.3048106211022167

>>> math.sin(30)

-0.9880316240928618
>>> math.sin(3.14/2)

0.9999996829318346

>>>

17. math.tan(x) Return the tangent of x radians.

>>> math.tan(3.14/3)

1.72992922008979

>>> math.tan(3.14/2)

1255.7655915007897

>>> math.tan(3.14/6)

0.576996400392873

>>>

Angular conversion

18. math.degrees(x) Convert angle x from radians to degrees.

19. math.radians(x) Convert angle x from degrees to radians.

Math Constants
math.pi The mathematical constant π = 3.141592…, to available precision.
math.e The mathematical constant e = 2.718281…, to available precision.
Number Representations:
Perform following operations of numbers.

Declare and print three variables of each number systems (Decimal, Binary, Octal and Hexadecimal
numbers).

1. Convert any three decimal numbers into the binary number system
o N1 = 4
o N1 in Binary = 100
o >>>bin(2)
o '0b10'
o >>> bin(3)
o '0b11'
o >>> bin(4)
o '0b100'
o >>>
2. Convert any three decimal numbers into the octal number system
o >>> oct(2)
o '0o2'
o >>> oct(10)
o '0o12'
o >>> oct(6)
o '0o6'
o >>>
3. Convert any three decimal numbers into the hexadecimal number system
>>> hex(34)
'0x22'
>>> hex(390)
'0x186'
>>> hex(53463878)
'0x32fcb46'
>>>
o
4. Convert following binary number into hexadecimal. 11011011110101
>>> hex(0b11011011110101)
'0x36f5'
o
5. Convert following hexadecimal number into binary. ABC
o >>> bin(0xABC)
o '0b101010111100'
o >>
6. Convert following octal number into binary. 543
o >>> bin(0o543)
o '0b101100011'
o

Examples of number system representations:

#Binary Representations
n3 = 0b101
print(n3)

#Octal representations
n6=0o51
print(n6)

#Hexa-decimal representations
n7 = 0xA
print(n7)

You might also like