Lib and Functions - NOTES
Lib and Functions - NOTES
1. Set of modules which have pre-built codes for performing some functionality.
2. There will be default libraries which will be loaded when we start Python.
3. Then, there are libraries which we have to load externally if we have to use a functionality of that library.
4. There are innumerable libraries in Python.
Functions
Library can be thought of as a container whereas functions are the actual blocks which hold the code.
Functions are nothing but a piece of code built to perform a specific function and are called as Built-in
functions.
We can also create our own functions which we are called as User Defined functions.
Some of the functions which you have already learnt in the previous academic year are
print()
input()
append()
insert()
pop()
Now, first let us see some functions in action which are from the default libraty and they do not need any
external libraries.
In [1]:
list1 = [1,2,3,999]
In [2]:
max_1 = max(list1)
Some Pointers
Now for the max function we should pass the name of a data structure such as a list. You can directly use the
max function and there is no hard and fast rule that you have to use a variable to save the result.
In [3]:
max(list1)
Out[3]:
999
In [4]:
max_1
Out[4]:
999
Similarly we are calculating the minimum value in the list using the min function.
In [5]:
min_1 = min(list1)
In [6]:
min_1
Out[6]:
1
In [7]:
list2 = ["A", "Mukund", "Zebra"]
In [8]:
max_alpha = max(list2)
print(max_alpha)
Zebra
In [9]:
min_alpha = min(list2)
print(min_alpha)
In [10]:
listt3 = [100,99,10,1,2,0,12]
list_sum = 0
for i in range(len(listt3)):
list_sum = list_sum + listt3[i]
In [11]:
list_sum
Out[11]:
224
We had to write a for loop and then we arrived at the solution. We also had to find the
length and iterate for those many times.
In [12]:
list_sum = sum(listt3)
In [13]:
list_sum
Out[13]:
224
In [14]:
a = [9,1,8,0,999]
a.sort()
In [15]:
a
Out[15]:
[0, 1, 8, 9, 999]
Till now the functions which we looked at were all in the default libraries which are pre-
loaded as soon as Python starts. However now we will look at those kind of functions for
whcih we have to use a different library.
To use any library in our program, we need to use the keyword "import" followed by the
library name. Below are some examples.
Math Library
In [16]:
import math
Suppose you need to get the exact value of pi. In that case you can use the pi value which is present in the
library called "math". Of course pi is not a function and is just a value which can then be stored in a variable and
can be used in various applications.
In [17]:
pi = math.pi
print("The value of pi is :", pi)
The value of pi is : 3.141592653589793
To use that function we need to write the library name and then a . symbol followed by the function name,
parentheses and inside the parentheses mention the arguments.
In [18]:
gcd = math.gcd(3,6)
print(gcd)
In [19]:
gcd_2 = math.gcd(12, 36)
print(gcd_2)
12
3125.0
numpy
In [21]:
import numpy as np
Arrays
Arrays are another data structure which are usually used to store data belonging to the same data type.
In many languages we can create arrays directly like how we created lists. However, in Python we use the
array function of numpy library.
Create an Array
In [22]:
arr = np.array([1, 2, 3, 4, 5])
print(arr)
for i in arr:
print(i)
[1 2 3 4 5]
1
2
3
4
5
[0 1 2 3 4 5 6 7 8 9]
In [24]:
arr1 = np.arange(1,10,1)
print(arr1)
[1 2 3 4 5 6 7 8 9]
In [25]:
arr1 = np.arange(2,20,2)
print(arr1)
[ 2 4 6 8 10 12 14 16 18]
3.0
Search in an Array
In [27]:
arr1 = np.array([1, 2, 3, 4, 5, 7, 8, 9 , 10])
result = np.where(arr1 == 3)
print(result)
(array([2], dtype=int64),)
<class 'list'>
In [30]:
arr3.sort()
In [31]:
print(arr3)
[0, 1, 1, 1, 2, 11, 12, 12, 19, 33, 44, 88, 99, 99]