Functions PDF
Functions PDF
XII-A
ROLL NO:4
COMPUTER SCIENCE
ASSIGNMENT
(FUNCTIONS)
FUNCTIONS
1. CODING:
print("Jananisri.S.S")
print("To nd the list of indices of numbers divisible by 5 from a list
of given numbers")
def div5(l):
li=[]
for i in range(len(l)):
if l[i]%5==0:
li.append(i)
return li
l=[int(input("Enter the number:")) for i in range(int(input("Enter the
number of elements:")))]
rl=div5(l)
print("List of index:”,rl)
Output:
fi
2. CODING:
print("Jananisri.S.S")
print("To check whether the given string is palindrome or not")
def palindrome(s):
s1=s[::-1]
ag=False
if s1==s:
ag=True
return ag
s=input("Enter the string:")
if palindrome(s):
print("Palindrome")
else:
print("Not palindrome”)
Output:
fl
fl
fl
3.CODING:
print("Jananisri.S.S")
print("To count the number of words in a line:")
def countword(l):
c=len(l)
return c
s=input("Enter the line:")
count=countword(s.split())
print("Count of words:”,count)
Output:
4.CODING:
print("Jananisri.S.S")
print("To nd Factorial of a given number")
def factorial(n):
p=1
for i in range(1,n+1):
p=p*i
return p
n=int(input("Enter the number:"))
r=factorial(n)
print("Result:",r)
Output:
fi
5.CODING:
print("Jananisri.S.S")
print("To nd bonacci series till n terms")
def bonacci(n):
f1,f2=-1,1
l=[]
for i in range(n):
f3=f1+f2
l.append(f3)
f1,f2=f2,f3
return l
n=int(input("Enter the number of bonacci terms:"))
rl= bonacci(n)
for i in rl:
print(i,end=" “)
Output:
fi
fi
fi
fi
fi
6.CODING:
print("Jananisri.S.S")
print('''To nd whether the given number is prime number or not ,
if it not a prime number then return its factors''')
def factor(n):
l=[]
ag=True
for i in range(2,n):
if n%i==0:
l.append(i)
ag=False
return ag,l
n=int(input("Enter the number:"))
r=factor(n)
if r[0]:
print("Prime number")
else:
print("Not a prime number")
print(“Factors:”,r[1])
Output:
fl
fl
fl
fi
7.CODING:
print("Jananisri.S.S")
print("To nd HCF and LCM of given two numbers:")
def hc cm(n,m):
if n<m:
s=n
else :
s=m
for i in range(1,s+1):
if n%i==0 and m%i==0:
hcf=i
lcm=n*m/hcf
return hcf,lcm
n1=int(input("Enter the number:"))
n2=int(input("Enter another number:"))
t=hc cm(n1,n2)
print(“HCF:",t[0],"LCM:",t[1])
Output:
fl
fl
fi
8.CODING:
print("Jananisri.S.S")
print("Sum of the series:,1+1*2+1*2*3+.......1*...*n")
def sumfactorial(n):
s=0
p=1
for i in range(1,n+1):
p=p*i
s=s+p
return s
n=int(input("Enter the number of terms:"))
r=sumfactorial(n)
print(“Result:",r)
Output:
9.CODING:
print("Jananisri.S.S")
print("To arrange the letters of the word in alphabetical order")
def arrangeletters(w):
w=sorted(w.lower())
return "".join(w)
w=input("Enter the word:")
nw=arrangeletters(w)
print("Word in alphabetical order:”,nw)
Output:
10.CODING:
print("Jananisri.S.S")
print("To count the number of vowels")
def countvowel(s):
c=0
for i in s:
if i in "aeiouAEIOU":
c+=1
return c
s=input("Enter the string:")
count=countvowel(s)
print("Count of vowels:”,count)
Output:
11.CODING:
print("Jananisri.S.S XI-A")
print("To search the element in the given list:")
def search(l,n):
ag=False
for i in range(len(l)):
if n==l[i]:
ag=True
break
return ag
l=[]
for i in range(int(input("Enter the number of elements:"))):
l.append(int(input("Enter the number:")))
n=int(input("Enter the number to be searched:"))
r=search(l,n)
if r:
print("The element is found in",l.index(n)+1,"position")
else:
print("Element is not found”)
Output:
fl
fl
fl
12.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd the prime numbers from 20 numbers:")
def prime(mylist):
l=[]
for i in mylist:
for j in range(2,i):
if i%j==0:
break
else:
l.append(i)
return l
lst=[int(input("Enter the number:")) for i in range(int(input("Enter the
number of elements:")))]
lp=prime(lst)
if lp==[]:
print("No prime numbers")
else:
print("List of prime numbers:”,lp)
Output:
fi
13.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd frequency of each characteer in the given string:")
def frech(s):
s=s.lower()
d={}
for i in s:
d[i]=s.count(i)
return d
s=input("Enter a string:")
f=frech(s)
for i in f:
print("Frequency of”,i,”:",f[i])
Output:
fi
14.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("to perform addition,subtraction,multiplication,division")
def add(n,n1):
return n+n1
def sub(n,n1):
return n-n1
def mul(n,n1):
return n*n1
def div(n,n1):
return n/n1
print("Menu")
print("1. Addition")
print("2.Subtraction")
print("3.Multiplication")
print("4.Division")
print("5.Exit")
while True:
ch=int(input("Enter your choice(1/2/3/4/5):"))
if ch==1:
n=int(input("Enter the number:"))
n1=int(input("Enter another number:"))
print("Sum:",add(n,n1))
elif ch==2:
n=int(input("Enter the number:"))
n1=int(input("Enter another number:"))
print("Di erene:",sub(n,n1))
elif ch==3:
n=int(input("Enter the number:"))
n1=int(input("Enter another number:"))
print("Product:",mul(n,n1))
elif ch==4:
n=int(input("Enter the number:"))
n1=int(input("Enter another number:"))
print("Quotient:",div(n,n1))
elif ch==5:
break
ff
else:
print(“Invalid choice”)
Output:
15.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd the temperature in celsius or fahrenheit")
def ftemp(c):
f=9/5*c+32
return f
def ctemp(f):
c=5/9*(f-32)
return c
print("Menu")
print("1.fahrenheit to celsius")
print("2.Celsius to fahrenheit")
print("3.Exit")
fi
while True:
ch=int(input("Enter your choice(1/2):"))
if ch==1:
temp= oat(input("Enter the temperature:"))
print("Temperature in Celsius:",ctemp(temp))
elif ch==2:
temp= oat(input("Enter the temperature:"))
print("Temperature in Fahrenheit:",ftemp(temp))
elif ch==3:
break
else:
print("Invalid choice”)
Output:
16.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd the count of consonants")
fi
fl
fl
def countconsonant(s):
c=0
for i in s:
if i.isalpha() and i not in "aeiouAEIOU":
c+=1
return c
s=input("Enter the string:")
count=countconsonant(s)
print("Count of consonants:”,count)
Output:
17.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd area of di erent gures")
def area_triangle(b,h):
return 1/2*b*h
def area_square(a):
return a*a
fi
ff
fi
def area_rectangle(l,b):
return l*b
def area_parallelogram(b,h):
return b*h
def area_circle(r):
return 3.14*r*r
print("Menu:")
print("1.Area of triangle")
print("2.Area of square")
print("3.Area of rectangle")
print("4.Area of paralleloram")
print("5.Area of circle")
print("6.Exit")
while True:
ch=int(input("Enter your choice(1/2/3/4/5/6):"))
if ch==1:
b=int(input("Enter the base:"))
h=int(input("Enter the height:"))
print("Area:",area_triangle(b,h))
elif ch==2:
a=int(input("Enter the side:"))
print("Area:",area_square(a))
elif ch==3:
l=int(input("Enter the length:"))
b=int(input("Enter the breadth:"))
print("Area:",area_rectangle(l,b))
elif ch==4:
b=int(input("Enter the base:"))
h=int(input("Enter the height:"))
print("Area:",area_parallelogram(b,h))
elif ch==5:
r=int(input("Enter the radius:"))
print("Area:",area_circle(r))
elif ch==6:
break
else:
print("Invalid choice”)
Output:
18.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To sort the numbers")
def arrange(lst):
lst.sort()
print(lst)
l=[int(input("Enter the number:")) for i in range(int(input("Enter the
number of elements:")))]
arrange(l)
Output:
19.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To change the case of rst letter of each word")
def change rst(l):
nl=[]
for i in l:
if i[0].islower():
nl.append(i[0].upper()+i[1::])
else:
nl.append(i[0].lower()+i[1::])
return nl
l=[input("Enter the string:") for i in range(int(input("Enter the number
of strings:")))]
rl=change rst(l)
print("New list:”,rl)
fi
fi
fi
Output:
20.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd the equivalent for dollar and rupee")
def dr(d):
r=d*83.243338
print(r)
def rd(r):
d=r/83.243338
print(d)
print("Menu")
print("1.Dollar to rupee")
print("2.Rupee to dollar")
print("3.Exit")
while True:
ch=int(input("Enter your choice(1/2/3):"))
if ch==1:
amount= oat(input("Enter the amount:"))
print("Rupees:",end=" ")
dr(amount)
elif ch==2:
fi
fl
amount= oat(input("Enter the amount:"))
print("Dollars:",end=" ")
rd(amount)
elif ch==3:
break
else:
print("Invalid choice”)
Output:
21.CODING:
print("Jananissri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To print cube of the given number or print cube of 2")
fl
def cube(n=2):
print(n**3)
n=input("Enter the number:")
if n.isdigit():
print("Cube of",int(n),":",end=" ")
cube(int(n))
else:
print("Cube of",2,":",end=" ")
cube()
Output:
22.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd whether the two given strings are same or not")
def same(s1,s2):
ag=False
if s1==s2:
ag=True
return ag
fl
fl
fi
fl
s=input("Enter the string:")
s_=input("Enter another string:")
if same(s,s_):
print("They are equal")
else:
print("They are not equal”)
Output:
23.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd whether the two given strings are of same length or
not")
def samelen(s1,s2):
ag=False
if len(s1)==len(s2):
fl
fi
ag=True
return ag
s=input("Enter the string:")
s_=input("Enter another string:")
if samelen(s,s_):
print("They are equal in length")
else:
print("They are not equal in length”)
Output:
24.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd nth root of the given number")
def nthroot(x,n=2):
return pow(x,1/n)
fl
fi
fl
x=int(input("Enter the number:"))
n=input("Enter the root number:")
if n.isdigit():
ans=nthroot(x,int(n))
print("Result:",ans)
else:
ans=nthroot(x)
print(“Result:",ans)
Output:
25.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd random number of n digits")
import random as r
def randnum(n):
s=0
fi
for i in range(n):
s=s*10+r.randint(1,9)
return s
n=int(input("Enter number of digits:"))
rnum=randnum(n)
print("Random number with",n,"digit :”,rnum)
Output:
26.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("Chech the unit digit and return the number")
def lessunit(n1,n2):
rn1=n1%10
rn2=n2%10
if rn1>rn2:
return n2
elif rn1<rn2:
return n1
else:
return "Both have same unit value"
n1=int(input("Enter the number:"))
n2=int(input("Enter the number:"))
result=lessunit(n1,n2)
print(“Result:",result)
Output:
27.CODING:
print("Jananisr.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To return the 1st,last middle two terms of the generated
series")
def fun(a,l):
d=(l-a)/3
nl=[]
nl.append(a)
nl.append(a+d)
nl.append(a+2*d)
nl.append(l)
if nl[1]==round(nl[1]) and nl[2]==round(nl[2]):
return nl
else:
return "Enter valid terms"
a1=int(input("Enter the starting term:"))
a2=int(input("Enter the last term:"))
z=fun(a1,a2)
print(z)
Output:
28.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To add pre x as Mr or Mrs")
fi
def MrorMrs(name,gender):
if gender.isupper()=="F":
name="Mrs."+name
return name
else:
name="Mr."+name
return name
name=input("Enter your name:")
gender =input("Enter your gender(M/F):")
newname=MrorMrs(name,gender)
print("Name:",newname)
Output:
29.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd roots of a quadratic equation")
import math as m
def quad(a,b,c):
d=b**2-4*a*c
if a==0:
fi
print("Invalid")
print("It is not a quadratic equation")
else:
if d>0:
r1=-b+((m.sqrt(d))/2*a)
r2=-b-((m.sqrt(d))/2*a)
print(d)
print("The roots are real and distinct")
print("Roots are:",r1,r2)
elif d==0:
r1=-b+((m.sqrt(d))/2*a)
print("Roots are real and equal")
print(d)
print("Roots are:",r1,r1)
else:
print("No real roots")
print("ax**2+bx+c, Enter the values of a,b,c to nd their roots")
x=int(input("Enter the value of a:"))
y=int(input("Enter the value of b:"))
z=int(input("Enter the value of c:"))
quad(x,y,z)
Output:
fi
30.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To pick one student in random")
import random
def pickstudent(n):
randomstudent=d[random.randint(1,n)]
return randomstudent
n=int(input("Enter the number of students:"))
d={}
for i in range(1,n+1):
name=input("Enter student name:")
d[i]=name
stu=pickstudent(n)
print("Random student:”,stu)
Output:
31.CODING:
def si(p,n,r):
return (p*n*r)/100
def ci(p,n,r):
return (p*(1+(r/100))**n)-p
print("Menu")
print("1.SI")
print("2.CI")
print("3.Exit")
r=7.5
n=1
while True:
ch=int(input("Enter the choice:"))
if ch==1:
p=int(input("Enter the amount:"))
s=si(p,n,r)
print("Simple Interest:",s)
elif ch==2:
p=int(input("Enter the amount:"))
c=ci(p,n,r)
print("Compoumd interest:",c)
elif ch==3:
break
else:
print("Invalid choice”)
Output:
32.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To return numbers after swapping if the condition is
satis ed")
def swapifneed(n1,n2):
ag=False
if n1<n2:
ag=True
n1,n2=n2,n1
return ag,n1,n2
fl
fi
fl
fl
else:
return ag,n1,n2
n1=int(input("Enter the number:"))
n2=int(input("Enter another number:"))
t=swapifneed(n1,n2)
if t[0]:
print("The numbers are swapped")
print("The numbers are:",t[1],t[2])
else:
print("The numbers are not swapped")
print("The numbers are:”,t[1],t[2])
Output:
33.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("Quiz")
import random
def quiz():
fl
dq={1:'''Which famous Indian monument is often referred to as
the \" Symbol of Love\" and is a
UNESCO World Heritage Site?''',
2:'''In which Indian state will you nd the backwaters and
houseboats in a network of
canals, known as the Venice of the East?''',
3:'''What is the popular name for the annual camel fair held in
the town of Pushkar,
Rajasthan?''',
4:'''Which city is known as the\"Queen of the Arabian Sea\"
and is a popular tourist
destination in the southwestern state of Kerala?''',
5:'''The Ajanta and Ellora Caves, famous for their ancient rock-
cut architecture,
are located in which Indian state?''',
6:'''The hill station of Shimla, known for its colonial
architecture, is the capital
of which Indian state?''',
7:'''Which Indian wildlife sanctuary is renowned for its
population of the Bengal tiger
and is a UNESCO World Heritage Site?''',
8:'''What is the name of the traditional dance form originating
from the state of Odisha,
known for its vibrant costumes and intricate footwork?''',
9:'''The annual Rann Utsav, a cultural festival, takes place in
the white salt desert of
which Indian state?''',
10:'''The Dilwara Temples, known for their stunning marble
architecture and intricate
carvings,are located in the Mount Abu region of which Indian
state?'''}
dans={1:'a',2:"c",3:"a",4:"b",5:"a",6:"b",7:"a",8:"c",9:"c",10:"d"}
dop={1:"a) Taj mahal b)Qutub Minar c)hawa mahal d)red fort",
2:"a) Maharashtra b)Gujarat c)Kerala d)Rajasthan",
3:"a)Pushkar Fair b)Kumbh Mela c)Surajkund Crafts Mela
d)Goa Carnival" ,
4:"a) Chennai b)Kochi c)Thiruvananthapuram
d)Kanyakumari",
5:"a) Maharashtra b)Gujarat c)Kerala d)Rajasthan",
6:"a) Maharashtra b)Himachal pradesh c)Kerala d)Rajasthan",
fi
7:'''a)Jim Corbett National Park b)Kaziranga National Park
c)Ranthambhore National Park
d) Sundarbans National Park''',
8:"a)Bharatanatyam b)Kathak c)Odissi d)Kathakali",
9:"a) Maharashtra b)Himachal pradesh c)Gujarat
d)Rajasthan",
10:"a) Maharashtra b)Himachal pradesh c)Kerala
d)Rajasthan"}
dplayer={}
ltoplayer=[]
while len(dplayer)<5:
r=random.randint(1,10)
if r not in ltoplayer :
print(dq[r])
print(dop[r])
ans=input("Enter the correct option(a/b/c/d):")
dplayer[r]=ans
ltoplayer.append(r)
points=0
for i in dplayer:
if dplayer[i]==dans[i]:
points+=1
print("Points scored(out of 5):",points)
print("Percentage:",points/5*100)
print("Welcome to quiz")
quiz()
Output:
34.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("Reverse the given number")
def reverse(n):
s=0
while n>0:
r=n%10
s=s*10+r
n=n//10
return s
n=int(input("Enter the number:"))
n1=reverse(n)
print("Reversed number:”,n1)
Output:
35.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To print prime and composite number")
def primeandcomposite(n):
lp=[]
lc=[]
for i in range(2,n):
for j in range(2,i):
if i%j==0:
lc.append(i)
break
else:
lp.append(i)
return lp,lc
n=int(input("Enter the number:"))
t=primeandcomposite(n)
print("Prime numbers:",end=" ")
for i in t[0]:
print(i,end=" ")
print()
print("Composite numbers:",end=" ")
for i in t[1]:
print(i,end=" ")
print()
Output:
36.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To generate series")
def count(start,end,skip):
l=[]
for i in range(start,end,skip):
l.append(i)
return l
print("To generate the series:")
s=int(input("Enter the start value:"))
e=int(input("Enter the end value:"))
sk=int(input("Enter the skip count:"))
print("Generated series:",end=" ")
for i in count(s,e,sk):
print(i,end=" “)
Output:
37.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To nd numbers divisible by 7,3")
def count3and7(n):
l3=[]
l7=[]
for i in range(1,n):
if i%3==0 and i%7==0:
l3.append(i)
l7.append(i)
elif i%3==0:
l3.append(i)
elif i%7==0:
l7.append(i)
return l3,l7
n=int(input("Enter the number:"))
t=count3and7(n)
print("Numbers divisible by 3:",end=" ")
for i in t[0]:
print(i,end=" ")
print()
print("Numbers divisible by 7:",end=" ")
fi
for i in t[1]:
print(i,end=" ")
print()
Output:
38.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To print city name with more than 5 characters")
def cityname(n):
d={}
for i in range(n):
sno=int(input("Enter the serial number:"))
city=input("Enter the city name:")
d[sno]=city
print(d)
for i in d:
if len(d[i])>5:
print(d[i])
n=int(input("Enter the number of cities:"))
cityname(n)
Output:
39.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To shift the elements to left of the list")
def leftshift(lst,x):
l=lst[x+1:]+lst[:x+1]
return l
lst=[int(input("Enter the element:")) for i in range(int(input("Enter the
number of elements:")))]
x=int(input("Number of elements to be shifted:"))
L=leftshift(lst,x)
print("New list:”,L)
Output:
40.CODING:
print("Jananisri.S.S XI-A")
print("\u0B9C\u0BA9\u0BA9\u0BBF\u0BB8\u0BCD\u0BB0\u0BC0
")
print("To shift the zeroes")
def shiftzero(lst):
ln=[]
lz=[]
for i in lst:
if i==0:
lz.append(i)
else:
ln.append(i)
return ln+lz
lst=[int(input("Enter the number:")) for i in range(int(input("Enter the
number of elements:")))]
L=shiftzero(lst)
print("New list:”,L)
Output: