0% found this document useful (0 votes)
88 views

Python Programming Lab

The document discusses a report on Python programming laboratory submitted by a student. It contains the student's details, certificate from the university and various Python programs written by the student as part of their curriculum.

Uploaded by

darshangd9899
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)
88 views

Python Programming Lab

The document discusses a report on Python programming laboratory submitted by a student. It contains the student's details, certificate from the university and various Python programs written by the student as part of their curriculum.

Uploaded by

darshangd9899
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/ 22

Visvesvaraya Technological University,Belagavi

POSTGRADUATION
CENTRE ,KALABURAGI
BACHELOR OF TECHNOLOGY(B.Tech)
IN
ELECTRONICS AND COMPUTER ENGINEERING

A
Report on
PYTHON PROGRAMMING
LABORATORY ( BUEL305 )

Submitted as a part of Academic Requirement for Third Semester


By

USN :

Department of Computer Electronics and computer Engineering,


Postgraduation Centre ,Visvesvaraya Technological University,
KALABURAGI

2023 - 2024
VISVESVARAYA TECHNOLOGICAL UNIVERSITY,
BELAGAVICPGS KALABURAGI

U. S.N: DATE: / / 2024

CERTIFICATE

This is to certify that Mr./Ms.…………………………………………..


Studying in the Department of ECE 3rd- Semester has successfully completed all
the Programs in……………………………with subject as prescribed by the
Visvesvaraya Technological University, Belagavi CPGS Kalaburagi
For the academic year 2023- 2024.

Faculty Incharge Programme Coordinator

Examiners:

1.Internal Examiner 2.Internal Examiner


1) a. Develop a python program to count the number of occurrences
of each digit in the given input number.

n=int(input("enter any integer value:"))


n=str(n)
m=input("enter the number of count:")
print("count is:",n.count(m))

OUTPUT :

enter any integer value:123445322


enter the number of count:2
count is: 3
b. Write a python program to compute the GCD of two numbers.

import math
print("the gcd of 60 and 48 is:",end="")
print(math.gcd(60,48))

OUTPUT :-

the gcd of 60 and 48 is:12


2) a. Write a python program to print the Fibonacci sequence.

def fibonaci(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fibonaci(n-1)+fibonaci(n-2)
for i in range(5):
print(fibonaci(i))

OUTPUT :

0
1
1
2
3
b. Develop a python program to convert a given decimal number to
Binary, Octal and Hexadecimal using functions.

dec=60
print("the decimal value of ",dec,"is:")
print(bin(dec),"in binary.")
print(oct(dec),"in octal.")
print(hex(dec),"in hexadecimal.")

OUTPUT :

the decimal value of 60 is:


0b111100 in binary.
0o74 in octal.
0x3c in hexadecimal.
3 ) a. Develop a python program to print the following pattern.

A
BC
DEF
GHIJ
KLMNO

def contalpha(n):
num=65
for i in range(0,n):
for j in range(0,i+1):
ch=chr(num)
print(ch,end="")
num=num+1
print("\r")
n=5
contalpha(n)

OUTPUT :-

BC

DEF

GHIJ

KLMNO
b. Write a python program that accepts a senstence and find the
number of words, digits, uppercase letters and lowercase letters.

s=input("enter a sentence:")
w,d,u,l=0,0,0,0
lw=s.split()
w=len(lw)
for c in s:
if c.isdigit():
d=d+1
elif c.isupper():
u=u+1
elif c.islower():
l=l+1
print("no of words:",w)
print("no of digits:",d)
print("no of uppercase letters:",u)
print("no of lowercase letters:",l)

OUTPUT :-

enter a sentence:YUKTI 2024 was greatest function in vtu


no of words: 7
no of digits: 4
no of uppercase letters: 5
no of lowercase letters: 24
4) a. Write a python program to swap two elements in a list.

