SourceCode1to8_Print
SourceCode1to8_Print
# Sum of Series
import math
def fact(n):
import math
fact=math.factorial(n)
return fact
def power(x, n):
series_sum = 0
for k in range(n):
numerator = (-1)**k * x**(2*k+1)
denominator = fact(2*k+2)
term = numerator / denominator
series_sum += term
return series_sum
x = int(input("Enter the value of x:"))
n = int(input("Enter the number of terms:"))
result = power(x, n)
print("The sum of the series is: ",result)
print("\nSeries2")
print("*********")
def fact(n):
import math
fact=math.factorial(n)
return fact
def power(x, n):
series_sum = 0
for i in range(n):
numerator = (-1)**i * x**(2*i+1)
denominator = fact(2*i+3)
term = numerator / denominator
series_sum += term
return series_sum
x = int(input("Enter the value of x:"))
n = int(input("Enter the number of terms:"))
result = power(x, n)
print("The sum of the series is: ",result)
Output:
Source Code:
# Statistical Functions
import statistics as st
def stat(l):
mean=st.mean(l)
median=st.median(l)
mode=st.mode(l)
return mean, median, mode
def main_fn():
l=[]
n=int(input("Enter the number of elements in the list: "))
for i in range(n):
ele=int(input("Enter the element:"))
l.append(ele)
print("The list is:",l)
a,b,c=stat(l)
print("\nMean,Median,Mode")
print("Mean is:",a)
print("Median is:",b)
print("Mode is:",c)
main_fn()
Output:
Source Code:
def fibonacci(n):
from fibonacci import fibonacci as fib
return fib(length=n)
def fact(n):
from math import factorial as fact
return fact(n)
Output:
Source Code:
def revstr(n):
return n[::-1]
while True:
print("\nMENU DRIVEN CODE- LIST/STRING")
print("1. Binary Search in a list of integers")
print("2. Reversing a string")
print("3. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
list1=eval(input("Enter the list:"))
sorted_list = sorted(list1)
search = int(input("Enter the element to search:"))
print("Sorted List is:",sorted_list)
result = binary_search(sorted_list, search)
if result != -1:
print("Element found at Position",result+1)
else:
print("Element not found")
elif choice==2:
str1=input("Enter the String to reverse: ")
print("The reversed string is: ",revstr(str1))
elif choice==3:
print("Exit")
break
Output:
Source Code:
def prime(l):
l1=[]
for num in l:
for i in range(2, num):
if (num % i) == 0:
break
else:
l1.append(num)
if l1==[]:
print("List of prime numbers are none")
else:
print("Prime number list is:",l1)
def Max_SecMax(l):
l.sort()
print("Sorted list is:",l)
print("Largest number is: ",l[-1])
print("Second Largest number is: ",l[-2])
def generate(ele,low,high):
from random import randint as r
l=[]
for i in range(ele):
x=r(low,high)
l.append(x)
print("The list is: ",l)
Max_SecMax(l)
prime(l)
while True:
print("\nPRIME NUMBER CHECK")
print("1. Generate Random number between the limits")
print("2. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
ele=int(input("Enter the number of items in the list: "))
low=int(input("Enter the lower limit: "))
high=int(input("Enter the higher limit: "))
generate(ele,low,high)
elif choice==2:
print("Exit")
break
Output:
Source Code:
while True:
print("\nMENU DRIVEN CODE – TUPLES")
print("1. Display words with all vowels")
print("2. Check BMI")
print("3. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
t1=eval(input("Enter a tuple to check: "))
print("Tuple of words with vowels:",vow(t1))
elif choice==2:
t1=eval(input("Enter the nested tuple with name, height(m), weight: "))
for i in t1:
a,b=bmi(i[0],i[1],i[2])
print("The BMI of",i[0],":",a,"-",b)
elif choice==3:
print("Exit")
break
Output:
Source Code:
# Menu Driven Program - Dictionary
def add(d):
while True:
name=input("Enter the name:")
phno=int(input("Enter the Ph no:"))
d[name]=phno
choice=input("Do you wish to add more data:Y/N: ")
if choice.lower()!="y":
break
def update(d):
if len(d)==0:
print("Dictionary is empty")
else:
name=input("Enter the name to modify the Phone no: ")
print("Current Phone no. is: ",d[name])
phno=int(input("Enter the new Phone no: "))
d[name]=phno
print("Number Updated")
def delete(d):
name=input("Enter the name to be deleted: ")
if name not in d:
print("Name not in the list")
else:
del d[name]
print("Entry deleted")
while True:
print("\nMENU DRIVEN CODE – DICTIONARY")
print("1. Add")
print("2. View")
print("3. Modify")
print("4. Delete")
print("5. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
d={}
add(d)
elif choice==2:
print(d)
elif choice==3:
update(d)
elif choice==4:
delete(d)
elif choice==5:
print("Exit")
break
Output:
Source Code:
# Nested Dictionary
def house(name):
if name==0:
house="BHARATHI"
elif name==1:
house="TAGORE"
elif name==2:
house="SHIVAJI"
elif name==3:
house="PRATAP"
return house
def seniors(d):
l=[]
for i in d:
l.append(d[i]["Seniors"])
name=l.index(max(l))
print("Seniors:",house(name),max(l))
def juniors(d):
l=[]
for i in d:
l.append(d[i]["Juniors"])
name=l.index(max(l))
print("Juniors:",house(name),max(l))
def subjuniors(d):
l=[]
for i in d:
l.append(d[i]["SubJuniors"])
name=l.index(max(l))
print("SubJuniors:",house(name),max(l))
elif choice==3:
subjuniors(d)
elif choice==4:
print("Exit")
break
Output: