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

Python VISHAL

This document contains an assignment on Python programs. It includes 5 programs - to check if a string is a palindrome, transpose a matrix, find factorial using recursion, get the size and dimensions of an image, and reverse a string. It provides the question, Python code logic and sample output for each program in 1-2 pages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Python VISHAL

This document contains an assignment on Python programs. It includes 5 programs - to check if a string is a palindrome, transpose a matrix, find factorial using recursion, get the size and dimensions of an image, and reverse a string. It provides the question, Python code logic and sample output for each program in 1-2 pages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

BANWARILAL

BHALOTIA COLLEGE
ASANSOL
ASSIGNMENT ON PYTHON

Name : Vishal Kumar Shaw


Department : BCA(H)
SEMESTER : 6th
Roll No : 1020612500100041
Registration No. : 102171250041
Session : 2017-18

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

# Finds transpose of A[][] in-place


def transpose(A):

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)

print("Modified matrix is")


for i in range(N):
for j in range(N):
print(A[i][j], " ", end='')
print()

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:

# height of image (in 2 bytes) is at 164th position


img_file.seek(163)

# read the 2 bytes


a = img_file.read(2)

# calculate height
height = (a[0] << 8) + a[1]

# next 2 bytes is width


a = img_file.read(2)

# calculate width
width = (a[0] << 8) + a[1]

print("The dimension of the image is",width,"x",height)

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 ("The original string is : ",end="")

print (s)

print ("The reversed string(using loops) is : ",end="")

print (reverse(s))

7|Page

You might also like