CBSE Class 12: Python Important Programs 2

Program:4.
Write a program to pass a string to a function. return and print reverse of it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def reverse(n):
n1=""
for i in n:
n1=i+n1
print("Reverse ",n1)
def main():
n=input("Enter any string ")
reverse(n)
"""
if __name__=='__main__':
main()
"""
#or
#function calling
main()
def reverse(n): n1="" for i in n: n1=i+n1 print("Reverse ",n1) def main(): n=input("Enter any string ") reverse(n) """ if __name__=='__main__': main() """ #or #function calling main()
def reverse(n):
	n1=""
	for i in n:
		n1=i+n1
	print("Reverse  ",n1)



def main():
	n=input("Enter any string ")
	reverse(n)

"""
if __name__=='__main__':
	main()
"""
#or
#function calling
main()

Output:

Enter any string hello
Reverse olleh

Enter any string computer
Reverse retupmoc

Program:5.
Write a Python program to pass a list of integers to the function. Count and return no. of elements which are multiples of 5.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def count(n):
c=0
for i in n:
if i%5==0:
print(i)
c=c+1
return(c)
def main():
n=[23,4,5,65,78,35,98,100,55]
n1=count(n)
print("Total multiples of 5 are ",n1)
"""
if __name__=='__main__':
main()
"""
#or
#function calling
main()
def count(n): c=0 for i in n: if i%5==0: print(i) c=c+1 return(c) def main(): n=[23,4,5,65,78,35,98,100,55] n1=count(n) print("Total multiples of 5 are ",n1) """ if __name__=='__main__': main() """ #or #function calling main()
def count(n):
	c=0
	for i in n:
		if i%5==0:
			print(i)
			c=c+1
	return(c)

def main():
	n=[23,4,5,65,78,35,98,100,55]
	n1=count(n)
	print("Total multiples of 5 are ",n1)

"""
if __name__=='__main__':
	main()
"""
#or
#function calling
main()

Program:6.
Write a Python script to pass a tuple to the function. Search and count the specified element.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def search_count(n,n1):
z=0
for i in n:
if i==n1:
z=z+1
if z==0:
print("Element ",n1," is not present")
else:
print("Element ",n1," is present ",z," times")
def main():
n=(23,4,5,65,78,65,98,65,55)
n1=int(input("Enter the element to search and count "))
search_count(n,n1)
"""
if __name__=='__main__':
main()
"""
#or
#function calling
main()
def search_count(n,n1): z=0 for i in n: if i==n1: z=z+1 if z==0: print("Element ",n1," is not present") else: print("Element ",n1," is present ",z," times") def main(): n=(23,4,5,65,78,65,98,65,55) n1=int(input("Enter the element to search and count ")) search_count(n,n1) """ if __name__=='__main__': main() """ #or #function calling main()
def search_count(n,n1):
	z=0
	for i in n:
		if i==n1:
			z=z+1
	if z==0:
		print("Element ",n1," is not present")
	else:
		print("Element ",n1," is present ",z," times")


def main():
	n=(23,4,5,65,78,65,98,65,55)
	n1=int(input("Enter the element to search and count "))
	search_count(n,n1)
	

"""
if __name__=='__main__':
	main()
"""
#or
#function calling
main()