Friday 16 February 2024

52 - Write a program in Python to find the mean of numeric values stored in the given tuple.

T=eval(input("Enter a numeric tuple: "))

print("Tuple is: \n", T)

mean=sum(T)/len(T)

print("Mean of the numeric values of the tuple = ",mean)

Output:

Enter a numeric tuple: (2, 8, 9, 6, 5)

Tuple is:

(2, 8, 9, 6, 5)

Mean of the numeric values of the tuple =  6.0

Thursday 15 February 2024

51 - Write a program in Python to count the frequency of all elements of a tuple

T= (10, 20, 30, 20, 40, 30, 20, 50)

print("Tuple is: ",T)

Uni= ()

for i in T:

if i not in Uni:

Uni = Uni + (i,)

for i in Uni:

print(i,":",T.count(i))

Output:

Tuple is:  (10, 20, 30, 20, 40, 30, 20, 50)

10 : 1

20 : 3

30 : 2

40 : 1

50 : 1

50 - Write a program in Python to count the frequency of all elements of a list.

L= [10, 20, 30, 20, 40, 30, 20, 50]

print("List is: ",L)

Uni= []

Freq= []

for i in L:

if i in Uni:

index=Uni.index(i)

Freq[index]+= 1

else:

Uni.append(i)

Freq.append(1)

for i in range(len(Uni)):

print(Uni[i],":",Freq[i])

Output:

List is:  [10, 20, 30, 20, 40, 30, 20, 50]

10 : 1

20 : 3

30 : 2

40 : 1

50 : 1

49 - WAP in the Python to create a list and find the mean of all numeric values stored in the list.

L=eval(input("Enter a list: "))

n=len(L)

N=[]

for i in L:

if isinstance(i,int) or isinstance(i,float):

N.append(i)

print("Mean of numeric values stored in the list:")

print(sum(N)/len(N))

Output:

Enter a list: [5,'A',9,6.5,'Python',3.5]

Mean of numeric values stored in the list:

6.0

48 - WAP in Python to create a dictionary with name of employee, their salary and access them.

Emp={}

c='y'

while c in 'yY':

name=input("Enter the name of employee: ")

salary=float(input("Enter the salary of employee: "))

Emp[name]=salary

c=input("Do you want to add more emp(y/n): ")

print("Details of Employee:")

print(Emp)

Output:

Enter the name of employee: Ram

Enter the salary of employee: 38545

Do you want to add more emp(y/n): y

Enter the name of employee: Raj

Enter the salary of employee: 45854

Do you want to add more emp(y/n): y

Enter the name of employee: Aman

Enter the salary of employee: 47825

Do you want to add more emp(y/n): n

Details of Employee:

{'Ram': 38545.0, 'Raj': 45854.0, 'Aman': 47825.0}

47 - WAP in Python to input a string and display the frequency of each character using dictionary

S=input("Enter a string: ")

F={}

for i in S:

if i in F:

F[i]+=1

else:

F[i]=1

print("Frequency of characters in the given string: ")

for i, j in F.items():

print(i,":",j)

Output:

Enter a string: S P SHARMA

Frequency of characters in the given string:

S : 2

: 2

P : 1

H : 1

A : 2

R : 1

M : 1

Wednesday 14 February 2024

x + x2/2! + x3/3! + x4/4! + -------- xn /n!

import math as m

x=int(input("Enter the value of x: "))

n=int(input("Enter the value of n: "))

sum=0

for i in range(1,n+1):

sum=sum+pow(x,i)/m.factorial(i)

print("Sum of the series=",sum)

Output:

Enter the value of x: 2

Enter the value of n: 3

Sum of the series= 5.333333333333333

Enter the value of x: 2

Enter the value of n: 4

Sum of the series= 6.0

x + x2/2 + x3/3 + x4/4 + -------- xn /n

x=int(input("Enter the value of x: "))

n=int(input("Enter the value of n: "))

sum=0

for i in range(1,n+1):

sum=sum+pow(x,i)/i

print("Sum of the series=",sum)

Output:

Enter the value of x: 2

Enter the value of n: 3

Sum of the series= 6.666666666666666

Enter the value of x: 2

Enter the value of n: 4

Sum of the series= 10.666666666666666

1 - x + x2 - x3 +x4 + -------- xn

x=int(input("Enter the value of x: "))

n=int(input("Enter the value of n: "))

sum=0

for i in range(0,n+1):

if i%2==0:

sum=sum+pow(x,i)

else:

sum=sum-pow(x,i)

print("Sum of the series=",sum)

Output:

Enter the value of x: 2

Enter the value of n: 3

Sum of the series= -5

Enter the value of x: 2

Enter the value of n: 4

Sum of the series= 11

1 + x + x2 + x3 +x4 + -------- xn

x=int(input("Enter the value of x: "))

n=int(input("Enter the value of n: "))

sum=0

for i in range(0,n+1):

sum=sum+pow(x,i)

print("Sum of the series=",sum)

Output:

Enter the value of x: 2

Enter the value of n: 3

Sum of the series= 15

Enter the value of x: 2

Enter the value of n: 4

Sum of the series= 31