Python Practical File for AKTU B Tech

Python Programming Lab Practical file according to the AKTU/UPTU B Tech (CSE-IV Sem) syllabus. Practical file with Source Code and Output for CS (Python) Engineering students. Below are 14 programs that has been given in lab syllabus by AKTU.

Syllabus : Python Language Programming Lab

Index of Practical file Topics

  • To write a python program that takes in command line arguments as input and print the number of arguments.
  • To write a python program to perform Matrix Multiplication.
  • To write a python program to compute the GCD of two numbers.
  • To write a python program to find the most frequent words in a text file.
  • To write a python program find the square root of a number (Newton’s method).
  • To write a python program exponentiation (power of a number).
  • To write a python program find the maximum of a list of numbers.
  • To write a python program linear search.
  • To write a python program Binary search.
  • To write a python program selection sort.
  • To write a python program Insertion sort.
  • To write a python program merge sort.
  • To write a python program first n prime numbers.
  • To write a python program simulate bouncing ball in Pygame.

Practical 1 : To write a python program that takes in command line arguments as input and print the number of arguments.

Program code :

#Importing sys module 

import sys

#Printing list (input values) we have given in command prompt

print(sys.argv)

#Accessing List elements 

print (len(sys.argv))

for i in sys.argv:

    print("Arguments", i)

Output :

csstudy.in

Practical 2 : To write a python program to perform Matrix Multiplication.

Program code :

X = [[1,2,3], 

    [2 ,5,6], 

    [7 ,8,9]]   

Y = [[9,8,7], 

    [6,5,4], 

    [3,2,1]]   

result = [[0,0,0], 

        [0,0,0], 

        [0,0,0]] 

#multiplication

for i in range(len(X)): 

    for j in range(len(Y[0])): 

        for k in range(len(Y)): 

            result[i][j] += X[i][k] * Y[k][j]  

for r in result: 

    print(r)

Output :
[30,24,18]
[66,53,40]
[138,114,90]

Practical 3: To write a python program to compute the GCD of two numbers.

Program code:

import math

a=int(input("Enter first number:"))

b=int(input("Enter second number:"))

print("GCD of ",a," and ",b,"is:",math.gcd(a,b))

Output:

Enter first number:60
Enter second number:48
GCD of 60 and 48 is: 12

Practical 4 : Python program to find the most frequent words in a text read from a file

Program code:

file__IO ="D:\\Komal\\n1\\goeduhub\\python.txt" 

with open(file__IO, 'r') as f:

    data = f.read()

    line = data.splitlines()

    words = data.split()

    spaces = data.split(" ")

    charc = (len(data) - len(spaces))

print('\n Line number:', len(line), '\n Words number:', len(words), '\n Spaces:', len(spaces), '\n Characters:', (len(data)-len(spaces)))

Output:

Line number: 4 
Words number: 29 
Spaces: 26  
Characters: 194

Practical 5 : Python Program to find the square root of a number by Newton’s Method

Program code:

def newton_method(number, number_iters = 100):

    a = float(number) 

    for i in range(number_iters): 

        number = 0.5 * (number + a / number) 

    return number

a=int(input("Enter first number:"))

b=int(input("Enter second number:"))

print("Square root of first number:",newton_method(a))

print("Square root of second number:",newton_method(b))

Output:
Enter first number:81
Enter second number:5
Square root of first number: 9.0
Square root of second number: 2.23606797749979

Practical 6 : Python program to find the exponentiation of a number

Program code:

num=int(input("Enter number: "))

exp=int(input("Enter exponential value: "))

result=1

for i in range(1,exp+1):

    result=result*num

print("Result is:",result)

Output:
Enter number: 2
Enter exponential value: 10
Result is: 1024

Practical 7 : Progarm to find maximum from list of numbers

Program code :

list1=[10,24,7,54,34,76,21]

print("Maximum number:",max(list1)) #using in-built method

maxn=0

for i in list1: #using loops

    if(i>maxn):

        maxn=i

print("Maximum number:",max(list1))

Output:

Maximum number: 76
Maximum number: 76

Practical 8 : Python Program to perform Linear Search

Program code:

def search(list,n):  

    for i in range(len(list)): 

        if list[i] == n: 

            return True

    return False 

list = [1, 2, 'goeduhub', 4,'python','machine learning',6]  

n = 'python'  # to be searched

if search(list, n): 

    print("Found at index ",list.index(n)) 

else: 

    print("Not Found")

Output :
Found at index 4

Practical 9 : Python Program to perform binary search

Program code:

def binarySearch(arr,beg,end,item):  

    if end >= beg:  

        mid = int((beg+end)/2)  

        if arr[mid] == item :  

            return mid+1  

        elif arr[mid] < item :   

            return binarySearch(arr,mid+1,end,item)  

        else:   

            return binarySearch(arr,beg,mid-1,item)  

    return -1  

