0% found this document useful (0 votes)
3 views21 pages

Python Lab Final PDF

The document contains Python programming exercises for BCA IV Semester 2024-25, covering topics such as Fibonacci sequence checking, solving quadratic equations, summing natural numbers, displaying multiplication tables, checking for prime numbers, implementing a sequential search, creating a calculator, string functions, stack operations, file reading/writing, regular expressions, list and dictionary usage, SQLite database operations, GUI creation with Tkinter, exception handling, and drawing shapes using turtle graphics.
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 views21 pages

Python Lab Final PDF

The document contains Python programming exercises for BCA IV Semester 2024-25, covering topics such as Fibonacci sequence checking, solving quadratic equations, summing natural numbers, displaying multiplication tables, checking for prime numbers, implementing a sequential search, creating a calculator, string functions, stack operations, file reading/writing, regular expressions, list and dictionary usage, SQLite database operations, GUI creation with Tkinter, exception handling, and drawing shapes using turtle graphics.
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

PYTHONPROGRAMMING BCAIV Sem2024-25

PART-A

1. Checkifa numberbelongs to theFibonacci Sequence.

n=int(input("Enter the number you want to check:"))


n1=0
n2=1
while(n1<=n):
s=n1
n3=n1+n2
n1=n2
n2=n3
if(s==n):
print("It comes under Fibonacci")
else:
print("Itdoesn'tcomeunderFibonacci")

OUTPUT:
Enter the number you want to check:5It comesunder Fibonacci

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

2. SolveQuadraticEquations.

a=int(input("Enter a value:"))
b=int(input("Enter b value:"))
c=int(input("Enter c value:"))
disc=b*b-4*a*c
if(disc==0):
print("Roots are real and same")
x1=x2=-b/2*a
print("Root1=Root2=",x1)
elif(disc>0):
print("Roots are real but distinct")
x1=(-b+disc**0.5)/2*a
x2=(-b-disc**0.5)/2*a
print("Root1=",x1,"Root2=",x2)
else:
print("Roots are imaginary")

OUTPUT:
Enter a value:1
Enter b value:2
Enter c value:3
Roots are imaginary

Enter a value:2
Enter b value:3
Entercvalue:1
Roots are real but distinct Root1=-2.0Root2=-4.0

Enter a value:1
Enter b value:21
Entercvalue:1
Roots are real and sameRoot1=Root2=-1.0

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

3) Findthe sumof n natural numbers.

n=int(input("Enter n value:"))
i=1
sum=0
while(i<=n):
sum=sum+i
i=i+1
print("The sum of n natural numbers=",sum)

OUTPUT:
Enternvalue: 10
The sum of n natural numbers = 55

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

4) Display Multiplication Tables.

n=int(input("Enter n value:"))
print("The multiplication of table:")
for i in range(1,11):
print(n,"*",i,"=",n*i)

OUTPUT:
Entern value:5
The multiplication of table: 5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

5) Checkifa givennumberis aprimenumberor not.

n=int(input("Enter n value:"))
if n>1:
for i in range(2,int(n/2)+1):
if (n%i)==0:
print(n,"is not prime number")break
else:
print(n,"is a prime number")
else:
print(n,"is not a prime number")

OUTPUT:
Enter n value: 5
5 is a prime number

Enter n value:10
10 is not prime number

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

6) Implement a sequential search.

n=int(input("Enter n value:"))
array=[]
print("Enter n random numbers:")
foriinrange(n):
element=int(input())
[Link](element)
element=int(input("Enter element to search:"))
loc=0
foriin range(n):
if array[i]==element:
loc=i
break
ifloc!=0:
print("success")
else:
print("Unsuccess")

OUTPUT:
Entern value:5
Enter n random numbers:2
4
6
8
10
Enter element to search: 6 success

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

7) Create a calculator program.

a=float(input("Enter a value:"))
b=float(input("Enter b value:"))
c=input("Enter one operator for calculation:")
print(c)
match c:case"+":
print("Addition",a+b)case"-":
print("Subtraction",a-b)case "*":
print("Mutiplication",a*b)case "/":
print("Division",a/b)case "**":
print("apowerb",a**b)

OUTPUT:
Enter a value:5
Enter b value:5
Enter one operator for calculation:+
+
Addition10.0

Enter a value:5
Enterbvalue:5
Enter one operator for calculation:-
-
Subtraction0.0

Enter a value:5
Enterbvalue:5
Enter one operator for calculation:*
*
Mutiplication 25.0

Enter a value:5
Enter b value:5
Enter one operator for calculation:/

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

/
Division1.0

Enter a value:5
Enter b value:5
Enter one operator for calculation:**
**
A power b 3125.0

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

8) String function program.

str='Python is easy!!!'
print(str)
print(str[0])
print(str[3:9])
print(str[4:])
print(str[-1])
print(str[:5])
print(str*2)
print(str+"ISN'TIT?")

OUTPUT:

Python is easy!!!P
hon is
on is easy!!!
!
Pytho
Python is easy!!!Python is easy!!!Pythoniseasy!!!ISN'TIT?

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

9) Implement Stack.

stack=[1,2,3,4,5,6]
print("original stack is:",stack)
[Link](7)
print("stack after push operation is:",stack)
[Link]()
print("stack after pop operation is:",stack)
last_element_index=len(stack)-1
print("value obtained after peep perationis:",stack[last_element_index])

