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

Class 9 Python QA 12-16

A python program is written to check if a string or number is a palindrome by comparing the original value to its reverse. The program takes user input, calls a function to check for palindromic properties, and prints whether the input is or isn't a palindrome. A second program is written to calculate the factorial of a non-negative integer by using a recursive function that multiplies all integers from the given number down to 1. The program takes user input, calls the factorial function, and prints the result. The last two programs demonstrate accessing and changing list items using indexes. One prints specific indexed items from a list, and the other changes the item at index 1 to a new value and prints it.

Uploaded by

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

Class 9 Python QA 12-16

A python program is written to check if a string or number is a palindrome by comparing the original value to its reverse. The program takes user input, calls a function to check for palindromic properties, and prints whether the input is or isn't a palindrome. A second program is written to calculate the factorial of a non-negative integer by using a recursive function that multiplies all integers from the given number down to 1. The program takes user input, calls the factorial function, and prints the result. The last two programs demonstrate accessing and changing list items using indexes. One prints specific indexed items from a list, and the other changes the item at index 1 to a new value and prints it.

Uploaded by

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

12. Write a python program to check a string is palindrome or not.

#user-defined function
A string is said to be a palindrome if
def myfunc(x): the reverse of the string is the same
return x==x[::-1] as the string.

For example, “madam” is a


#main function palindrome, but “sir” is not a
palindrome.
x=input('enter a string: ')
response=myfunc(x) OUTPUT
if response: enter a string: madam
print(x,'is palindrome') madam is palindrome
enter a string: sir
else:
sir is not palindrome
print(x, 'is not palindrome')

13. Write a python program to check a number is palindrome or not.


#user-defined function
A number is said to be a palindrome if
def myfunc(x): the reverse of the number is the
return x==x[::-1] same as the given number.

For example, “121” is a palindrome,


#main function but “123” is not a palindrome.
x=input('Enter an integer number: ')
OUTPUT
response=myfunc(x)
Enter an integer
if response: number: 121
print(x,'is palindrome') 121 is palindrome
else: Enter an integer
print(x, 'is not palindrome') number: 123
123 is not palindrome
14. Write a python program to find out the factorial of an integer number.
# User-defined function
def factorial(n): Factorial of a non-negative integer is
if (n==1 or n==0): multiplication of all integers smaller
than or equal to n.
return 1
else: For example factorial of 6 is
return n * factorial(n - 1) 6*5*4*3*2*1 which is 720.

#main function
num = int(input('Enter an integer number to find out the factorial: '))
result=factorial(num)
Enter an integer number to find out
print("Factorial of",num,"is",result) factorial: 4
Factorial of 4 is 24
15. Write a python program to access the list items using index.

list1 = ["apple", "banana", "cherry"]


print(list1[0])
print(list1[1])
print(list1[2])

OUTPUT
apple
banana
cherry

16. Write a python program to change the 2nd item with ‘mango’ using index.

list1 = ["apple", "banana", "cherry"]


list1[1]='mango'
print(list1[1])

OUTPUT
mango

You might also like