Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Lab Manaual Python Programming Lab

The document outlines the Python Programming Lab course (LS-CSE-215G) at DPGITM Engineering College, Gurugram, detailing its objectives, assessment structure, and a list of programming experiments. The lab aims to teach students to write, test, and debug Python programs, implement conditionals and loops, and work with data structures like lists and dictionaries. It includes 14 specific programming tasks, such as computing GCD, searching algorithms, sorting methods, and matrix multiplication.

Uploaded by

Meenakshi Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Manaual Python Programming Lab

The document outlines the Python Programming Lab course (LS-CSE-215G) at DPGITM Engineering College, Gurugram, detailing its objectives, assessment structure, and a list of programming experiments. The lab aims to teach students to write, test, and debug Python programs, implement conditionals and loops, and work with data structures like lists and dictionaries. It includes 14 specific programming tasks, such as computing GCD, searching algorithms, sorting methods, and matrix multiplication.

Uploaded by

Meenakshi Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PYTHON PROGRAMMING LAB

(LS-CSE-215G)
III SEMESTER

DPGITM ENGINEERING COLLEGE ,GURUGRAM


PYTHON PROGRAMMING LAB

Course code LS-CSE-215G

Category Laboratory Course

Course title Python Programming


Lab

Scheme and Credits L T P CREDITS


0 0 2 1

Objectives: 1. To write, test, and debug simple Python programs.


2. To implement Python programs with conditionals
and loops.
3. Use functions for structuring Python programs.
4. Represent compound data using Python lists,
tuples, and dictionaries.
5. Read and write data from/to files in Python.
Class work 25 Marks

EXAM 25 Marks

TOTAL 50 Marks

DURATION OF 03 HOURS
EXAM
LIST OF PROGRAMS

S. No. Program Title


1. Write a program to compute the GCD of two numbers.
2. Write a program to find square root of a number (Newton’s
method).
3. Write a program to find the exponentiation (Power of a
number).
4. Write a program to find the maximum of a list of numbers.
5. Write a program to search an element in an array using
Linear search technique.
6. Write a program to perform binary search.
7. Write a program to perform selection sort.
8. Write a program to sort the elements using insertion sort.
9. Write a program to Merge two lists.
10. Write a program to perform swapping of two numbers.
11. Write a program to find that given number is odd or even.
12. Write a program to display calendar of a month using year
value and month number entered by user.
13. Write a program to multiply matrices.
14. Write a program to count words in string using for loop.
Experiment 1
Program – Write a program to compute the GCD of two numbers.

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

print("\nSorted List is: ", end="")


for i in range(tot):
print(nums[i], end=" ")

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()))

for i in range(1, arrSize):


elem = arr[i]
if elem<arr[i-1]:
for j in range(i+1):
if elem<arr[j]:
index = j
for k in range(i, j, -1):
arr[k] = arr[k-1]
break
else:
continue
arr[index] = elem

print("\nThe New (Sorted) List is: ")


for i in range(arrSize):
print(end=str(arr[i]) + " ")

print()

Output-
Experiment-9
Program- Write a program to Merge two lists.
Code-
one = []
two = []

print(end="Enter the Size of First List: ")


oneSize = int(input())
print(end="Enter " +str(oneSize)+ " Elements: ")
for i in range(oneSize):
one.append(input())

print(end="\nEnter the Size of Second List: ")


twoSize = int(input())
print(end="Enter " +str(twoSize)+ " Elements: ")
for i in range(twoSize):
two.append(input())

three = one+two
threeSize = len(three)

print("\nThe Two Lists Merged Successfully!")


print("\nThe New List is:-")
for i in range(threeSize):
print(end=three[i] + " ")
print()

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

print("Enter Year: ")


yy = input()
print("\nEnter Month Number (1-12): ")
mm = input()

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)

print("\nEnter Row and Column Size of Second Matrix: ", end="")


rTwo = int(input())
if cOne != rTwo:
print("\nMultiplication Not Possible!")
exit()
else:
cTwo = int(input())
print("Enter " +str(rTwo * cTwo)+ " Elements: ", end="")
mTwo = []
for i in range(rTwo):
mTwo.append([])
for j in range(cTwo):
num = int(input())
mTwo[i].append(num)

# now multiplying two matrices


mThree = []
for i in range(rOne):
mThree.append([])
for j in range(cTwo):
sum = 0
for k in range(cOne):
sum = sum + (mOne[i][k] * mTwo[k][j])
mThree[i].append(sum)

# now printing the multiplication result


print("\nMultiplication Result of Two Given Matrix is:")
for i in range(rOne):
for j in range(cTwo):
print(mThree[i][j], end=" ")
print()
Output-
Experiment-14
Program- Write a program to count words in string using for loop.
Code-
print("Enter the String: ")
text = input()

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

print("\nNumber of Word(s): ")


print(countWord)

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

You might also like