Python Lab Manual
Python Lab Manual
I - MCA II - SEMESTER
DEPARTMENT OF MCA
INDEX
S. No Name of Programs
1 Write python a program that takes inputs and prints its sum,
Multiplication, subtraction, division, modulus..etc
2 Write a python program to find the square root of a number
by Newton’s Method.
3 Write a python program to biggest of three numbers?
8 Write a python program to print all the prime numbers below the given
number
9 Write a python program to count the number of characters in the string
using loop?
10 Write a python program to read a string from the user and print lowercase
character in uppercase and uppercase character in lowercase.
11 Write a python program to perform linear Search
13
Write a python program to perform Bubble Sort
Program: 2
Write a python program to find the square root of a number by Newton’s Method.
The square root of a number is defined as the value, which gives the number when
it is multiplied by itself. The radical symbol √ is used to indicate the square root. For
example, √16 = 4.
Newton’s Method: Let N be any number then the square root of N can be given by the
formula: root = 0.5 * (X + (N / X))
def newton_method(number, number_iters = 100):
a = float(number)
for i in range(number_iters):
return number
Program :3
Write a python program to biggest of three numbers?
Program: 4
Write a python program to find sum of digits of a given number
def getSum(n):
sum=0
sum+=int(digit)
return sum
n=12345
print(getSum(n))
OUTPUT:
15
Program: 5
Write a python program to find GCD of two numbers?
if(b == 0):
return a
else:
return hcf(b, a % b)
a = 60
b = 48
print(hcf(60, 48))
OUTPUT:
Program: 6
Write a python program to print the following pattern.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
n=int(input())
for i in range(1,n+1):
for j in range(1,i+1):
numbers=numbers + (str(i)+” “)
print(left-space + numbers)
OUTPUT:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Program: 7
Write a python program to find factorial of a given number
n=23
fact=1
for i in range(1,n+1):
fact=fact*i
print(fact)
OUTPUT:
Program: 8
Write a python program to print all the prime numbers below the given number
if n > 1:
for i in range(2,n):
if (n % i) == 0:
break
else:
print(n)
OUTPUT:
2 3 5 7 11 13 17 19
Program: 9
Write a python program to count the number of characters in the string
using loop?
total = 0
for i in str1:
total = total + 1
Program: 10
Write a python program to read a string from the user and print lowercase
character in uppercase and uppercase character in lowercase.
str1="Great Power"
newStr = ""
if str1[i].islower():
newStr += str1[i].upper()
elif str1[i].isupper():
newStr += str1[i].lower()
else:
newStr += str1[i]
Program: 11
Write a python program to perform linear Search
if (array[i] == x):
return i
return -1
array = [2, 4, 0, 1, 9]
x=1
n = len(array)
result = linearSearch(array, n, x)
if(result == -1):
else:
nums = []
print("Enter 10 Numbers (in ascending order):")
for i in range(10):
nums.insert(i, int(input()))
print("Enter a Number to Search:")
search = int(input())
first = 0
last = 9
middle = (first+last)/2
middle = int(middle)
while first <= last:
if nums[middle]<search:
first = middle+1
elif nums[middle]==search:
print("The Number Found at Position:")
print(middle+1)
break
else:
last = middle-1
middle = (first+last)/2
middle = int(middle)
if first>last:
print("The Number is not Found in the List")
OUTPUT:
Program: 13
Write a python program to perform Bubble Sort
def bubblesort(elements):
swapped = False
for i in range(n):
swapped = True
if not swapped:
return
print(elements)
bubblesort(elements)
print(elements)
OUTPUT:
[39,12,18,85,72,10,2,18]
[2,10,12,18,18,39,72,85]
Program: 14
Write a python program to perform Selection Sort
min_index = ind
min_index = j
size = len(arr)
selectionSort(arr, size)
print('The array after sorting in Ascending Order by using selection sort is:')
print(arr)
OUTPUT:
The array after sorting in Ascending Order by using selection sort is:
Statements
Eg:1
print('Hi,This is python')
a=int(input('Enter a value'))
b=int(input('Enter b value'))
print(a/b)
print(a+b)
print(a-b)
print("thank you")
OUTPUT:1
Hi,This is python
Enter a value25
Enter b value5
5.0
30
20
thank you
OUTPUT:2
Hi,This is python
Enter a value52
Enter b value0
Traceback (most recent call last):
print(a/b)
Eg:2
print('Hi,This is python')
a=int(input('Enter a value'))
b=int(input('Enter b value'))
try:
print(a/b)
print(a+b)
print(a-b)
except Exception as e:
print(e)
print("thank you")
OUTPUT:1
Hi,This is python
Enter a value25
Enter b value3
8.333333333333334
28
22
thank you
OUTPUT:2
Hi,This is python
Enter a value25
Enter b value0
division by zero
thank you
Eg:3
try:
import fabric
except TypeError:
except NameError:
except ZeroDivisionError:
except ModuleNotFoundError:
except Exception as e:
print(e)
finally:
OUTPUT:1
try:
print(5/0)
except TypeError:
except NameError:
except ZeroDivisionError:
except ModuleNotFoundError:
except Exception as e:
print(e)
finally:
OUTPUT:1