a=input("Enter elements :")

b=a.split(",")

arr=[]

for i in b:

    arr.append(int(i))

arr.sort()

print("Sorted list:",arr)

item = int(input("Enter the item which you want to search:"))  

location = -1;   

location = binarySearch(arr,0,len(arr),item);  

if location != -1:   

    print("Item found at location: %d" %(location))  

else:   

    print("Item not found") 

Output:

Enter elements :4,26,87,12,0,67,69,34,32,48
Sorted list: [0, 4, 12, 26, 32, 34, 48, 67, 69, 87]
Enter the item which you want to search:34
Item found at location: 6

Practical 10 : Python Program to perform selection sort

Program code:

def selsort(n):

    for i in range(len(n)-1,0,-1):

        max=0

        for j in range(1,i+1):

            if n[j]>n[max]:

                max = j

            temp = n[i]

            n[i] = n[max]

            n[max] = temp

n = [78,25,11,29,75,69,45,67]

selsort(n)

print("Sorted array : ",n)

Output:
Sorted array : [11, 25, 29, 45, 67, 69, 75, 78]

Practical 11: Python Program to perform insertion sort

Program code:

a=[5,46,12,65,13,78,23,80]

b=[]

for i in range(1,len(a)):

    key = a[i]

    j = i-1  

    while j>0 and key<=a[j]:

        a[j+1]=a[j] 

        j = j-1  

    a[j+1] = key  

for i in a:  

    b.append(i) 

print("Elements in sorted order are:",b)

Output:

Elements in sorted order are: [5, 12, 13, 23, 46, 65, 78, 80]

Practical 12 : Python Program to perform merge sort

Program code:

def mergeSort(nlist):

    print("Splitting:",nlist)

    if len(nlist)>1:

        mid = len(nlist)//2

        lefthalf = nlist[:mid]

        righthalf = nlist[mid:]

        mergeSort(lefthalf)

        mergeSort(righthalf)

        i=j=k=0      

        while i < len(lefthalf) and j < len(righthalf):

            if lefthalf[i] < righthalf[j]:

                nlist[k]=lefthalf[i]

                i=i+1

            else:

                nlist[k]=righthalf[j]

                j=j+1

            k=k+1

        while i < len(lefthalf):

            nlist[k]=lefthalf[i]

            i=i+1

            k=k+1

        while j < len(righthalf):

            nlist[k]=righthalf[j]

            j=j+1

            k=k+1

    print("Merging:",nlist)

nlist = [23,47,83,13,56,69]

mergeSort(nlist)

print("Sorted list:",nlist)

Output :

Splitting: [23, 47, 83, 13, 56, 69] 
Splitting: [23, 47, 83] 
Splitting: [23] 
Merging: [23] 
Splitting: [47, 83] 
Splitting: [47] 
Merging: [47] 
Splitting: [83] 
Merging: [83] 
Merging: [47, 83] 
Merging: [23, 47, 83] 
Splitting: [13, 56, 69] 
Splitting: [13] 
Merging: [13] 
Splitting: [56, 69] 
Splitting: [56] 
Merging: [56] 
Splitting: [69] 
Merging: [69] 
Merging: [56, 69] 
Merging: [13, 56, 69] 
Merging: [13, 23, 47, 56, 69, 83] 
Sorted list: [13, 23, 47, 56, 69, 83]

Practical 13 : Python program to find first n prime numbers

Program code:

numr=int(input("Enter range:"))

print("Prime numbers:",end=' ')

for n in range(1,numr):

    for i in range(2,n):

        if(n%i==0):

            break

    else:

        print(n,end=' ')        

Output:

Enter range: 50
Prime numbers: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Practical file 14: Python program to bouncing ball in Pygame

Program code :

import sys, pygame

pygame.init()

size = width, height = 800, 400

speed = [1, 1]

background = 255, 255, 255

screen = pygame.display.set_mode(size)

pygame.display.set_caption("Bouncing ball")

ball = pygame.image.load("ball.png")

ballrect = ball.get_rect()

while 1:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            sys.exit()

    ballrect = ballrect.move(speed)

    if ballrect.left < 0 or ballrect.right > width:

        speed[0] = -speed[0]

    if ballrect.top < 0 or ballrect.bottom > height:

        speed[1] = -speed[1]

    screen.fill(background)

    screen.blit(ball, ballrect)

    pygame.display.flip()

Output:

Thanks for visiting post AKTU B.Tech Python Practical file. Below are project and practical files.

Python Projects for Class 12 with Source Code (Mysql Connectivity) – CS Study

Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study

Computer Science Practical file for class 12 Term-2 – CS Study

Python Programs for Class 11 and 12 – Online Study Test

Leave a Comment

Your email address will not be published.