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

Python Codes 2024 2025

The document contains a series of Python programming tasks, each requiring the creation of functions to perform specific operations such as displaying characters at every nth occurrence, finding occurrences of a substring, managing a stack of multiples of 5, calculating averages, and manipulating strings and lists. Each task includes code snippets that demonstrate the implementation of the required functionality. The tasks cover a wide range of programming concepts including loops, conditionals, data structures, and string manipulation.

Uploaded by

bg98990021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python Codes 2024 2025

The document contains a series of Python programming tasks, each requiring the creation of functions to perform specific operations such as displaying characters at every nth occurrence, finding occurrences of a substring, managing a stack of multiples of 5, calculating averages, and manipulating strings and lists. Each task includes code snippets that demonstrate the implementation of the required functionality. The tasks cover a wide range of programming concepts including loops, conditionals, data structures, and string manipulation.

Uploaded by

bg98990021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

#1Write a function that takes an integer and string as an argument and display character at every

nth occurance in the string.


[]
s = input('Enter a string')
p= int(input('Enter the nth number'))
def disp (s, p) :
for i in range (n-1,len(s),n):
print(s[I])

#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])

rep_s(‘an apple keeps a doc away”)

op - $n $pple $ d$y keeps doctor $w$y

#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)

op- {'Brazil': 6, 'Venezuela': 9, 'Argentina': 9, 'Ecuador': 7, 'Bolivia': 7}


#7 waf named show_grades(st) that takes a dictionary st as an argument having A student
name as the key and a list of marks as it value the function should allocate grade based on the
following criteria.
A) Avg marks >= 90 “A”
B) <90 & >= 60 “B”
C) <60 “C”

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')

show_grades({'amit': [50,60,90,73], 'harsh': [90,80,99,95,]})

OP- B
A

#8 WAF THAT ACCEPTS A DICTIONARY AS AN ARGUMENT AND RETURNS SUM OF


MARKS OBTAINED BY ALL THE STUDENTS IN ENGLISH.

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

[797956, 67646, 319497]

None

[797956, 67646, 319497, 31654]

[797956, 67646, 319497, 31654]

None

None

#10 WAF THAT TAKES TWO STRINGS AS AN ARGUMENT AND RETURNS THE STRING WHOSE LENGTH IS LARGER.

def big_length(str1, str2):


s1 = len(str1)
s2 = len(str2)
if s1>s2:
print(s1 is larger)
else:
print(s2 is larger)

#11 waf in python that accepts a list of integers(non repeated) as an argument.


The function should display all the integers in a random
fashion[14,2,3,5,20,26]

rom random import randint


def r_lis(x):
m= x.copy()
for i in range(len(m)):
k= randint(0,len(m)-1)
print(m.pop())

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

def add_value(str L=[]):

return L.append(str)

a= add_value(str l=[])

print(a):

print(add value("avni gupta"))

print(add value("sualeha saifi”))

print(add value("divya singh”))

op- avni gupta

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

these int values

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

Ip= input("my favourate fruit is kiwi")

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

t=eval(input('enter the list'))

ob= input('enter the element')

p= index_values(t,ob)

print(p)
enter the list [12,23,45,67,89]

enter the element 12

-1

#18 waf to input a list. The program should find all the numbers and print their product

t= eval(input('enter the list of integers'))

p=1

for i in lt:

if type(i)== int or type(i)== float:

p=p*i

print(p)

op- enter the list of integers [29,65,98,38,37,24,3]

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

st= input('enter two word string')

m= st.split()

if len(m)==2:

print(cmps(st))

else:

print('string should have two word only')

op- ‘red happy'

'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

Def capitalize chrt(s):


X=s.split()

For x in s:

E=E+k.capitalize()

Print(e)

S+=input(“enter a string”)

Capitalize chrt(s+)

Op- enter a string – ‘sassy savage soya sauce

Sassy Savage Soya Sauce

#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):

final_str= ' '

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)

enter string 'samuel is not girl'

enter string 'gigi is not boy

#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')

ch= input('enter a character')

print(no_charactr(x,ch))

op-enter a string 'hello how are you you fine'

enter a character 'h'

You might also like