CIT 101 Py Lesson 6 Final
CIT 101 Py Lesson 6 Final
CIT 101 Py Lesson 6 Final
Don’t forget that every object in Python has a unique identity, a type, a value, behaviours and an address in
memory. So at the top level you get a collection of packages. A package can contain sub-packages and
modules.
So far we have experienced the use of a few built-in functions such as print(), input(), int(), float(), bool(),
abs(), round() etc. We also have discussed a very little about structures such as ‘list’, ‘tuple’, ‘dict’ and ‘set’
and their use. These things are readily available for use as a built-in part of the Python’s Interactive shell.
Python’s Interactive shell is the interface of Python through which we interact with Python. We give
commands to solve our problems and Python provides answers to them.
100000
Suppose if you want to know how many digits are there in 2 (Your Problem), you present it to Python as
follows and Python will provide the solution as 301030 in mostly 2 to 3 seconds.
>>> print(len(str(2**1000000)))
301030
Page 1 of 9
Built-in module
Basically the built-in module contains the following:
built-in functions
built-in constants
built-in types (There are 14 types and we have studied about 7 (essentials)
built-in exceptions
Exception
In addition to ‘built-ins’ Python contains many more things. Exception - Runtime Slip
Some of them belong to Python’s Standard Library.
Standard Library
Python’s Standard Library itself contains a wide range of modules. As they are not integrated into Python’s
shell we have to import them as and when required.
Modules contain program elements such as ‘classes’, ‘functions’, ‘constants’ , ‘variables’ or even ‘bock of
codes’
Python Standard Library contains well over 320 different modules at present. They provide different services
to Python users. There may be many modules to do the same thing. But their objectives and purposes are
different.
Try 1: Use the normal way of addition to calculate 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1 (add 0.1 10
times.) Issue the following command and look at the result.
Try 2: Use the built-in function sum() to calculate 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1 (add 0.1 10
times.) Issue the following command and look at the result.
Try 3: Use a function available in the standard library. So we are going to use ‘fsum()’ in the math module.
Since the math module is not pre-loaded we have to import it.
Page 2 of 9
Importing Modules
(I will discuss more about ‘pip3’ when you need it. So don’t’ worry about it now)
Note: If you want to get a list of modules in the standard Library, then issue the following command.
>>> help('modules')
The users can create their own modules and functions and make their own package.
1. sqrt(x) => Returns the square root of x (x>0). x can be an integer or a floating point number
Examples:
>>> math.sqrt(4.361) # Display the square root of 4.361 (Floating point number)
2.0883007446246817
>>> math.sqrt(12*0.03/5) # Display the square root of the given expression
0.2683281572999748
N.B. There is no function available in the in built-in module to find the square root of a number.
Don’t forget that there is an invisible Super Power behind you that is your common sense.
Page 3 of 9
2. ceil(x) => Returns ceiling value of x. That is the smallest integer x. x can be an integer or a
floating point number.
Examples:
Examples:
4. trunc(x) => Returns the real value of x truncated to an integer. (Removes the decimal part
completely.)
Examples:
Page 4 of 9
Note: To verify that (trunc() returns an integer value
5. fsum(Iterable) => Returns an accurate floating point sum of values in the iterable.
Examples:
>>> .3+.3+.3+.3+.3+.3+.3+.3+.3+.3
2.9999999999999996 # We expect 3.0 – not accurate
>>> sum([.3,.3,.3,.3,.3,.3,.3,.3,.3,.3])
2.9999999999999996 # We expect 3.0 – not accurate
>>> math.fsum([.3,.3,.3,.3,.3,.3,.3,.3,.3,.3])
3.0 # Exactly 3.0 which is accurate
Factorial is the product of all positive integers less than or equal to a given positive integer.
We write factorial n as n! = n.n-1.n-2.n-3. … 3.2.1
Thus
1! = 1
2! = 2x1 = 2
3! = 3x2x1 = 6
4! = 4x3x2x1 = 24
5! = 5x4x3x2x1 = 120
Manual calculations become difficult as it comes to large integers. So we use Python to do it.
Examples:
Page 5 of 9
7. gcd(* integers) => Returns the greatest common denominator of the specified integer arguments.
8. lcm(* integers) => Returns the least common multiplier of the specified integer arguments.
9. prod(iterable, start = 1) => Returns the product of all elements in the input iterable. Default
value of start = 1
51451872591913119192091351920851916554156129782019172111854
The number e, sometimes called Euler's number, is an important mathematical constant approximately
equal to 2.71828. It is one of the universal constants. The value of ‘e’ used by Python is as follows. ‘e’ is an
irrational number.
is the ratio of the circumference of any circle to the diameter of that circle. The value of pi is approximately
3.141592... (Can be approximated crudely to 22/7 which is not accurate but 355/113 is more accurate) is
an irrational number. It is one of the universal constants. The value use by Python is as follows.
Page 6 of 9
13. log(x [,base]) => Returns the natural logarithm for x.
14. log10(x) => Returns the base 10 logarithm more accurate than log(x, 10).
>>> math.pow(123.45,3) # Display the value of 123.45 to the power 3 using pow()
1881365.963625
>>> math.pow(123.45,15) # Display the value of 123.45 to the power 3 using pow()
2.3570370761335754e+31
Page 7 of 9
A
16. hypot(*coordinate) => Returns the hypotenuse of a right angled triangle.
C = (a2+b2)
b Hypotenuse
C B
a
>>> math.hypot(3,4) # Display the hypotenuse of the right angled triangle whose other two sides are 3 and 4.
5.0
>>> math.hypot(9,12) # Display the hypotenuse of the right angled triangle whose other two sides are 9 and 12.
15.0
>>> math.hypot(3.4,5.6) # Display the hypotenuse of the right angled triangle whose other two sides are 3.4 and 5.6.
6.551335741663681
17. dist(p, q) => Returns the Euclidean distance of two points p and q.
The Euclidean distance between P(3,4) and Q(6,8) = [(6-3) + (8-4) ] = (3 + 4 ) = (9+16) = 5
2 2 2 2
>>> math.dist([6],[3]) # Display the x component of the Manhattan distance between P(3,4) and Q(6,8)
3.0
>>> math.dist([4],[8]) # Display the y component of the Manhattan distance between P(3,4) and Q(6,8)
4.0
>>> math.dist([3,4],[6,8]) # Display the Euclidean distance between P(3,4) and Q(6,8)
5.0
Angular Measurements
We are accustomed to measure an angle in degrees (e.g. 30 ). But in technology mostly we measure angles
in ‘radians’. (E.g. 30 = 0.523599 rad )
180 = radians
Therefore we use the following conversion:
1 = /180 radians
18. degrees(x) => Returns the degree for a given value of x in radians.
Page 8 of 9
19. radians(x) => Returns the radians for a given value of x in degrees.
Page 9 of 9