Practical No.
1
Exercises-1
Q.1Write down the result you see.
>>> print("Hello","World!")
Hello World!
>>> print(3)
>>> print(3.0)
3.0
>>> print(2.0+3.0)
5.0
>>> print("2+3=",2+3)
2+3= 5
>>> print(2*3)
>>> print(2**3)
>>> print(7/3)
2.3333333333333335
>>> print(7//3)
2
>>> i=10
>>> print('the value of i is',i)
the value of i is 10
>>> print('the value of 3+4 is',3+4,'.')
the value of 3+4 is 7 .
Q.2 Evaluate the expression.
>>> 2+5*4
22
>>>(2+5)*4
28
>>> (2/5)+(4/5)-(7*5)/5
-5.8
>>> (35/7*5-7)+4**2//5
21.0
>>> (35%7*5-7)+4*2//5
-6
>>> (1+1)*(3+3)-(4*4)
-4
>>> 5+(4-2)*2+6%2-4+3-(5-3)/1
6.0
Q.3. Find the value of tan(pi/3) using math module in python.
>>>import math
>>> math.tan(math.pi/3)
1.7320508075688767
OR
>>>from math import *
>>>tan(pi/3)
1.7320508075688767
Q.4. Find the value of tanh(pi/3) using math module in python
>>>import math
math.tanh(math.pi/6)
0.48047277815645156
Q.5. Find the value of log (100)to the base2,10 and e using math module in
python
>>> math.log(100,10)
2.0
>>> math.log(100,2)
6.643856189774725
>>> math.log(100,math.e)
4.605170185988092
Q.6 Write a function that calculate square of a given number.
step 1.>>>def square(x):
s=x*x
print(s)
step 2.Run
step 3.>>> square(5)
25
Q.7.Write a python program sum of two numbers.
>>>n=int(input("Enter value for n="))
m=int(input("Enter value for m="))
p=n+m
print(p)
Output:
Enter value for n=5
Enter value for m=6
11
Q.8 Write a function that gives sin,cos,& log of given no.
step 1.>>>import math
def p(x):
a=math.sin(x)
b=math.cos(x)
c=math.log(x)
print("sin(x)=",a)
print("cos(x)=",b)
print("log(x)=",c)
step 2. Run
step 3.>>> p(1.2)
Output:
sin(x)= 0.9320390859672263
cos(x)= 0.3623577544766736
log(x)= 0.182321556793954
Q.9. Write a function that calculate sum & product of three no.
def g(x,y,z):
sum=x+y+z
prod=x*y*z
print('sum=',sum)
print('product=',prod)
>>> g(1,2,3)
sum= 6
product= 6
Q.10. Write a function that calculates quotient &remainder when ‘a’ divided by
‘b’.
>>>def division(a,b):
q=a//b
r=a%b
print("Quotient is:",q)
print('Remainder is:',r)
>>> division(7,2)
Quotient is: 3
Remainder is: 1
Q.11 Write a function that calculates area & circumference of circle if radius is
given.
.>>>import math
def circle(radius):
area=math.pi*radius**2
circumference=2*math.pi*radius
print("area is:",area)
print("circumference is:",circumference)
>>> circle(5)
area is: 78.53981633974483
circumference is: 31.41592653589793
Q.12. Write a function that calculates roots of the quadratic equation
ax2+bx+c=0
import cmath
def f(a,b,c):
r=-b/(2*a)
s=cmath.sqrt(b*b-4*a*c)/(2*a)
print("Roots=",r+s,r-s)
>>> f(1,1,4)
Roots= (-0.5+1.9364916731037085j) (-0.5-1.9364916731037085j)
Q.13. Use Python code to find hypotenuse of triangle whose sides are 12 and 5.
>>>import math
>>> math.hypot(12,5)
13.0
Practical No. 2
Exercise-2
Q.1 Write python code to concatenate two string “Good”&”Friday”
>>> x="Good"
>>> y="Friday"
>>> x+y
'GoodFriday'
Q.2. Write python code to replicate the string “amazing” 10 times.
>>> x="Amazing"
>>> y=x*10
>>> y
'AmazingAmazingAmazingAmazingAmazingAmazingAmazingAmazingAmazingA
mazing'
Q.3. Show the result for-s1="Very"
s2="nice !"
a]3*s1+2*s2
b]s1[1]
c]s1[1:3]
d]s1[2]+s2[:2]
e]s1+s2[-1]
f]s1.upper()
ANS-
>>> s1="Very"
>>> s2="nice !"
>>> 3*s1+2*s2
'VeryVeryVerynice !nice !'
>>> s1[1]
'e'
>>> s1[1:3]
'er'
>>> s1[2]+s2[:2]
'rni'
>>> s1+s2[-1]
'Very!'
>>> s1.upper()
'VERY'
Q.4. Write a python program to convert a string in upper case and lower
case.
>>> z="FyBSc COMPUTER science"
>>> print(z.lower())
fybsc computer science
>>> print(z.upper())
FYBSC COMPUTER SCIENCE
Q.5 Write a python program to add “ing” at the end of a given string.
a)Play b)End c)Teach
>>>x,y,z=”Play”,”End”,”Teach”
>>>a=”ing”
>>>x+a
“Playing”
>>>y+a
“Ending”
>>>z+a
“Teaching”
Q.6. Compare the given string -
a)
>>>a='mango'
>>>b='mango'
>>>a==b
True
>>>a>b
False
b)
>>>x='Mango'
>>>y='mango'
>>>x==y
False
c)
>>>a='Arts'
>>>b='Science'
>>>a==b
False
>>>a>b
False
>>>a<b
True
Q.7. Construct a string x=”Good Morning” and y=”to all”.Use operators +and *
find
1]2x+y
2]3*x
3]4y+2x
>>> x=”Good Morning”
>>> y=”to all”
>>>2*x+y
Good MorningGood Morningto all
>>>3*x
Good MorningGood MorningGood Morning
>>>4*y+2*x
to allto allto allto allGood MorningGood Morning
Q.8.Contruct a string in python and find its length.
>>>x=”My Name is Dimpal”
>>>len(x)
17
Q.9.Write a python program to get a string made of the first 2 and last 2
characters from a given string.
>>>m=”Introduction to Python Programming Language”
>>>m[0:2]+m[-2:]
Inge
Q.10.Write a Python program to change a given string to a new string where the
first and last chars have been exchanged.
>>>x="science"
>>>x[-1]+x[1:-1]+x[0]
Eciencs
Practical No. 3
Exercise 3
Q.1 Show the result
>>> l1=[1,2,3,4,5]
>>> l2=['a','b','c','d','e']
>>> l1[4]
>>> l2[:3]
['a', 'b', 'c']
>>> l1[2:]+l2[:3]
[3, 4, 5, 'a', 'b', 'c']
>>> l2*3
['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e']
>>> l1+l2
[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e']
Q.2 Show the result
>>> t1=('m','u','m','b','a','i')
>>> t2=('p','u','n','e')
>>> t1[3]
'b'
>>> t2[:4]
('p', 'u', 'n', 'e')
>>> t1[1:]
('u', 'm', 'b', 'a', 'i')
>>> t1[1:]+t2[:1]
('u', 'm', 'b', 'a', 'i', 'p')
>>> t1+t2
('m', 'u', 'm', 'b', 'a', 'i', 'p', 'u', 'n', 'e')
>>> t2*2
('p', 'u', 'n', 'e', 'p', 'u', 'n', 'e')
Q.3 Write a python program to count the no. of characters in a string.
>>> z="Good morning"
>>> len(z)
12
>>> z="Good morning"
>>> z.count('o')
Q.4 Write a python program to add “ing” at the end of a given list.
>>> l1=['hello','hi','2020','2019']
>>> l1.append('ing')
>>> print(l1)
['hello', 'hi', '2020', '2019', 'ing']
Q.5 Write a python program to find length of tuple.
>>> t1=('hello', 'hi', '2020', '2019', 'Orange')
>>> len(t1)
Q.6 Copy the element 44&55 from the following tuple into new tuple.
>>> t1=(11,22,33,44,55,66)
>>> t2=t1[3:5]
>>> print(t2)
(44, 55)
Q.7 Sort a tuple of tuples by ascending order
>>> t1=(44,11,66,22,33,55)
>>> tuple(sorted(list(t1)))
(11, 22, 33, 44, 55, 66)
Q.8 Write python program to reverse a tuple.
>>> t1=(44,11,66,22,33,55)
>>> t1[::-1]
(55, 33, 22, 66, 11, 44)
Q.9 Write a python program to remove last element from list.
>>> l1=['hello','hi','2020','2019']
>>> l1.pop()
'2019'
>>> print(l1)
['hello', 'hi', '2020']
Q.10 Write a python program to create a tuple with different data type
Ans-
>>> a=(1,2,3,4,5,["hello"])
>>> print(a)
(1, 2, 3, 4, 5, ['hello'])
Q.11 Swap following tuple
>>> x=(4,5)
>>> y=(6,7)
>>> x,y=y,x
>>> print(x)
(6, 7)
>>> print(y)
(4, 5)
Q.12 Write a python program to combine & repeat a list.
>>> p=[1,2,3,4]
>>> s=[6,7,8,9]
>>> print(p+s)
[1, 2, 3, 4, 6, 7, 8, 9]
>>> print(p+s*3)
[1, 2, 3, 4, 6, 7, 8, 9, 6, 7, 8, 9, 6, 7, 8, 9]
>>> print(s*4)
[6, 7, 8, 9, 6, 7, 8, 9, 6, 7, 8, 9, 6, 7, 8, 9]
>>> print(p*2)
[1, 2, 3, 4, 1, 2, 3, 4]
Q.13 Write a program to print valid list & obtain first item of the list & first 3
item of list
>>> l1=['hello','hi','2020','2019']
>>> print(l1[0])
hello
>>> print(l1[0:3])
['hello', 'hi', '2020']
Q.14 Give an example of the comparison between integer,float,string.
>>> a=2
>>> b=1.99
>>> c='hi'
>>> d='2'
>>> print(a>b)
True
>>> print(a==c)
False
>>> print(a>b) and (a!=c)
True
>>> print(a>b) or(a==b)
True
False
Q.15 Give an example of repetition,append &concatenation of list.
Concatenate two list by + operator
>>> l1=['hello','hi','2020','2019']
>>> l2=[1,2,3,4]
>>> l1+l2
['hello', 'hi', '2020', '2019', 1, 2, 3, 4]
Repetition
>>> l2=[1,2,3,4]
>>> print(l2*3)
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Append()- to add an item in list
>>> l1=['hello','hi','2020','2019']
>>> l1.append('Orange')
>>> print(l1)
['hello', 'hi', '2020', '2019', 'Orange']
Q.16 Write a program enters 9 digits & pick their elements.
L=eval(input('Enter a list: '))
print('The first element is',L[0])
print('The last element is',L[8])
print('The fifth element is',L[4])
Output:
Enter a list :1,2,3,4,5,6,7,8,9
The first element is 1
The last element is 9
The fifth element is 5
Practical No. 4
Exercise No. 4
Q.1.Write a program to add all its elements into a given set.
>>>sample_set = {"Yellow", "Orange", "Black"}
>>>sample_list = ["Blue", "Green", "Red"]
>>>sample_set.update(sample_list)
>>>print(sample_set)
{'Yellow', 'Black', 'Blue', 'Orange', 'Red', 'Green'}
Q.2.Return a new set of identical items from two sets
>>>set1 = {10, 20, 30, 40, 50}
>>>set2 = {30, 40, 50, 60, 70}
>>>print(set1.intersection(set2))
{40, 50, 30}
Q.3.Write a Python program to return a new set with unique items from both
sets by removing duplicates.
>>>set1 = {10, 20, 30, 40, 50}
>>>set2 = {30, 40, 50, 60, 70}
>>>print(set1.union(set2))
{70, 40, 10, 50, 20, 60, 30}
Q.4.Join multiple sets with the union() method:
>>>set1 = {"a", "b", "c"}
>>>set2 = {1, 2, 3}
>>>set3 = {"John", "Elena"}
>>>set4 = {"apple", "bananas", "cherry"}
>>>myset = set1.union(set2, set3, set4)
>>>print(myset)
{1, 2, 'b', 'a', 3, 'cherry', 'c', 'apple', 'bananas', 'John', 'Elena'}
OR
>>>set1 = {"a", "b", "c"}
>>>set2 = {1, 2, 3}
>>>set3 = {"John", "Elena"}
>>>set4 = {"apple", "bananas", "cherry"}
>>>myset = set1 | set2 | set3 |set4
>>>print(myset)
{1, 2, 'b', 'a', 3, 'cherry', 'c', 'apple', 'bananas', 'John', 'Elena'}
Q.5.Join sets that contains the values True, False, 1, and 0, and see what is
considered as duplicates:
>>>set1 = {"apple", 1, "banana", 0, "cherry"}
>>>set2 = {False, "google", 1, "apple", 2, True}
>>>set3 = set1.intersection(set2)
>>>print(set3)
{False, 1, 'apple'}
Practical No.5
Creating the dictionary:
e.g-
>>>dict1={}
>>>print(dict1)
>>>dict2={1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}
>>>print(dict2)
>>>dict3={"name": "Dimpal", 1: [20,30]}
>>>print(dict3)
{}
{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}
{'name': 'Dimpal', 1: [20, 30]}
Accessing Values in a Dictionary:
e.g-
>>>dict1={'name': 'Mayuri', 'city':'pune'}
>>>print(dict1)
>>>print(dict1['city'])
{'name': 'Mayuri', 'city':'pune'}
pune
>>>> dict2={1:"Red",2: "Blue",3: "Green"}
>>> dict2 [1]
'Red'
>>>>>> dict2.get(2)
'Blue'
Dictionary is mutable
e.g-
>>>d1=dict({1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'})
>>>d1[1]="Sunday"
>>>d1[4]="Monday"
>>>d1[5]="Monday"
>>>d1[1]="Saturday"
>>>d1
{1: 'Saturday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Monday', 5: 'Monday'}
Deleting the Elements from a dictionary:
e.g-
>>>squares={1:1,2:4,3:9,4:16,5:25}
>>> squares
{1:1, 2:4, 3:9, 4: 16, 5:25}
>>> print(squares.popitem())
(5, 25)
>>> squares
{1:1, 2:4, 3:9, 4: 16}
>>> squares.pop(2)
4
>>> squares
{1:1, 3:9, 4:16}
>>> del squares [3]
>>> squares
{1:1, 4:16}
>>> squares.clear()
>>> squares
{}
e.g-
>>>dict1={'kl':100, 'k2': [3,4,5], 'k3': {'name': 'Mayuri', 'city':'pune'}}
>>>print (dict1)
>>>print(dict1['k2'])
>>>print(dict1['k2'][1])
>>>print(dict1['k3']['city'])
{'k1': 100, 'k2': [3, 4, 5], 'k3': {'name': 'Mayuri', 'city': 'pune'}}
[3, 4, 5]
pune
Updating Dictionary
e.g-
>>>dict1={'name': 'Mayuri', 'age':34}
>>>print(dict1)
{'name': 'Mayuri', 'age':34}
>>>dict1['age']=45
>>>dict1
{'name': 'Mayuri', 'age':45}
>>>dict1['address']='pune'
>>>dict1
{'name': 'Mayuri', 'age':45,'address':'pune'}
Properties of Dictionary Keys:
e.g-
>>>dict={1:'Apple',2:'Cherry',3:'Orange',2:'Grapes'}
>>>dict
{1:'Apple',2:'Grapes',3:'Orange'}
Built in dictionary functions and Methods:
len( ): It returns the length (the number of items) in the dictionary.
type( ): Returns the type of passed variable.
copy ( ): Returns the copy of the dictionary.
get( ):Returns the value of specified key.
keys( ):Returns a list containing the dictionary’s keys.
items( ):Returns a list containing the a tuple for each key value pair.
values( ):Returns list of all values in dictionary.
clear( ): Removes all the elements from dictionary.
e.g-
>>>dict={1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}
>>>print(len(dict))
>>>print("Variable Type:",type(dict))
>>>print(dict.copy( ))
>>>print(dict.get(1))
>>>print(dict.keys( ))
>>>print(dict.values( ))
>>>print(dict.items( ))
>>>dict.clear( )
>>>print(dict)
Variable Type: <class 'dict'>
{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}
Monday
dict_keys([1, 2, 3])
dict_values(['Monday', 'Tuesday', 'Wednesday'])
dict_items([(1, 'Monday'), (2, 'Tuesday'), (3, 'Wednesday')])
{}
Practical No. 6
Range function:
>>> print(list(range(2,20,3)))
[2, 5, 8, 11, 14, 17]
>>> print(list(range(2,20,-3)))
[]
>>> print(list(range(20,2,-3)))
[20, 17, 14, 11, 8, 5]
>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(list(range(2,20)))
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> type([1,2])
<class 'list'>
>>> type((1,2))
<class 'tuple'>
>>> type({'a':1,'b':2})
<class 'dict'>
>>> type({'a','b'})
<class 'set'>
Q.1Write a function that gives number is positive if it is positive.
>>>def p(x):
if x>0:
print(x,'is positive')
>>> p(5)
5 is positive.
Q.2 Write a function that prints whether number is divisible by another
no.
>>>def d(x,y):
if x%y==0:
print(x,'is divisible by',y)
>>> d(15,5)
15 is divisible by 5
Q.3 Write a function that tests whether number is multiple of 5 or not.
>>>def m(x):
if x%5==0:
print(x,'is multiple of 5.')
>>> m(45)
45 is multiple of 5.
Q.4 Write a function that tests whether number is divisible by 5 and
greater than 100.
>>>def g(x):
if x>=100 and x%5==0:
print(x,'is divisible by 5 & greater than 100.')
>>> g(140)
140 is divisible by 5 & greater than 100.
Q.5 Write a function that tests whether number is even or odd.
>>>def g(x):
if x%2==0:
print(x,'is even.')
else:
print(x,'is odd.')
>>> g(5)
5 is odd.
Q.6 Write a function that gives given no. is positive or negative.
>>>def p(x):
if x>0:
print(x,'is positive.')
else:
print(x,'is negative.')
>>> p(-9)
-9 is negative
Q.7 Write a function that gives given no. is positive or negative or zero.
def w(x):
if x>0:
print(x,'is positive.')
elif x<0:
print(x,'is negative.')
else:
print('given number is zero.')
Output:
>>> w(0)
given number is zero.
>>> w(-9)
-9 is negative.
Q.8 Write a function that estimate the class for a given marks according
to table
Marks range Class
0-39 Fail
40-49 Second class
50-59 Higher Second class
60-75 First class
76-100 First class with distinction
def cl(x):
if x<=39:
print('fail')
elif x<=49:
print('second class')
elif x<=59:
print(' higher second class')
elif x<=75:
print('first class')
else:
print('first class with distinction')
Output:
>>> cl(79)
first class with distinction
Q.9 Write a function to find max of three no.
p=int(input('Enter first no.'))
a=int(input('Enter second no.'))
r=int(input('Enter third no.'))
if p>a and p>r:
print('Number1 is max')
elif a>p and a>r:
print('Number2 is max')
else:
print('Number3 is max')
Output:
Enter first no.5
Enter second no.8
Enter third no.3
Number2 is max
Q.10 Print ‘hi’ 4 times using for loop.
for i in range(4):
print('hi')
Output:
hi
hi
hi
hi
Q.11 Print square of nos. in list[0,4,5]
for i in [0,4,5]:
print(i*i)
output:
0
16
25
Q.12 print each letter 3 times the str ‘PUNE’.
for i in 'PUNE':
print(i*3)
Output:
PPP
UUU
NNN
EEE
Q.13 For each name list print message.
for i in ['P','U','N','E']:
print('Hello',i,'How are you? ')
Output:
Hello P How are you?
Hello U How are you?
Hello N How are you?
Hello E How are you
Q.14 Print 3 to 7 nos.
x=range(3,8)
for n in x:
print(n)
Output:
3
4
5
6
7
Q.15 Find the nos.of integer between 1 to 1000 which are multiple of 7.
>>> n=range(7,1000,7)
>>> len(n)
142
Q.16 Find the first 5 natural nos. using while loop.
i=1
while i<=5:
print(i)
i=i+1
Output:
1
2
3
4
5
Q.17 Find the sum of first 10 natural nos. using while loop.
sum=0
n=1
while n<=10:
sum=sum+n
n=n+1
print('sum=',sum)
Output:
sum= 55
end=’,’ in print gives outputs in comma separated sequence.
Q.18 Print Fibonacci numbers less than 1000 using while loop.
a,b=0,1
while a<1000:
print(a,end=',')
a,b=b,a+b
Output:
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
Practical 7 & 8.
Q.1 Create row & column vector.
(i)
>>> from sympy import*
>>> Matrix([[1,2,3]])
Matrix([[1, 2, 3]])
(ii)
>>> from sympy import*
>>> Matrix([[1],[2],[3]])
Matrix([
[1],
[2],
[3]])
Q.2 Find i) u+v ii) u-v iii)3u iv)2u+3v
>>> from sympy import*
>>> u=Matrix([[2],[5],[-3]])
>>> v=Matrix([[1],[0],[-2]])
>>> u+v
Matrix([
[ 3],
[ 5],
[-5]])
>>> u-v
Matrix([
[ 1],
[ 5],
[-1]])
>>> 3*u
Matrix([
[ 6],
[15],
[-9]])
>>> 2*u+3*v
Matrix([
[ 7],
[ 10],
[-12]])
Q.3 Find order of a matrix.
>>> from sympy import*
>>> A=Matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> A.shape
(3, 3)
Q.4 Construct the following matrices :
1. Identity matrix of order 3 x 4
2. Zero matrix of order 2 x 3
3. Ones matrix of order 2 x 3
4. Diagonal matrix
>>> from sympy import*
>>> eye(3)
Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
>>> zeros(2,3)
Matrix([
[0, 0, 0],
[0, 0, 0]])
>>> ones(2,3)
Matrix([
[1, 1, 1],
[1, 1, 1]])
>>> diag(1,2,3)
Matrix([
[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
Q.5 Find i) A+B ii) A-B iii) A*A iv) A*B v) A*V
vi) A**3 vii) B*A
>>> from sympy import*
>>> A=Matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> B=Matrix([[4,6,-3],[-4,1,6],[0,8,-9]])
>>> V=Matrix([[1],[5],[6]])
>>> A+B
Matrix([
[5, 8, 0],
[0, 6, 12],
[7, 16, 0]])
>>> A-B
Matrix([
[-3, -4, 6],
[ 8, 4, 0],
[ 7, 0, 18]])
>>> A*A
Matrix([
[ 30, 36, 42],
[ 66, 81, 96],
[102, 126, 150]])
>>> A*B
Matrix([
[-4, 32, -18],
[-4, 77, -36],
[-4, 122, -54]])
>>> A*V
Matrix([
[ 29],
[ 65],
[101]])
>>> A**3
Matrix([
[ 468, 576, 684],
[1062, 1305, 1548],
[1656, 2034, 2412]])
>>> B*A
Matrix([
[ 7, 14, 21],
[ 42, 45, 48],
[-31, -32, -33]])
Q.6 Print
i) 1st row of A & 3nd row of B
ii) 2nd column of A & 1st column of A
>>> from sympy import*
>>> A=Matrix([[2,1,1],[1,2,1],[1,1,2]])
>>> B=Matrix([[1,1,1],[0,1,1],[0,0,1]])
>>> A.row(0)
Matrix([[2, 1, 1]])
>>> B.row(2)
Matrix([[0, 0, 1]])
>>> A.col(1)
Matrix([
[1],
[2],
[1]])
>>> A
Matrix([[2, 1, 1], [1, 2, 1], [1, 1, 2]])
>>> Matrix([[2, 1, 1], [1, 2, 1], [1, 1, 2]])
Matrix([
[2, 1, 1],
[1, 2, 1],
[1, 1, 2]])
>>> A.col(-2)
Matrix([
[1],
[2],
[1]])
Q.7 For matrix
i) Delete 2st row of A
ii) Delete last column of A
>>> from sympy import*
>>> A=Matrix([[2,1,1],[1,2,1],[1,1,2]])
>>> A.row_del(1)
>>> A
Matrix([[2, 1, 1], [1, 1, 2]])
>>> A
Matrix([
[2, 1, 1],
[1, 1, 2]])
>>> A.col_del(-1)
>>> A
Matrix([
[2, 1],
[1, 1]])
Q.8 Insert row at 2nd place& column at 1st place
>>> from sympy import*
>>> A=Matrix([[2,1,1],[1,2,1],[1,1,2]])
>>> A=A.row_insert(1,Matrix([[0,5,4]]))
>>> A
Matrix([
[2, 1, 1],
[0, 5, 4],
[1, 2, 1],
[1, 1, 2]])
>>> A=A.col_insert(0,Matrix([[0],[5],[4],[6]]))
>>> A
Matrix([
[0, 2, 1, 1],
[5, 0, 5, 4],
[4, 1, 2, 1],
[6, 1, 1, 2]])
Practical No. 9 &10
Q. 1 Using SymPy find
1)Transpose of matrix
2)Determinant of matrix
3)Reduced row echelon form of matrix
4)Rank of matrix
(i)
>>> from sympy import*
>>> A=Matrix([[4,6,-3],[-4,1,6],[0,8,-9]])
>>> A
Matrix([
[ 4, 6, -3],
[-4, 1, 6],
[ 0, 8, -9]])
>>> A.T
Matrix([
[ 4, -4, 0],
[ 6, 1, 8],
[-3, 6, -9]])
>>> A.det()
-348
>>> A.rref()
(Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]), (0, 1, 2))
>>> A.rank()
3
(ii)
>>> from sympy import*
>>> B=Matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> B
Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> B.T
Matrix([
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
>>> B.det()
0
>>> B.rref()
(Matrix([
[1, 0, -1],
[0, 1, 2],
[0, 0, 0]]), (0, 1))
>>> B.rank()
2
Q.2 Using SymPy find inverse of matrix.
>>> from sympy import*
>>> A=Matrix([[2,1,1],[1,2,1],[1,1,2]])
>>> B=Matrix([[1,1,1],[0,1,1],[0,0,1]])
>>> A.inv()
Matrix([
[ 3/4, -1/4, -1/4],
[-1/4, 3/4, -1/4],
[-1/4, -1/4, 3/4]])
>>> B.inv()
Matrix([
[1, -1, 0],
[0, 1, -1],
[0, 0, 1]])
Q.3 Find minors and cofactors of a matrix.
>>>from sympy import*
>>>A= Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>>A.minor(1, 1)
-12
>>>from sympy import *
>>>A= Matrix([[1, 2], [3, 4]])
>>>A.cofactor(0, 1)
-3
Q.4 Find cofactor-matrix of A.
>>>from sympy import*
>>>A= Matrix([[1, 2], [3, 4]])
>>>A.cofactor_matrix()
Matrix([
[ 4, -3],
[-2, 1]])
Q.5 Find adjoint of a matrix.
>>>from sympy import*
>>>A=Matrix([[1, 2], [3, 4]])
>>>A.adjugate()
Matrix([
[ 4, -2],
[-3, 1]])
Q.6 Find inverse of a matrix by adjoint method.
>>>from sympy import*
>>>A=Matrix([[1, 2], [3, 4]])
>>>A.inv('ADJ')
Matrix([
[ -2, 1],
[3/2, -1/2]])
Practical No.11
Q.1 Using python find matrices L and U such that A=LU
where
i) A=Matrix([[1,2,4],[3,8,14],[2,6,13]])
>>>from sympy import*
>>>A=Matrix([[1,2,4],[3,8,14],[2,6,13]])
>>>L,U,_=A.LUdecomposition()
>>>L
Matrix([
[1, 0, 0],
[3, 1, 0],
[2, 1, 1]])
>>>U
Matrix([
[1, 2, 4],
[0, 2, 2],
[0, 0, 3]])
>>>L*U
Matrix([
[1, 2, 4],
[3, 8, 14],
[2, 6, 13]])
ii)B=Matrix([[1,2,3],[4,5,6],[7,8,9]])
>>>from sympy import*
>>>B=Matrix([[1,2,3],[4,5,6],[7,8,9]])
>>>L,U,_=B.LUdecomposition()
>>>L
Matrix([
[1, 0, 0],
[4, 1, 0],
[7, 2, 1]])
>>>U
Matrix([
[1, 2, 3],
[0, -3, -6],
[0, 0, 0]])
>>>L*U
Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Q.2 Write a python program to solve system of linear
equations by LU Decomposition Method.
(i)LM=Matrix([[6,18,3,3],[2,12,1,19],[4,15,1,0]])
>>> from sympy import*
>>> from sympy.abc import x,y,z
>>> LM=Matrix([[6,18,3,3],[2,12,1,19],[4,15,1,0]])
>>> solve_linear_system_LU(LM,[x,y,z])
{x: -14, y: 3, z: 11}
(ii)CD=Matrix([[1,2,2,7],[2,1,2,8],[2,2,1,6]])
>>> from sympy import*
>>> from sympy.abc import x,y,z
>>> CD=Matrix([[1,2,2,7],[2,1,2,8],[2,2,1,6]])
>>> solve_linear_system_LU(CD,[x,y,z])
{x: 7/5, y: 2/5, z: 12/5}
Practical No.12
Gauss Elimination Method
Q.1 Solve the following system of equations by Gauss elimination
method.
i)3x+2y-z=3
2x-3y+4z=6
2x-y+2z=9
ii) 5x+2y-2z=8
7x-3y=17
6y=12
(i)
>>> from sympy import*
>>> x,y,z=symbols("x,y,z")
>>> A=Matrix([[3,2,-1],[2,-2,4],[2,-1,2]])
>>> B=Matrix([[3],[6],[9]])
>>> linsolve((A,B),[x,y,z])
FiniteSet((6, -11, -7))
(ii)
>>> from sympy import*
>>> x,y,z=symbols("x,y,z")
>>> A=Matrix([[5,2,-2],[7,-3,0],[0,6,0]])
>>> P=Matrix([[8],[17],[12]])
>>> linsolve((A,P),[x,y,z])
FiniteSet((23/7, 2, 87/14))
Gauss Jordan Method
Q.2 Solve the following systems by Gauss Jordan method.
(i)2x+y=5; x+2y=7
(ii)x+2y+3z=3; 4x+5y+6z=6; 7x+8y+10z=9
(i)
>>> from sympy import*
>>> M=Matrix([[2,1],[1,2]])
>>> N=Matrix([[5],[7]])
>>> M.gauss_jordan_solve(N)
(Matrix([
[1],
[3]]), Matrix(0, 1, []))
(ii)
>>> from sympy import*
>>> S=Matrix([[1,2,3],[4,5,6],[7,8,10]])
>>> R=Matrix([[3],[6],[9]])
>>> sol,params=S.gauss_jordan_solve(R)
>>> sol
Matrix([
[-1],
[ 2],
[ 0]])
>>> params
Matrix(0, 1, [])
Q.3 Solve the following system by Gauss Jordan method.
>>>from sympy import Matrix
>>>A = Matrix([[1, 2, 1, 1], [1, 2, 2, -1], [2, 4, 0, 6]])
>>>B = Matrix([7, 12, 4])
>>>sol, params = A.gauss_jordan_solve(B)
>>>sol
Matrix([
[-2*_tau0 - 3*_tau1 + 2],
[ _tau0],
[ 2*_tau1 + 5],
[ _tau1]])
>>>Params
Matrix([
[_tau0],
[_tau1]]