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

Python lab 7

This document is a course manual for the Advanced Python Programming Lab at Vivekanandha College of Arts and Sciences for Women, detailing the syllabus for the M.Sc in Computer Science program. It includes various Python programming examples using the NumPy library, covering matrix operations, binary operations, mathematical functions, and string operations. The manual is intended for the first semester of the academic year 2024-25, with effective date noted as August 21, 2024.

Uploaded by

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

Python lab 7

This document is a course manual for the Advanced Python Programming Lab at Vivekanandha College of Arts and Sciences for Women, detailing the syllabus for the M.Sc in Computer Science program. It includes various Python programming examples using the NumPy library, covering matrix operations, binary operations, mathematical functions, and string operations. The manual is intended for the first semester of the academic year 2024-25, with effective date noted as August 21, 2024.

Uploaded by

Boomika G
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Effective Date 21.08.2024 Page No.

Page 1 of 11

VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN


(Autonomous)

Course Material

Department Computer Science Programme M.Sc(CS)

Advanced Python Programming Lab


Course Title Course Code 24P1CSP02
Manual

Semester &
Class & Section I M.Sc(CS) ‘A’ I Semester, 2024-25
Academic Year

Handling Staff Dr.P.Sumitra Designation Assisant Professor

Staff Incharge HoD Principal


VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN
(Autonomous)

Effective Date 21.08.2024 Page No. Page 2 of 11

7. Numpy Libraries
a. Matrix Operation Add, Subtract and Multiply

# Importing the NumPy library


import numpy as np

# Displaying a message for addition operation


print("Add:")
# Performing addition
print(np.add(1.0, 4.0))

# Displaying a message for subtraction operation


print("Subtract:")
# Performing subtraction
print(np.subtract(1.0, 4.0))

# Displaying a message for multiplication operation


print("Multiply:")
# Performing multiplication
print(np.multiply(1.0, 4.0))

# Displaying a message for division operation


print("Divide:")
# Performing division
print(np.divide(1.0, 4.0))

Input & Output


Effective Date 21.08.2024 Page No. Page 3 of 11

VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN


(Autonomous)

b. Binary Operations
1. numpy.bitwise_and() :
# Python program explaining
# bitwise_and() function

import numpy as geek


in_num1 = 10
in_num2 = 11

print ("Input number1 : ", in_num1)


print ("Input number2 : ", in_num2)

out_num = geek.bitwise_and(in_num1, in_num2)


print ("bitwise_and of 10 and 11 : ", out_num)

Input & Output

2. numpy.bitwise_or()
# Python program explaining
# bitwise_or() function

import numpy as geek


in_num1 = 10
in_num2 = 11

print ("Input number1 : ", in_num1)


print ("Input number2 : ", in_num2)

out_num = geek.bitwise_or(in_num1, in_num2)


print ("bitwise_or of 10 and 11 : ", out_num)
VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN
(Autonomous)

Effective Date 21.08.2024 Page No. Page 4 of 11

Input & Output

3. numpy.bitwise_xor()
# Python program explaining
# bitwise_xor() function

import numpy as geek


in_num1 = 10
in_num2 = 11

print ("Input number1 : ", in_num1)


print ("Input number2 : ", in_num2)

out_num = geek.bitwise_xor(in_num1, in_num2)


print ("bitwise_xor of 10 and 11 : ", out_num)

Input & Output


Effective Date 21.08.2024 Page No. Page 5 of 11

VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN


(Autonomous)

c. Mathematical Functions

1. Trigonometric Functions
2. Arithmetic Functions
3. Rounding Functions
1. Trigonometric Functions

a) sine of an angle and the inverse sine


import numpy as np
# array of angles in radians
angles = np.array([0, 1, 2])
print("Angles:", angles)

# compute the sine of the angles


sine_values = np.sin(angles)
print("Sine values:", sine_values)

# compute the inverse sine of the angles


inverse_sine = np.arcsin(angles)
print("Inverse Sine values:", inverse_sine)

Input & Output


VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN
(Autonomous)

Effective Date 21.08.2024 Page No. Page 6 of 11

b) degrees( ) and radians( )


import numpy as np
# define an angle in radians
angle = 1.57079633
print("Initial angle in radian:", angle)

# convert the angle to degrees


angle_degree = np.degrees(angle)
print("Angle in degrees:", angle_degree)

# convert the angle back to radians


angle_radian = np.radians(angle_degree)
print("Angle in radians (after conversion):", angle_radian)

Input & Output

2. Arithmetic Functions
Effective Date 21.08.2024 Page No. Page 7 of 11

VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN


(Autonomous)

a) Add two matrices by using add ( ) function


import numpy as np

first_array = np.array([1, 3, 5, 7])


second_array = np.array([2, 4, 6, 8])

# using the add() function


result2 = np.add(first_array, second_array)
print("Using the add() function:",result2)

Input & Output

3. Rounding Functions

a) round( ) function
import numpy as np
numbers = np.array([1.23456, 2.34567, 3.45678, 4.56789])
# round the array to two decimal places
rounded_array = np.round(numbers, 2)
print(rounded_array)

Input & Output


VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN
(Autonomous)

Effective Date 21.08.2024 Page No. Page 8 of 11

ceil( ) function
import numpy as np

array1 = np.array([1.23456, 2.34567, 3.45678, 4.56789])

print("Array after floor():", np.floor(array1))

print("Array after ceil():", np.ceil(array1))

Input & Output

d. String Operations
1. numpy.lower()
# Python program explaining
# numpy.lower() function

import numpy as np

# converting to lowercase
print(np.char.lower(['GEEKS', 'FOR']))

# converting to lowercase
print(np.char.lower('GEEKS'))

Input & Output


Effective Date 21.08.2024 Page No. Page 9 of 11

VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN


(Autonomous)

2. numpy.split()
# Python program explaining
# numpy.split() function

import numpy as np

# splitting a string
print(np.char.split('geeks for geeks'))

# splitting a string
print(np.char.split('g e e k s, f o r, g e e k s', sep = ':'))

Input & Output

3. numpy.join()
# Python program explaining
# numpy.join() function

import numpy as np

# splitting a string
print(np.char.join('-', 'geeks'))

# splitting a string
print(np.char.join(['-', ':'], ['geeks', 'for']))

Input & Output


VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN
(Autonomous)

Page 10 of
Effective Date 21.08.2024 Page No.
11

4. numpy.count()
# Python program explaining
# numpy.count() function

import numpy as np

a=np.array(['geeks', 'for', 'geeks'])

# counting a substring
print(np.char.count(a,'geek'))

# counting a substring
print(np.char.count(a, 'fo'))

Input & Output

5. numpy.isnumeric() :
# Python program explaining
# numpy.isnumeric() function

import numpy as np

# counting a substring
print(np.char.isnumeric('geeks'))

# counting a substring
print(np.char.isnumeric('12geeks'))

Input & Output


Effective Date Page 11 of
21.08.2024 Page No.
11

VIVEKANANDHA COLLEGE OF ARTS AND SCIENCES FOR WOMEN


(Autonomous)

6. numpy.equal():
# Python program explaining
# numpy.equal() function

import numpy as np

# comparing a string elementwise


# using equal() method
a=np.char.equal('geeks','geeks')

print(a)

Input & Output

7. numpy.not_equal ( )
import numpy as np

# Comparing a string element-wise using not_equal() method


a = np.char.not_equal('geeks', 'for')

print(a)

Input & Output

You might also like