Professional Documents
Culture Documents
BHALOTIA COLLEGE
ASANSOL
ASSIGNMENT ON PYTHON
1|Page
INDEX
Topic Page No.
1) Whether the string is palindrome or not 3
2) Write a program in python to transpose a matrix 4
3) Develop a program in python to find the factorial of a number using
recursion 5
4) Develop a program in python to measure the size(resolution) of an image 6
5) Develop a program in python to reverse a given string 7
2|Page
1. Develop a program in python to find a string is palindrome or not.
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
3|Page
2.Write a program in python to transpose a matrix.
N=4
for i in range(N):
for j in range(i+1, N):
A[i][j], A[j][i] = A[j][i], A[i][j]
# driver code
A = [ [1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]]
transpose(A)
4|Page
3.Develop a program in python to find the factorial of a number using
recursion.
1. def recur_factorial(n):
2. if n == 1:
3. return n
4. else:
5. return n*recur_factorial(n-1)
6. # take input from the user
7. num = int(input("Enter a number: "))
8. # check is the number is negative
9. if num < 0:
10. print("Sorry, factorial does not exist for negative numbers")
11. elif num == 0:
12. print("The factorial of 0 is 1")
13. else:
14. print("The factorial of",num,"is",recur_factorial(num))
5|Page
4.Develop a program in python to measure the size(resolution) of an image.
import os
def getSize(filename):
st = os.stat(filename)
return st.st_size
def getDimension(filename):
# open image for reading in binary mode
with open(filename,'rb') as img_file:
# calculate height
height = (a[0] << 8) + a[1]
# calculate width
width = (a[0] << 8) + a[1]
filePath = "sample_image_file.jpg"
print("The size of file is " + str(getSize(filePath)) + " bytes")
getDimension(filePath)
6|Page
5.Develop a program in python to reverse a given string.
def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = "Geeksforgeeks"
print (s)
print (reverse(s))
7|Page