DAV IET.
DALTONGANJ
Department of Computer Science & Engineering
AGINEERNG&TECHNOLOGY
ANINSTI UT 2008
ESTD.
DALTONGAN
Lab-Manual
DATA SCIENCE
Manual B-Tech CSE Page1
Data Science Lab
LIST OF EXPERIMENTS
S.No. Content PageN0.
Basic Python or R programming 3 to 4
Program to add two numbers
Maximum of two numbers
Program for factorial of a number
" Programto check Armstrong number
2 5 to 6
Array Programming
Program to find sum of array
Program to reverse an array
Program to find largest element of an array
3 List Programming 7 to 8
Program to swap to elements in a list
Program to find sum of numbers in a list
Program to find even numbers in a list
Program to do cumulative sum of a list
4 Matrix Program 9 to 11
Program to add two matrices
Program to multiply two matrices
Program to find transpose of matrix
" Program to subtract matrices
5 Dictionary program 12 to 13
Program tofund sum of all items in a dictionary
Program to merge two dictionary
" Program to remove all duplicate words in a
sentence
PRINCIPAL
Principal
D.A.V. Institute Of
Engineering &Technology
Medininagar, Palamau
B-Tech CSE
Data Science Lab Manual Page |2
Experiment No. 1
Aim: Basic Python or R programming
" Program to add two numbers
" Maximum of two numbers
" Program for factorial of a number
Program to check Armstrong number
Procedure: -
Python Code:
1. Program to add two numbers:
a= 10
b=20
Sum F a t b
print("Sum of", a, "and", b, "is:", sum)
Output:
Sum of 10 and 20 is: 30
2. Maximum of two numbers:
a= 10
b=20
ifa > b:
print(a, "isgreater than", b)
else:
print(b, "is greater than", a)
Output:
20 is greater than 10
Data Science Lab Manual B-Tech CSE
Page 3
3. Program for factorial of
a number:
num =5
fact =1
for i in range(1, num+1):
fact = fact * i
print("Factorial of", num, "is", fact)
Output:
Factorial of 5 is 120
4. Program to check Armstrong number:
num = 153
temp =num
Sum = 0
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //- 10
if num Sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Output:
153 is an Armstrong number
B-Tech CSE
Data Science Lab Manual Page 4
Experiment No. 2
Aim: Array Programming
" Program to find sum of array
Program to reverse an array
Program to find largest element of an array
Procedure: -
1. Program to find sum of array:
def sum array(arr):
Given an array of numbers, return the sumn of all the
elements in the array.
Sum =0
for num in arr:
Sum =num
return sum
2. Program to reverse an array:
def reverse array(arr):
Given an array, return a new array with the elements in
reverse order.
reversed arr=1
for iin range(len(arr)-1, -1, -1):
reversed arr.append(arr[il)
return reversed arr
3. Program to find largest element of an array
def find largest(arr):
Given an array of numbers, return the largest element in
Data Science Lab Manual B-Tech CSE
Page 5
the array.
largest = arr[0]
for num in arr:
if num > largest:
largest = num
return largest
YOu can use these functions by passing in your array as an argument. For example,
to find the sum of an array you could do:
arr = [1, 2, 3, 4, 5]
sum = sum array(arr)
print(sum) # Output: 15
Similarly, you can use the reverse array and find largest functions to reverse an
array and find its largest element, respectively.
B-Tech CSE Page |6
Manual
Data Science Lab
Experiment No. 3
Aim: List Programming
" Program to swap toelements in a list
" Program to find sum of numbers in a list
" Program to find even numbers in a list
Program to do cumulative sum of a list
Procedure: -
1. Program to swap to elements in a list:
def swapelements(Ist, i, j):
Given a list and two indices, swap the elements at the
indices in the list.
Ist[i], Ist[j]= Ist[jl, Ist[l
2. Program to find sum of numbers in a list:
def sum list(Ist):
Given a list of numbers, return the sum of all the elements
in the list.
Sum = 0
for num in Ist:
Sum t num
return sum
3. Program to find even numbers ina list:
def find even(lst):
Given a list of numbers, return a new list containing only
the even numbers.
even list =
for num in Ist:
if num %2 ==0:
even_list.append(num)
return even list
B-Tech CSE
Data Science Lab Manual Page 7
4. Program to do cumulative sum of a list
def cumulative sum(Ist):
Given a list of numbers, return a new list where each
element is the cumulative sum of the previous elements.
cumsum list=I
cumsum =0
for numn in Ist:
Cumsum t=num
cumsum list.append(cumsum)
return cumsum list
Youcan use these functions by passing in your list as an argument. For example, to
swap two elements in a list you could do:
Ist = [1, 2, 3, 4, 5]
swap_ elements(lst, 1, 3)
print(Ist) # Output: [1, 4, 3, 2, 5]
Similarly, you can use the sum list, find even, and cumulative sum functions to
find the sum of a list, find the even numbers in a list, and compute the cumulative
sum of a list, respectively.
B-Tech CSE
Data Science Lab Manual Page 8
Experiment No, 4
Aim: Matrix Program
Program to add two matrices
" Program to multiply two matrices
Program to find transpose of matrix
Program to subtract matrices
Procedure: -
1. Program to add two matrices:
def add matrices(mat1, mat2):
Given two matrices of the same size, return a new matrix that is
the sum of the two matrices.
rows = len(matl)
cols =len(mat1 [0])
result = [[0for jin range(cols)]for iinrange(rows)]
for iin range(rows):
for jin range(cols):
result[i]i]= mat1[i][i]+ mat2[i]i]
return result
2. Program to multiply two matrices:
def multiply_matrices(mat1,mat2):
Given two matrices, return a new matrix that is the product of
the two matrices.
rowsl =len(mat1)
colsl = len(mat1 [0])
rows2 =len(mat2)
cols2 =len(mat2[0])
B-Tech CSE Page |9
Data Science Lab Manual
result = [[0 for jin range(cols2)] for i inrange(rows 1)]
if cols1 rows2:
print("Matrices cannot be multiplied!")
else:
for i inrange(rows l):
forj in range(cols2):
for k in range(rows2):
result[i][i] t matl [iJk] *mat2[k][iJ
return result
3. Program to find transpose of matrices:
def transpose matrix(mat):
transpose of the
Given a matrix, return a new matrix that is the
original matrix.
rows = len(mat)
cols len(mat[0])
result [[O forjin range(rows)] for i inrange(cols)]
for iin range(rows):
forj in range(cols):
result[j][i] =mat[i][i]
return result
4. Program to subtract twO matrices:
def subtract matrices(mat1, mat2):
Given two matrices of the same size, return a new matrix that
is the difference between the two matrices,
rows = len(mat1)
cols = len(matl [0])
result =[0 for jin range(cols)] for i in range(rows))
for iin range(rows):
for j in range(cols):
Data Science Lab Manual B-Tech CSE
Page |10
result[i][i]= mat1[i][i]-mat2[i][i]
return result
Youcan use these functions by passing in your matrices as arguments. For example,
to add two matrices you could do:
matl = [[1, 2], [3,4]]
mat2 [[5, 6], (7, 81I
sum mat = add matrices(matl, mat2)
print(sum mat) # Output: [6, 8], [10, 12]]
Similarly, you can use the multiply_matrices, transpose_matrix, and
subtract_matrices functionsto multiply two matrices, find the transpose of a matrix,
and subtract two matrices, respectively.
Data Science Lab Manual B-Tech CSE Page |11
Experiment No. 5
Aim: Dictionary program
" Program to findsum of all items in a dictionary
Program to merge two dictionary
" Program to remove all duplicate words ina sentence
Procedure:
1. Program to find sum of all items in a dictionary:
def sum dict items(d):
Given a dictionary, return the sum of all its values.
return sum(d.values()
2. Program to merge two dictionaries:
def merge dicts(d1, d2):
Given two dictionaries, return a new dictionary that is
the result of merging the two dictionaries.
If a key exists in both dictionaries, the value in d2 will
overwrite the value in d1.
result = d1.copy()
result.update(d2)
return result
3. Program toremove allduplicate words in a sentence
def remove duplicates(sentence):
Given a sentence, return a new sentence that has all
duplicate words removed.
words = sentence.split)
Data Science Lab Manual B-Tech CSE Page |12
unique words set(words)
return " "join(unique_words)
You can use these functions by passing in your dictionaries or sentence as
arguments. For example, to find the sum of all items in a dictionary you could do:
d={'a': 1, 'b': 2, 'c: 3}
sum items = sum dict items(d)
print(sum items)
# Output: 6
Similarly, you can use the merge dicts and remove duplicates functions to merge
twodictionaries and remove duplicate words from a sentence,
respectively.
Data Science Lab Manual B-Tech CSE
Page | 13