n1=15
n2=30
n1=input("enter the value of x:")
n2=input("enter the value of y:")
temp=n1
n1=n2
n2=temp
print("the value of x after swapping:{}".format(n1))
print("the value of y after swapping:{}".format(n2))

OUTPUT :-

enter the value of x:15


enter the value of y:30
the value of x after swapping:30
the value of y after swapping:15
b. Write a program to convert roman numbers into integer values
using dictionaries.

class py_solution:
def roman_to_int(self,s):
rom_val={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
int_val=0
for i in range(len(s)):
if i>0 and rom_val[s[i]]>rom_val[s[i-1]]:
int_val+=rom_val[s[i]]-2*rom_val[s[i]]
else:
int_val+=rom_val[s[i]]
return int_val
print(py_solution().roman_to_int('MMMMLXXXVI'))
print(py_solution().roman_to_int('MMMD'))
print(py_solution().roman_to_int('C'))

OUTPUT :

4086
3500
100
5) a. Develop a python program that could search the text in a
file for phone numbers (+919900889977) and email addressess (
sample@gmail.com ).

import re
phone_regex=re.compile(r'\+\d{12}')
email_regex=re.compile(r'[A-Za-z0-9._]+@[A-Za-z0-9]+\.[A-Z|a-z]{2,}')
with open('b.txt','r')as f:
for line in f:
matches=phone_regex.findall(line)
for match in matches:
print(match)
matches=email_regex.findall(line)
for match in matches:
print(match)

OUTPUT :

+919900889977
sample@gmail.com
b. Write a python program to extract year, month and date from the
URL.

import re
def extract_date(url):
return re.findall(r'/(\d{4})/(\d{1,2})/(\d{1,2})/',url)
urll="www.vtu.ac.in/2020/02/22/"
print(extract_date(urll))

OUTPUT :

[('2020', '02', '22')]


6) Write a python program to create a ZIP file of a particular
folder which contains several files inside it.

import zipfile
import os

def zip_folder(folder_path, zip_filename):


with zipfile.ZipFile(zip_filename, 'w') as zip_file:
for foldername, subfolders, filenames in os.walk(folder_path):
for filename in filenames:
file_path = os.path.join(foldername, filename)
arcname = os.path.relpath(file_path, folder_path)
zip_file.write(file_path, arcname)

folder_to_zip = "/path/to/your/folder"
zip_file_name = "output.zip"

zip_folder(folder_to_zip, zip_file_name)
print(f"Zip file '{zip_file_name}' created successfully.")

OUTPUT :

Zip file 'output.zip' created successfully.


7) Write a python program by creating a class called Employee to
store the details of Name, Employee ID, Department and Salary,
and implement a method to update salary of employees belonging
to a given department.

class employee:
def _init_(self):
self.name=""
self.empID=""
self.dept=""
self.salary=0
def getEmpDetails(self):
self.name=input("enter emp name: ")
self.empID=input("enter emp empID: ")
self.dept=input("enter emp dept: ")
self.salary=int(input("enter emp salary: "))
def showEmpDetails(self):
print("employee details")
print("name:",self.name)
print("empID:",self.name)
print("dept:",self.dept)
print("salary:",self.salary)
def updtsalary(self):
self.salary=int(input("enter the new salary:"))
print("updated salary:",self.salary)
e1=employee()
e1.getEmpDetails()
e1.showEmpDetails()
e1.updtsalary()
OUTPUT :

enter emp name: admin


