0% found this document useful (0 votes)
6 views24 pages

I BSC AIML Python Lab Programs

The document contains a series of Python lab programs demonstrating various programming concepts, including displaying information, finding the greatest of three numbers, summing user-input numbers, matrix multiplication, calculating GCD, factorial, Fibonacci series, checking for prime numbers, generating random numbers, sorting, a simple calculator, and searching algorithms. Each program includes code snippets along with example outputs. The programs serve as practical exercises for learning Python programming.
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)
6 views24 pages

I BSC AIML Python Lab Programs

The document contains a series of Python lab programs demonstrating various programming concepts, including displaying information, finding the greatest of three numbers, summing user-input numbers, matrix multiplication, calculating GCD, factorial, Fibonacci series, checking for prime numbers, generating random numbers, sorting, a simple calculator, and searching algorithms. Each program includes code snippets along with example outputs. The programs serve as practical exercises for learning Python programming.
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

Python lab programs

Program 1

#program to display information

name = "Winsha"
address = "Thiruvarur, Tamil nadu"
mobile = "1234567890"
college = "Global College"
course = "BSc AIML"
subjects = "Python, RDBMS"

print("Name: ", name)


print("Address: ", address)
print("Mobile: ", mobile)
print("College: ", college)
print("Course: ", course)
print("Subject: ", subjects)
Program 1 Output:
Name: Winsha
Address: Thiruvarur, Tamil nadu
Mobile: 1234567890
College: Global College
Course: BSc AIML
Subject: Python, RDBMS
Program 2
#greatest of three numbers

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


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

if a>b and a>c:


print("Largest number:", a)
elif b>c:
print("Largest number:", b)
else:
print("Largest number:", c)
Program 2 output:

Enter first number:1


Enter second number:3
Enter third number:2
Largest number: 3
Program 3
numbers = []
total = 0
while True:
n = int(input("Enter a positive number(-ve to stop): "))
if n<0:
break
[Link](n)
total += n

print("Numbers entered: ", numbers)


print("sum of numbers: ", total)
Program 3 output
Enter a positive number(-ve to stop): 2
Enter a positive number(-ve to stop): 3
Enter a positive number(-ve to stop): 4
Enter a positive number(-ve to stop): -4
Numbers entered: [2, 3, 4]
sum of numbers: 9
Program 4
m = int(input("Enter the number of rows of matrix A(m):"))
p = int(input("Enter the number of coloumns of matrix A(p):"))
r = int(input("Enter the number of coloumns of matrix B(r):"))
print("Enter the elements of matrx A:")
A = []
for i in range(m):
row = []
for j in range(p):
[Link](int(input()))
[Link](row)
print("Enter the elements of matrix B: ")
B = []
for i in range(p):
row = []
for i in range(r):
[Link](int(input()))
[Link](row)
c = [[0 for j in range(r)] for i in range (m)]
#c= [i][j]
for i in range(m):
for j in range(r):
for k in range(p):
c[i][j] += A[i][k]* B[k][j]
print("Product of matrices A and B:")
for row in c:
print(row)
Program 4 output
Enter the number of rows of matrix A(m):2
Enter the number of coloumns of matrix A(p):2
Enter the number of coloumns of matrix B(r):2
Enter the elements of matrx A:
1
2
3
4
Enter the elements of matrix B:
5
6
7
8
Product of matrices A and B:
[19, 22]
[43, 50]
Program 5
#GCD recursive function

def rec(a, b):


if b == 0:
return a

return rec(b, a%b)

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


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

print("GCD is:", rec(n1, n2))


Program 5 output
Enter first number:6
Enter second number:9
GCD is: 3
Program 6
#factorial

def fac(n):
if n ==0 or n== 1:
return 1
return n*fac(n-1)

n = int(input("Enter a number:"))
print("factorial of", n, "is", fac(n))
Program 6 output
Enter a number:4
factorial of 4 is 24
Program 7
#fibonacci series

def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)

n=6
for i in range(n):
print("fibonacci",fib(i))
Program 7 output
fibonacci 0
fibonacci 1
fibonacci 1
fibonacci 2
fibonacci 3
fibonacci 5
Program 8
# check whether prime

import math
def isprime(n):
for i in range(2, int([Link](n)) + 1):
if n %i == 0:
return False
return True

n = int(input("Enter a number:"))
for x in range(2, n+1):
if isprime(x):
print(x, end=" ")
Program 8 output
Enter a number:10
2357
Program 9
#random numbers to file

import random
n=5
file = open("[Link]", "w")
print("random number")
for i in range(n):
r = [Link](1,100)
[Link](str(r)+ "\n")
print(r)
[Link]()
Program 9 output
random number
44
92
52
86
84
Program 10
#sort list, string, tuple

s = "python"
lst = [4,1,3,2]
tup = (9,5,7,1)
print("sorted string", sorted(s))
print("sorted list", sorted(lst))
print("sorted tup", sorted(tup))
Program 10 output
sorted string ['h', 'n', 'o', 'p', 't', 'y']
sorted list [1, 2, 3, 4]
sorted tup [1, 5, 7, 9]
Program 11
#simple calculator

a = int(input("Enter a number:"))
b = int(input("Enter another number:"))

print("Addition:", a+b)
print("subtraction:", a-b)
print("Division:", a/b)
print("Multiplication:", a*b)
Program 11 output
Enter a number:10
Enter another number:5
Addition: 15
subtraction: 5
Division: 2.0
Multiplication: 50
Program 12
#binary search linear search

arr = [10,20,30,40,50]
key = 30
found = False
for i in range(len(arr)):
if arr[i] == key:
print("linear search : found at position", i)
found = True
break
if not found:
print("linear search: not found")

low = 0
high = len(arr) - 1

while low <= high:


mid = (low+high)//2
if arr[mid]==key:
print("binary search: found at position",mid)
break
elif arr[mid]<key:
low = mid+1
else:
high = mid-1
Program 12 output
linear search: not found
linear search: not found
linear search : found at position 2
binary search: found at position 2

You might also like