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

Answers_-_Write_Functions[1]

The document contains a series of Python function definitions that perform various tasks, including mathematical operations, string manipulations, and data processing. Functions include calculating sums of even numbers, counting odd digits, swapping elements in lists, and checking for palindromes. Each function is designed to take specific inputs and return or print results based on the operations defined within.

Uploaded by

karungs2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Answers_-_Write_Functions[1]

The document contains a series of Python function definitions that perform various tasks, including mathematical operations, string manipulations, and data processing. Functions include calculating sums of even numbers, counting odd digits, swapping elements in lists, and checking for palindromes. Each function is designed to take specific inputs and return or print results based on the operations defined within.

Uploaded by

karungs2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

def even(n1,n2):
s=0
for i in range(n1+1,n2):
if i%2==0:
s+=i
return s
2. def proDigits(n):
p=1
while n>0:
d=n%10
n//=10
p*=d
return p
3. def countOddDigits(n):
c=0
while n>0:
d=n%10
n//=10
if d%2==1:
c+=1
print("Number of odd digits =",c)
4. def delete(A, B):
i=0
while i<len(A):
if A[i] in B:
del A[i]
else: i+=1
5. def swap(A):
n=len(A)
A=A[n//2:]+A[:n//2]
print(A)
6. def REVERSE(N):
left,right=0,len(N)-1
while left<right:
N[left],N[right]=N[right],N[left]
left+=1
right-=1
7. def Alternate(nums):
n=len(nums)
if n%2==1:
n-=1
for i in range(0,n,2):
nums[i],nums[i+1]=nums[i+1],nums[i]
print(nums)
8. def combine(A,B,C):
n=len(A)
for i in range(n):
x=2*A[i]+3*B[i]
C.append(x)
9. def calculate(A):
n=len(A)
for i in range(n):
if A[i]%5==0:
A[i]//=5
else: A[i]*=2
10. def combine(A,B,C):
n=len(A)
for i in range(n):
C.append(A[i])
C.append(B[i])
11. def replace(A):
n=len(A)
for i in range(n):
if A[i]%2==0:
A[i]//=2
else: A[i]*=2
12. def shortName(ft,md,last):
nm=ft[0]+'. '+md[0]+'. '+last
return nm
13. def palin(S):
if S.upper()==S[::-1].upper():
print('Palindrome')
else: print('Not Palindrome')
14. def countPalin(n):
c=0
for i in range(n):
S=input("Enter a string: ")
if S==S[::-1]:
c+=1
return c
15. def counters():
S=input("Enter a string: ")
d=l=u=s=0
for ch in S:
if ch.isdigit():
d+=1
elif ch.islower():
l+=1
elif ch.isupper():
u+=1
elif ch.isspace():
s+=1
print("Digits:",d)
print("Lowercase alphabets:",l)
print("Uppercase alphabets:",u)
print("Whitespace characters:",s)
16. def replace():
S=input("Enter a string: ")
while ' ' in S:
S=S.replace(' ',' ')
return S
17. def countVowelWords():
S=input("Enter a string: ")
words=S.split()
c=0
for word in words:
if word[0] not in 'AEIOU':
c+=1
return c
18. S='Apple in Pineapple'
def characters(S):
chars=[]
for ch in S:
if ch not in chars:
chars.append(ch)
return chars
19. def frequency(S):
freq={}
S=S.upper()
for ch in S:
if ch.isalpha():
if ch in freq:
freq[ch]+=1
else: freq[ch]=1
for k,v in freq.items():
print(k,v,sep=' - ')
20. def sortWords(S):
words=S.split()
words.sort()
print(words)
21. def vowelStar(S,ch):
ns=''
for ele in S:
if ele in 'aeiou':
ns+=ch
else: ns+=ele
return ns

You might also like