Lab Manaual Python Programming Lab
Lab Manaual Python Programming Lab
(LS-CSE-215G)
III SEMESTER
EXAM 25 Marks
TOTAL 50 Marks
DURATION OF 03 HOURS
EXAM
LIST OF PROGRAMS
Code-
def gcd_fun (x, y):
if (y == 0):
return x
else:
return gcd_fun (y, x % y)
x =int (input ("Enter the first number: "))
y =int (input ("Enter the second number: "))
num = gcd_fun(x, y)
print("GCD of two number is: ")
print(num)
Output
Experiment 2
Program- Write a program to find square root of a number (Newton’s
method).
Code-
def newtonsqrt(n):
approx=0.5*n
better=0.5*(approx+n/approx)
while better!=approx:
approx=better
better=0.5*(approx+n/approx)
return approx
n =int (input ("Enter the number: "))
print(newtonsqrt(n))
Output-
Experiment 3
Program- Write a program to find the exponentiation (Power of a
number).
Code-
base=int(input("Enter a number:"))
exponent=int(input("Enter a number:"))
r=base
for i in range(1,exponent):
r=base*r
print("Exponent",r)
Output-
Experiment 4
Program-Write a program to find the maximum of a list of numbers.
Code-
n=int(input("Enter number of element in list"))
mylist=[]
print("Enter elements of the list")
for _ in range(n):
a=int(input())
mylist.append(a)
maximum=max(mylist)
print("Maximum of the list is :",maximum)
Output-
Experiment 5
Program- Write a program to search an element in an array using
Linear search technique.
Code-
print(end="Enter the Size: ")
arrSize = int(input())
print("Enter " +str(arrSize)+ " Elements: ")
arr = []
for i in range(arrSize):
arr.append(input())
print("Enter an Element to Search: ")
elem = input()
chk = 0
for i in range(arrSize):
if elem==arr[i]:
index = i
chk = 1
break
if chk==1:
print("\nElement Found at Index Number: " + str(index))
else:
print("\nElement doesn't found!")
Output
Experiment 6
Program- Write a program to perform binary search.
Code-
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-
Experiment-7
Program- Write a program to perform selection sort.
Code-
nums = []
print("Enter the size of list: ", end="")
tot = int(input())
print("Enter", tot, "numbers for the list: ", end="")
for i in range(tot):
nums.append(int(input()))
for i in range(tot-1):
chk = 0
small = nums[i]
for j in range(i+1, tot):
if small > nums[j]:
small = nums[j]
chk = chk + 1
index = j
if chk != 0:
temp = nums[i]
nums[i] = small
nums[index] = temp
Output-
Experiment-8
Program- Write a program to sort the elements using insertion sort.
Code-
arr = []
print(end="Enter the Size: ")
arrSize = int(input())
print("Enter " +str(arrSize)+ " Elements: ")
for i in range(arrSize):
arr.append(int(input()))
print()
Output-
Experiment-9
Program- Write a program to Merge two lists.
Code-
one = []
two = []
three = one+two
threeSize = len(three)
Output-
Experiment-10
Program- Write a program to perform swapping of two numbers.
Code-
print("Enter the First Number: ", end="")
a = int(input())
print("Enter the Second Number: ", end="")
b = int(input())
print("\nBefore Swap")
print("a =", a)
print("b =", b)
x=a
a=b
b=x
print("\nAfter Swap")
print("a =", a)
print("b =", b)
Output-
Experiment-11
Program- Write a program to find that given number is odd or even.
Code-
print("Enter the Number: ")
num = int(input())
if num%2==0:
print("\nIt is an Even Number")
else:
print("\nIt is an Odd Number")
Output-
Experiment-12
Program- Write a program to display calendar of a month using year
value and month number entered by user.
Code-
import calendar
y = int(yy)
m = int(mm)
print("\n", calendar.month(y, m))
Output-
Experiment-13
Program- Write a program to multiply matrices.
Code-
print("Enter the Row and Column Size of First Matrix: ", end="")
rOne = int(input())
cOne = int(input())
print("Enter " +str(rOne * cOne)+ " Elements: ", end="")
mOne = []
for i in range(rOne):
mOne.append([])
for j in range(cOne):
num = int(input())
mOne[i].append(num)
chk = 0
countWord = 0
textLen = len(text)
for i in range(textLen):
if text[i]==' ':
if chk!=0:
countWord = countWord+1
chk = 0
else:
chk = chk+1
if chk!=0:
countWord = countWord+1
Output-
Course Outcomes
1. Write, test, and debug simple Python programs.
2. Implement Python programs with conditionals and loops.
3. Develop Python programs step-wise by defining functions and
calling them.
4. Use Python lists, tuples, dictionaries for representing compound
data.
5. Read and write data from/to files in Python