Python Codes 2024 2025
Python Codes 2024 2025
#2 Write a function in python that takes 2 strings as arguments and find occurrence of
smaller string in the larger string.
[]
def occur(s1, s2) :
l1 = len(s1)
l2 = len(s2)
if l1 >l2:
for i in range (l2) :
t = s2 [i : l2]
if t== s1:
print(s1)
f=1
if f == 0:
print('the does not exists')
#3Write 2 functions with push data and pop data. Push data function should take a list
of numbers as an argument and should push all the multiples of 5 in a stack MLT5. Pop
data function should remove elements from MLT5 one by one and display appropriate
message when stack is empty.
[]
MLT5 =[]
def pushdata (l):
for i in l:
if i% 5==0:
MLT5.append (l)
def popdata ():
if len (MLT5) ==0:
print('stack is empty')
else:
for i in range (len(MLT5)):
print(MLT5.pop())
else:
print ('Stack is empty')
#4 Write a function that takes a list of integers as an argument. The function should pick three integers randomly
from the list and display their average. It should return value 1 if the average is greater than average of all
integers in the list, if the average should return 0 and -1 if the average is lesser than overall average.
from random import randint
def avrg(l):
s= sum(l)
av= s/len(l)
for i in range(3):
s3= s3+ [randint(0, len(l-1))]
a3= s3/3
print(average)
if a3> av:
return 1
elif a3==av:
return 0
else:
return -1
A= avrg([12,34,56,76,86,17,8])
print(a)
#5 wap that takes a string as an argument and replace every occurrence of every a in the s=$,
T= @, Without using replace function
Def rep_s(st):
for i in range(0, len(st));
If st[i] == 'a':
Print(st+= '$')
else:
print(st+= str[i])
#6 waf that takes a list as an argument and the function should create and return a dictionary
having all the strings as key and the length is corresponding values to all the strings. Size>5 can
be added to the dictionary
def dictstr(s):
d={}
for i in s:
if len(i)>5:
d[i] = len(i)
return(d)
a = dictstr(["Brazil", "Venezuela", "Argentina", "Ecuador", "Bolivia", "Peru"])
print(a)
def show_grades(st):
for m in st:
sm= sum(st[m])
av= sm/ len(st[m])
if av>=90:
print('A')
elif av>=60:
print('B')
else:
print('C')
OP- B
A
def sumeng(s):
c=0
for i in s:
c=c+s[i][0]
return s
a = sumeng({ 'avni':[90,80,70], 'raj':[20,40,50]})
print(a)
op- 110
#9 waf that accepts a list of integers as an arguments. The function should store all the integers with
the digits more than 5 in a stack named big num
Big_num=[]
def numbr(d):
for i in d:
if i>9999:
print(Big_num.append(i))
else:
print(Big_num)
a = numbr([2654,797956,67646,7946,319497,716,31654,9413,9999,10000])
print(a)
op - None
None
[797956, 67646]
None
None
None
None
#10 WAF THAT TAKES TWO STRINGS AS AN ARGUMENT AND RETURNS THE STRING WHOSE LENGTH IS LARGER.
x=[14,2,3,5,20,26]
b= r_lis(x)
print(b)
26
20
5
3
2
14
#12 waf that should replace or swap first and last character of both the
strings
def facto(n):
if n==1:
return 1
else:
return n*facto(n-1)
print(facto(4))
a= facto(6)
print(a)
op-720
#13waf that accepts a string and list as an argument the list should have a
default value of blank list the function should add string to the list amd the
function should return the appendix list
return L.append(str)
a= add_value(str l=[])
print(a):
divya singh
sualeha saifi
#14 Q3 wap that accepts variable number of arguments the function should display all the arguments passed to it
Def s1(*a):
For I in a :
Print (I)
S1(5,10)
S1(“somya,bhavya,avni”)
Op
#15 waf that accepts variable number as arguments the function should find and return the avarage of
def s(*a):
d=0
c=0
for i in a:
d=d+i
c=c+1
return d/c
a=7
b= s(*a)
print(b)
#16 wap in python and count the no of vowels that takes a string as an argument
vowel =['a','e','i','o','u']
s=0
for i in ip:
if i in vowel:
s=s+1
op-o u a e u I I I I I
#17 wap with a function that takes a list and one element as an argument and returns the index value of the element in the
list. If the element is not present in the list
def index_values(l,e):
i=[]
for k in range(0,len(l)):
if l[k]==e:
i.append(k)
elif len(i)==0:
return -1
else:
return i
p= index_values(t,ob)
print(p)
enter the list [12,23,45,67,89]
-1
#18 waf to input a list. The program should find all the numbers and print their product
p=1
for i in lt:
p=p*i
print(p)
18700587360
#20waf that accepts a two word string as an argument and returns true if the starting letter of both the strings is same and
otherwise it should return false
def cmps(s):
x= s.split()
if x[0][0]== x[1][0]:
return true
else:
return false
m= st.split()
if len(m)==2:
print(cmps(st))
else:
'sad faced'
'sad faced'
#21 waf that accepts a string as an argument and capitalizes first character of all the words of the string
Import string
For x in s:
E=E+k.capitalize()
Print(e)
S+=input(“enter a string”)
Capitalize chrt(s+)
#22 waf that two strings as arguments and returns the string having all the words of length 4 of those
string
def st_ring(s1,s2):
x1= s1.split()
x2= s2.split()
for i in x1:
if len(i)==4:
final_str= final_str+k
for k in x2:
if len(k)==4:
final_str = final_str+k
return final_str
j= input('enter string')
a= input('enter string')
p= st_ring(j,a)
print(p)
#23 wap a function that returns no of times a character appear in the string without using a count function. The function
should take A string and a character as an argument
def no_charactr(st,ch):
a=0
for i in st:
if i==ch:
a=a+1
return a
x= input('enter a string')
print(no_charactr(x,ch))