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

Experiments Op

The document contains summaries of several Python programming experiments involving concepts like loops, functions, recursion, lists, tuples, dictionaries, strings, matrices, and exceptions. Specifically: 1) It shows experiments using while and for loops to calculate factorial, check if a number is prime, sum and average numbers, and generate Fibonacci series. 2) Experiments demonstrate built-in functions on tuples and sets, indexing and slicing lists, and sorting lists. 3) Other experiments implement dictionary indexing, iteration, and comprehension, as well as string methods. 4) Recursion is used to calculate factorial and generate Fibonacci series. 5) NumPy functions calculate mean, median, and percentile of arrays,

Uploaded by

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

Experiments Op

The document contains summaries of several Python programming experiments involving concepts like loops, functions, recursion, lists, tuples, dictionaries, strings, matrices, and exceptions. Specifically: 1) It shows experiments using while and for loops to calculate factorial, check if a number is prime, sum and average numbers, and generate Fibonacci series. 2) Experiments demonstrate built-in functions on tuples and sets, indexing and slicing lists, and sorting lists. 3) Other experiments implement dictionary indexing, iteration, and comprehension, as well as string methods. 4) Recursion is used to calculate factorial and generate Fibonacci series. 5) NumPy functions calculate mean, median, and percentile of arrays,

Uploaded by

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

EXPERIMENT 4.

D
# WRITE A PYTHON PROGRAM TO FIND FACTORIAL NUMBER (USING WHILE LOOP)

Code:
print("Enter the number:")
num=int(input())
fact=1
i=1
while i<=num:
fact=fact*i
i=i+1
print("\nFactorial=",fact)

Output:
Enter the number:
12

Factorial= 479001600
EXPERIMENT 4.F
# WRITE A PYTHON PROGRAM TO FIND THE NUMBER IS PRIME OR NOT (USING FOR LOOP)

Code:
n=int(input("Enter a number:"))
flag=0
for x in range(2,n):
if (n%x)==0:
flag=1
break
if(flag==0):
print("It is a prime number")
else:
print("It is not a prime number")

Output:

Enter a number:5
It is a prime number
EXPERIMENT 4.G
# WRITE A PYTHON PROGRAM SUM AND AVERAGE OF N NUMBERS(USING FOR LOOP)

Code:
n=int(input("Enter a number:"))
sum=0
#loop from 1 to n
for num in range(1,n+1,1):
sum=sum+num
print("sum of first:",n,"number is:",sum)
avg=sum/n
print("Average of:",n,"number is:",avg)

Output:
Enter a number:12
sum of first: 12 number is: 78
Average of: 12 number is: 6.5
EXERIMENT 4.H
# WRITE A PYTHON PROGRAM TO FIND FIBONACCI NUMBER(USING FOR LOOP)

Code:
n=int(input("Enter a number:"))
a=0
b=1
if n<=0:
print("The output of your input is:",a)
else:
print(a,b,end="")
for x in range(2,n):
c=a+b
print(c,end="")
a=b
b=c

Output:
Enter a number:14
0 1123581321345589144233
EXPERIMENT 4.I
# WRITE A PYTHON PROGRAM TO FIND FACTORIAL NUMBER(USING FOR LOOP)

Code:
n=int(input("Enter a number:"))
factorial=1
if n>=1:
for i in range(1,n+1):
factorial=factorial*i
print("Factorial of the given number is:",factorial)

Output:
Enter a number:14
Factorial of the given number is: 87178291200
EXPERIMENT 5.A
# WRITE A PYTHON PROGRAM TO PREFER ANY 5 BUILT-IN FUNCTIONS ON TUPLE

Code:
set1={1,2,5,7,8}
set2={1,2,3,4,5,6,7,8,9}
print("set=",set1)
print("Set=",set2)
union=set1.union(set2)
intersec on=set1 and set2
difference=set1-set2
symmetric_difference=set1^set2
print("The union of sets=",union)
print("The intersc on of sets=",intersec on)
print("The difference of sets=",difference)
print("The symmetric difference of sets=",symmetric_difference)

Output:
set= {1, 2, 5, 7, 8}
Set= {1, 2, 3, 4, 5, 6, 7, 8, 9}
The union of sets= {1, 2, 3, 4, 5, 6, 7, 8, 9}
The intersc on of sets= {1, 2, 3, 4, 5, 6, 7, 8, 9}
The difference of sets= set()
The symmetric difference of sets= {3, 4, 6, 9}
EXPERIMENT 5.B
# CREATE A TUPLE AND PERFORM ANY 5 BUILT-IN METHOD

Code:
my_tuple=(1,2,3,4,5)
print("my_tuple=",my_tuple)
print(len(my_tuple))
print(my_tuple.index(3))
print(my_tuple.count(4))
print(min(my_tuple))
print(max(my_tuple))

Output:
my_tuple= (1, 2, 3, 4, 5)
5
2
1
1
5
EXPERIMENT 6.A
# WRITE A PYTHON PROGRAM TO SORT LIST IN ASCENDING AND DESCENDING

Code:
list=[12,26,44,33,75,25,1]
print("original list=",list)

list.sort()
print("Ascending order of the list=",list)
list.sort(reverse=True)
print("Descending order of the list=",list)

Output:
original list= [12, 26, 44, 33, 75, 25, 1]
Ascending order of the list= [1, 12, 25, 26, 33, 44, 75]
Descending order of the list= [75, 44, 33, 26, 25, 12, 1]
EXPERIMENT 6.B
# WRITE A PYTHON PROGRAM TO IMPLEMENT INDEXING. SLICING AND COMPREHENSION
OF LIST

Code:
list=[10,20,30,40,50,60,77,80]
print("list=",list)
print("The posi ve indexing of list=")
print(list[3])
print("The nega ve indexing of list=")
print(list[-3])
print("The sclicing of list=")
print(list[1:3])
print(list[:4])
print(list[1:])
newlist=[number**2 for number in list]
print("list comprehension=",newlist)

Output:
list= [10, 20, 30, 40, 50, 60, 77, 80]
The posi ve indexing of list=
40
The nega ve indexing of list=
60
The sclicing of list=
[20, 30]
[10, 20, 30, 40]
[20, 30, 40, 50, 60, 77, 80]
list comprehension= [100, 400, 900, 1600, 2500, 3600, 5929, 6400]
EXPERIMENT 7.A
# WRITE A PYTHON PROGRAM TO FIND ALL ELEMENTS IN DICTIONARY

Code:
d={'A':100,'B':540,'C':239}
print("Total sum of values in the dic onary:")
print(sum(d.values()))

Output:
Total sum of values in the dic onary:
879
EXPERIMENT 7.B
# WRITE A PYTHYON PROGRAM TO PERFORM DICTIONARY INDEXING,ITERATING AND
COMPREHENSION

Code:
d={'key1':'value1','key2':'value2','key3':'value3'}
print(d['key1'])
for key,value in d.items():
print(key,value)
new_dict={key: value.upper() for key, value in d.items()}
print(new_dict)

Output:
value1
key1 value1
key2 value2
key3 value3
{'key1': 'VALUE1', 'key2': 'VALUE2', 'key3': 'VALUE3'}
EXPERIMENT 8.A
# WRITE A PYTHON PROGRAM TO SEARCH AN ELEMENT IN AN ARRAY

Code:
pets=["cats","dog","rabbit","tortoise"]
print("Array=",pets)
x=pets[1]
print("The 2nd elements of array is:",x)

Output:
Array= ['cats', 'dog', 'rabbit', 'tortoise']
The 2nd elements of array is: dog
EXPERIMENT 8.B
# WRITE A PYTHON PROGRAM TO IMPLEMENT 5 BUILT-IN STRING METHODS

Code:
a="hello world"
print(len(a))
print(a.upper())
print(a.lower())
print(a.strip())
print(a. tle())

Output:
11
HELLO WORLD
hello world
hello world
Hello World
EXPERIMENT 9.A
# WRITE A PYTHON PROGRAM TO FIND FACTORIAL OF GIVEN N NUMBER

Code:
def factorial(n):
print(fact)
num=int(input("enter num="))
fact=1
while(num>0):
fact=fact*num
num=num-1
factorial(num)

Output:
enter num=24
620448401733239439360000
EXPERIMENT 9.B
# WRITE A PYTHON PROGRAM TO FIND FACTORIAL USING RECURSION

Code:
def recur_factorial(n):
if n==1:
return n
else:
return n*recur_factorial(n-1)

num=int(input("enter num:"))
#check if the number is negav ve(-)
if num<0:
print("Sorry, factorial does not exist for nega ve numbes")
elif num==0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))

Output:
enter num:10
The factorial of 10 is 3628800
EXPERIMENT 9.C
# WRITE A PYTHON PROGRAM USING FUNCTION OR RECURSION TO GENERATE FIBONACCI
SERIES

Code:
def recur_fibo(n):

if n<=1:

return n

else:

return(recur_fibo(n-1) + recur_fibo(n-2))

nterms=10

# check if the number of the terms is valid

if nterms<=0:

print("Plese enter a posi ve integer")

else:

print("Fibonacci sequence:")

for i in range(nterms):

print(recur_fibo(i))

Output:
Fibonacci sequence:

13

21

34
EXPERIMENT 11.A
# WRITE A PYTHON PROGRAM TO IMPLEMENT NUMPY STATICAL FUNCTION (Mean,
Median AND Percen le)

Code:
import numpy as np
a=np.array([[1,2,5],[70,60,52],[12,45,10]])
print("Our Array is")
print(a)

print("Applying mean() func on")


print(np.mean(a))

print("Applying median() func on")


print(np.median(a))

print("Applying percen le() func on")


print(np.percen le(a,40))

Output:
Our Array is
[[ 1 2 5]
[70 60 52]
[12 45 10]]
Applying mean() func on
28.555555555555557
Applying median() func on
12.0
Applying percen le() func on
10.4
EXPERIMENT 11.B
# WRITE A PYTHON PROGRAM TO ADD,SUBTRACT AND MULTIPLE TWO MATRICES

Code:
import numpy as np
a=np.array([[1,2],[3,4]])
b=np.array([[4,5],[6,7]])
print("prin ng element of first matrix")
print(a)
print("Prin ng element of second matrix")
print(b)

print("Addi on of two matrix")


print(np.add(a,b))

print("Subtrac on of two matrix")


print(np.subtract(a,b))

print("Mul plica on of two matrix")


res=np.dot(a,b)

print(res)
Output:
prin ng element of first matrix
[[1 2]
[3 4]]
Prin ng element of second matrix
[[4 5]
[6 7]]
Addi on of two matrix
[[ 5 7]
[ 9 11]]
Subtrac on of two matrix
[[-3 -3]
[-3 -3]]
Mul plica on of two matrix
[[16 19]
[36 43]]
EXPERIMENT 11.C
# WRITE A PYTHON PROGRAM TO TRANSPOSE OF MATRIX

Code:
x=[[14,13],
[6,9],
[3,4]]
result=[[0,0,0],
[0,0,0]]
#iterate through rows
for i in range(len(x)):
#iterate through columns
for j in range(len(x[0])):
result[j][i]=x[i][j]
for r in result:
print(r)

Output:
[14, 6, 3]
[13, 9, 4]
EXPERIMENT 13.A
# WRITE A PYTHGON PROGRAM TO IMPLEMENT EXCEPTIONAL HANDLING

Code:
input1=int(input("value of a="))
input2=int(input("value of b="))
try:
div=intput1/intput2
print(div)
except:
print("There is an error in assigned value")

Output:
value of a=16
value of b=0
There is an error in assigned value

You might also like