0% found this document useful (0 votes)
8 views

Lib and Functions - NOTES

Uploaded by

baabaasheep50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lib and Functions - NOTES

Uploaded by

baabaasheep50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Library

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.

Some examples are


math
numpy
matplotlib
sys
datetime

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.

min and max functions for numbers and string datatypes

Now, we will look at the min & max functions.

In [1]:
list1 = [1,2,3,999]

In [2]:
max_1 = max(list1)

Some Pointers

Whenever we use the function, it should be followed by the parentheses.


Whatever we write inside the function, it is called as an Argument.
Based on the definition of the function, some arguments may be mandatory and some may be optional.

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

Finding out the min and max of lists with strings.

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)

Sum of list elements


Write a program to find out the sum of list elements.

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.

Instead of that now we will use a simple "sum" function !!

In [12]:
list_sum = sum(listt3)

In [13]:
list_sum

Out[13]:
224

Sort elements of a List


In this example shown below we will use the sort function to sort the values of a list.

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

To get the value of pi

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 find the GCD - Greatest Common Divisor


This is one of the most common problems in Maths. To find the GCD. There is a function called gcd for that.

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)

Can also be written as

In [19]:
gcd_2 = math.gcd(12, 36)
print(gcd_2)

12

Return the power of a number.


In [20]:
num_1 = math.pow(5,5)
print(num_1)

3125.0

numpy
In [21]:
import numpy as np

The major usage of numpy is to create Arrays.

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

Another method to create an Array


In [23]:
arr1 = np.arange(10)
print(arr1)

[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]

Average of Array elements


In [26]:
array_average = np.average(arr)
print(array_average)

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),)

Find indexes whose values are odd


In [28]:
arr2 = np.array([1, 2, 3, 4, 5, 7, 8, 9 , 10])
result = np.where(arr1%2 == 1)
print(result)

(array([0, 2, 4, 5, 7], dtype=int64),)


Sort the Array
In [29]:
arr3 = [19,99,1,0,99,1,2,11,12,33,44,88,1,12]
print(type(arr3))

<class 'list'>

In [30]:
arr3.sort()

In [31]:
print(arr3)

[0, 1, 1, 1, 2, 11, 12, 12, 19, 33, 44, 88, 99, 99]

You might also like