OUTPUT:

Original stackis: [1,2,3,4,5,6]


Stack after push operationis: [1,2,3,4,5,6,7]
stack after pop operation is: [1,2,3,4,5,6]
value obtained after peep operationis:6

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

10) Read and write into a file.

# openafilefile=open("[Link]","w")# write datatothefile


[Link]("python is a programming language.\n At its
best!!\n")#readthedata fromfile

file=open("[Link]","r")
print("Data of the file is:\n",[Link]())#closes openedfile
[Link]()

OUTPUT:

Data of the file is:


python is a programming [Link] its best!!

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

PART-B

1) Demonstrate usage of basic regular expression.


import re

pattern = '^a...s$'test_string='abyss'
result=[Link](pattern,test_string)

ifresult:
print("Search successful.")
else:
print("Searchunsuccessful.")

OUTPUT:
Search successful.

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

2) Demonstrate use of advanced regular expressions for data


validation.

import re
txt="Theraininspain"
x= [Link]("^The.*spain S",txt)
if x:
print("yes! we have a match!")
else:
print("nomatch")
txt="all is well all but love is goal spain"
x=[Link]("GFGC.*spain$",txt)
if x:
print("yes ! we have a match")
else:
print("no match")txt="The rain in spain"
x=[Link]("ai",txt)
print(x)
txt="The rain in spain"x=[Link]("ia",txt)
print(x)
txt="The rain in spain"x=[Link]("\s","a",txt)
print(x)
txt="The rain in spain"x=[Link]("\s",txt)
print(x)

OUTPUT:

no match no match['ai', 'ai'][]


The araina in a spain['The','rain','in','spain']

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

3) Demonstrate use of List.

list=[2,1.8,-7,"Anil","Ajay"]
print(list)
print(list[0])
print(list[1:3])
print(list[-1])
print(list*2)
list[1]=1.5
print(list)

OUTPUT:

[2,1.8,-7,'Anil','Ajay']
2
[1.8, -7]
Ajay
[2,1.8,-7,'Anil','Ajay',2,1.8, -7,'Anil','Ajay']
[2,1.5,-7,'Anil','Ajay']

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

4) Demonstrate use of Dictionaries.

# creating a dictionarycountry_
capitals={"United States": "Washington D.C.","Italy": ome","England":"London"}

# printing the dictionary


print(country_capitals)

OUTPUT:

{'UnitedStates':'WashingtonD.C.','Italy':'Rome','England':'London'}

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

5) Create SQLite Data base and Perform Operations on Tables.

Import sqlite3
# Connecting to sqlite#connectionobject
connection_obj=[Link]('[Link]')

#cursorobject
cursor_obj=connection_obj.cursor()

# Drop the GEEK table if already


exists.cursor_obj.execute("DROPTABLEIFEXISTSGEEK")

#Creating table
table=""" CREATETABLEGEEK(EmailVARCHAR(255)NOTNULL,
First_Name CHAR(25) NOT NULL,Last_NameCHAR(25),Score INT); """

cursor_obj.execute(table)

print("TableisReady")

# Close the connectionconnection_obj.close()

OUTPUT:

Table is Ready

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

6) Createa GUI using Tkinter module.

from tkinter import*root=Tk()


frame=Frame(root)
[Link]()
bottomframe=Frame(root)
[Link](side=BOTTOM)
redbutton=Button(frame,text='Red',fg='red')
[Link](side=LEFT)
greenbutton=Button(frame,text='Green',fg='green')
[Link](side=LEFT)
bluebutton=Button(frame,text='Blue', fg='blue')
[Link](side=LEFT)
blackbutton=Button(frame,text='Black',fg='black')
[Link](side=LEFT)
[Link]()

OUTPUT:

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

7) Demonstrate Exceptions in Python.

a=int(input("Enter a value:"))
b=int(input("Enter b value:"))
try:
c=a/b
print("Quotient=",c)
except:
print("Denominator cannot be zero")
print("Good job")

OUTPUT:

Enter a value:12
Enter b value:6
Quotient= 2.0Good job

Enter a value:10
Enterb value:0
Denominator cannot be zeroGood job

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

8) Star using turtle.

import

[Link]()

[Link](200,-100)

[Link]()

[Link](300,-300)

[Link](100,-300)

[Link](200,-100)

[Link]()

[Link](100,-150)

[Link]()

[Link](300,-150)

[Link](200,-350)

[Link](100,-150)

[Link](300,-150)

OUTPUT:

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

9) Draw cube using turtle.

import
[Link]()
[Link](150,-300)
[Link]()
[Link](300,-300)
[Link](300,-150)
[Link](150,-150)
[Link](150,-300)
[Link](250,-250)

[Link](400,-250)
[Link](400,-100)
[Link](250,-100)
[Link](250,-250)
[Link](400,-250)

[Link](300,-300)
[Link](300,-150)
[Link](400,-100)
[Link](250,-100)
[Link](150,-150)

OUTPUT:

GovernmentDegreeCollege,Sindhanur
PYTHONPROGRAMMING BCAIV Sem2024-25

GovernmentDegreeCollege,Sindhanur

You might also like