enter emp empID: ADMIN22EE001
enter emp dept: engineering
enter emp salary: 100000
employee details
name: admin
empID: admin
dept: engineering
salary: 100000
enter the new salary:200000
updated salary: 200000
8) Write a python program to find the whether the given input is
palindrome or not ( for both string and integer ) using the concept
of polymorphism and inheritance.
class A:
def __init__(self, s):
self.s = s
def palindrome(self):
rev = self.s[::-1]
if rev == self.s:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
class B(A):
def __init__(self, s):
super().__init__(s)
def palindrome(self):
temp = self.s
rev = 0
while self.s != 0:
dig = self.s % 10
rev = rev * 10 + dig
self.s = self.s // 10
if temp == rev:
print("The number is a palindrome")
else:
print("The number isn't a palindrome")
string_input = input("Enter the string: ")
integer_input = int(input("Enter the integer: "))
a = A(string_input)
b = B(integer_input)
a.palindrome()
b.palindrome()
OUTPUT : Enter the string: MADAM
Enter the integer: 12321
The string is a palindrome
The number is a palindrome
DEMONSTRATION EXPERIMENTS ( FOR CIE )

1) Demonstrate read and write operation on spreadsheet.

from openpyxl import Workbook, load_workbook

wb = Workbook()

ws = wb.active

ws['A1'] = 'Hello'

ws['B1'] = 'World'

wb.save('example.xlsx')

wb = load_workbook('example.xlsx')

ws = wb.active

print(ws['A1'].value)

print(ws['B1'].value)

OUTPUT :

Hello

World
2) Develop a python program to combine selected pages from many
PDFs.

from PyPDF2 import PdfWriter, PdfReader


num = int(input("Enter page number you want combine from multiple documents "))
pdf1 = open('birds.pdf', 'rb')
pdf2 = open('birdspic.pdf', 'rb')
pdf_writer = PdfWriter()
pdf1_reader = PdfReader(pdf1)
page = pdf1_reader.pages[num - 1]
pdf_writer.add_page(page)
pdf2_reader = PdfReader(pdf2)
page = pdf2_reader.pages[num - 1]
pdf_writer.add_page(page)
with open('output.pdf', 'wb') as output:
pdf_writer.write(output)

OUTPUT:

Enter page number you want combine from multiple documents : 3


birds
birdspic
output
3) Generate a QR Code using Python.

import pyqrcode
from pyqrcode import QRCode
dest='www.google.com'
myQR=QRCode(dest)
myQR.show()

OUTPUT :-
4) Create a Quiz game using Python.

print("Wellcome to quiz game !!")


print('NOTE: if your spelling is incorrect then it is considered as wrong answer')
score = 0
question_no = 0
playing = input('Do you want to play ? ').lower()
if playing == 'yes':
question_no += 1
ques = input(f'\n{question_no}. what does CPU stand for? ').lower()
if ques == 'central processing unit':
score +=1
print('correct! you got 1 point')
else:
print('Incorrect!')
print(f'current answer is --> central processing unit')
question_no += 1
ques = input(f'\n{question_no}. what does GPU stand for? ').lower()
if ques == 'graphics processing unit':
score +=1
print('correct! you got 1 point')
else:
print('Incorrect!')
print(f'current answer is --> graphics processing unit')
question_no += 1
ques = input(f'\n{question_no}. what does RAM stand for? ').lower()

if ques == 'random access memory':


score +=1
print('correct! you got 1 point')
else:
print('Incorrect!')
print(f'current answer is --> random access memory')
question_no += 1
ques = input(f'\n{question_no}. what does PSU stand for? ').lower()

if ques == 'power supply unit':


score +=1
print('correct! you got 1 point')

else:
print('Incorrect!')
print(f'current answer is --> power supply unit')

question_no += 1
ques = input(f'\n{question_no}. what does ROM stand for? ').lower()

if ques == 'read only memory':


score +=1
print('correct! you got 1 point')

else:
print('Incorrect!')
print(f'current answer is --> read only memory')

else:
print('thankyou you are out of a game.')
quit()

print(f'\nnumber of question is {question_no}')


print(f'your score is {score}')
try:
percentage = (score *100)/question_no
except ZeroDivisionError:
print('0% quetions are correct')

print(f'{percentage}% questions are correct.')


OUTPUT:
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit


correct! you got 1 point

2. what does GPU stand for? graphics processing unit


correct! you got 1 point

3. what does RAM stand for? RAM


Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit


Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory


correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.

You might also like