0% found this document useful (0 votes)
10 views26 pages

East Delhi Public School

The document is a Computer Science report file for a student named Ritesh Kumar Pal from East Delhi Public School, detailing various Python programs created during the 2022-2023 academic session. It includes programs for mathematical calculations, data structures, file handling, and MySQL database interactions. Each program is presented with its code, purpose, and sample outputs.

Uploaded by

anugreh7524
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)
10 views26 pages

East Delhi Public School

The document is a Computer Science report file for a student named Ritesh Kumar Pal from East Delhi Public School, detailing various Python programs created during the 2022-2023 academic session. It includes programs for mathematical calculations, data structures, file handling, and MySQL database interactions. Each program is presented with its code, purpose, and sample outputs.

Uploaded by

anugreh7524
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/ 26

East DElhi public school

CS REPORT FILE
coDE FilE
sEssioN 2022-2023
NaME:-RitEsh KuMaR pal
class:-12th ‘b’
Roll No. :-34
subJEct:-coMputER sciENcE
subMittED to:- MR.s pRiyaNKa MaM
subMittED by:- RitEsh KuMaR pal
Certificate
This is to certify that
RITESH KUMAR PAL of
class XII Springdale has
worked on the project
“SOME CODEING FILE”
to my full satisfaction.

DATE:-

SIGNATURE:-
{MR.s PRIYANKA MAM}
It is a pleasure to acknowledge many
people who knowingly helped me
complete my project. First and
foremost, I would like to express my
regards towards PRIYANKA
MAM the honorable teacher of my
school for her encouragement and
guidance. I would also like to express
my immense gratitude towards ,
cooperation and support extended
during the completion of this project.

(RITESH KUMAR PAL)


Program:1

Write a python Program to take input for a number, calculate and print its square

and cube?

a=int(input("Enter any no "))

b=a*a

c=a*a*a

print("Square = ",b)

print("cube = ",c)

Output:

Enter any no 10Square = 100cube = 1000>>>

Program:2

Write a python program to take input for 2 numbers, calculate and print their sum,

product and difference?

a=int(input("Enter 1st no "))

b=int(input("Enter 2nd no "))

s=a+b

p=a*b

if(a>b):

d=a-b

else:
d=b-a

print("Sum = ",s)

print("Product = ",p)

print("Difference = ",d)

Output:

Enter 1st no 10Enter 2nd no 20Sum = 30Product = 200Difference


= 10>>>

Program:3

Write a python program to take input for 3 numbers, check and print the largest

number?

a=int(input("Enter 1st no "))

b=int(input("Enter 2nd no "))

c=int(input("Enter 3rd no "))

if(a>b and a>c):

m=a

else:

if(b>c):

m=b

else:

m=c

print("Max no = ",m)
Output:

Enter 1st no 25Enter 2nd no 63Enter 3rd no 24Max no = 63>>>

Method:2

num1=int(input("Enter 1st no "))

num2=int(input("Enter 2nd no "))

num3=int(input("Enter 3rd no "))

if(num1>num2 and num1>num3):

max=num1

elif(num2>num3):

max=num2

else:

max=num3

print("Max no = ",max)

Output:

Enter 1st no 25Enter 2nd no 98Enter 3rd no 63Max no = 98>>>


Program:4

Write a python program to take input for 2 numbers and an operator (+ , – , * ,

/ ). Based on the operator calculate and print the result?

a=int(input("Enter 1st no "))

b=int(input("Enter 2nd no "))

op=input("Enter the operator (+,-,*,/) ")

if(op=="+"):

c=a+b

print("Sum = ",c)

elif(op=="*"):

c=a*b

print("Product = ",c)

elif(op=="-"):

if(a>b):

c=a-b

else:

c=b-a

print("Difference = ",c)

elif(op=="/"):

c=a/b

print("Division = ",c)

else:
print("Invalid operator")

First Run Output:

Enter 1st no 10Enter 2nd no 20Enter the operator (+,-,*,/)


+Sum = 30>>>

Second Run Output:

Enter 1st no 10Enter 2nd no 36Enter the operator (+,-,*,/)


–Difference = 26>>>

Program: 5 Write a python program to take input for a number and print its table?

num=int(input("Enter any no "))

i=1

while(i<=10):

table=num*I

print(num," * ",i," = ",table)

i=i+1

Output:

Enter any no 55 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 =


305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 50>>>

Program:6

Write a python program to take input for a number and print its factorial?

n=int(input("Enter any no "))

i=1

f=1
while(i<=n):

f=f*i

i=i+1

print("Factorial = ",f)

Output:

Enter any no 5Factorial = 120>>>

Program:7

Write a python program to take input for a number check if the entered number is

Armstrong or not.

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

n1=n

s=0

while(n>0):

d=n%10

s=s + (d *d * d)

n=int(n/10)

if s==n1:

print("Armstrong Number")

else:

print("Not an Armstrong Number")


First Run Output:

Enter the number to check : 153Armstrong Number>>>

Second Run Output:

Enter the number to check : 152Not an Armstrong Number>>>

Program:8

Write a python program to take input for a number and print its factorial using

recursion?

#Factorial of a number using recursion

def recur_factorial(n):

if n == 1:

return n

else:

return n*recur_factorial(n-1)

#for fixed number

num = 7

#using user input

num=int(input("Enter any no "))

#check if the number is negative

if num < 0:
print("Sorry, factorial does not exist for negative
numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

print("The factorial of", num, "is",


recur_factorial(num))

Output:

Enter any no 5The factorial of 5 is 120>>>

Program:9

Write a python program to Display Fibonacci Sequence Using Recursion?

#Python program to display the Fibonacci sequence

def recur_fibo(n):

if n <= 1:

return n

else:

return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = int(input("Enter upto which term you want to


print"))

#check if the number of terms is valid


if (nterms <= 0):

print("Plese enter a positive integer")

else:

print("Fibonacci sequence:")

for i in range(nterms):

print(recur_fibo(i))

Output:

Fibonacci sequence:0112358132134>>>

Program: 10

Write a python program to maintain book details like book code, book title and price

using stacks data structures? (implement push(), pop() and traverse() functions)

"""

push

pop

traverse

"""

book=[]

def push():

bcode=input("Enter bcode ")

btitle=input("Enter btitle ")

price=input("Enter price ")

bk=(bcode,btitle,price)
book.append(bk)

def pop():

if(book==[]):

print("Underflow / Book Stack in empty")

else:

bcode,btitle,price=book.pop()

print("poped element is ")

print("bcode ",bcode," btitle ",btitle," price ",price)

def traverse():

if not (book==[]):

n=len(book)

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

print(book[i])

else:

print("Empty , No book to display")

while True:

print("1. Push")

print("2. Pop")

print("3. Traversal")

print("4. Exit")

ch=int(input("Enter your choice "))

if(ch==1):

push()
elif(ch==2):

pop()

elif(ch==3):

traverse()

elif(ch==4):

print("End")

break

else:

print("Invalid choice")

Output:

1. Push2. Pop3. Traversal4. Exit

Enter your choice 1

Enter bcode 101

Enter btitle python

Enter price 254

1. Push2. Pop3. Traversal4. ExitEnter your choice 3

(‘101’, ‘python’, ‘254’)

1. Push2. Pop3. Traversal4. ExitEnter your choice


Program:11

Write a python program to maintain employee details like empno,name and salary

using Queues data structure? (implement insert(), delete() and traverse() functions)

#queue implementation (using functions)

#program to create a queue of employee(empno,name,sal).

"""

add employee

delete employee

traverse / display all employees

"""

employee=[]

def add_element():

empno=input("Enter empno ")

name=input("Enter name ")

sal=input("Enter sal ")

emp=(empno,name,sal)

employee.append(emp)

def del_element():

if(employee==[]):

print("Underflow / Employee Stack in empty")

else:

empno,name,sal=employee.pop(0)
print("poped element is ")

print("empno ",empno," name ",name," salary ",sal)

def traverse():

if not (employee==[]):

n=len(employee)

for i in range(0,n):

print(employee[i])

else:

print("Empty , No employee to display")

while True:

print("1. Add employee")

print("2. Delete employee")

print("3. Traversal")

print("4. Exit")

ch=int(input("Enter your choice "))

if(ch==1):

add_element()

elif(ch==2):

del_element();

elif(ch==3):

traverse()

elif(ch==4):

print("End")
break

else:

print("Invalid choice")

Output:

1. Add employee2. Delete employee3. Traversal4. Exit

Enter your choice 1Enter empno 101Enter name AmitEnter sal


45000

1. Add employee2. Delete employee3. Traversal4. ExitEnter


your choice 3(‘101’, ‘Amit’, ‘45000’)

1. Add employee2. Delete employee3. Traversal4. ExitEnter


your choice

Program:12

Write a python program to read a file named “article.txt”, count and print total

alphabets in the file?

def count_alpha():

lo=0

with open("article.txt") as f:

while True:

c=f.read(1)

if not c:

break

print(c)

if((c>='A' and c<='Z') or (c>='a' and c<='z')):


lo=lo+1

print("total lower case alphabets ",lo)

#function calling

count_alpha()

Output:

Hello how are you12123byetotal lower case alphabets 17>>>

Program:13

Write a python program to read a file named “article.txt”, count and print the

following:

(i) length of the file(total characters in file)

(ii)total alphabets

(iii) total upper case alphabets

(iv) total lower case alphabets

(v) total digits

(vi) total spaces

(vii) total special characters

def count():

a=0

ua=0

la=0

d=0

sp=0
spl=0

with open("article.txt") as f:

while True:

c=f.read(1)

if not c:

break

print(c)

if((c>='A' and c<='Z') or (c>='a' and c<='z')):

a=a+1

if(c>='A' and c<='Z'):

ua=ua+1

else:

la=la+1

if(c>='0' and c<='9'):

d=d+1

if(c==''):

sp=sp+1

else:

spl=spl+1

print("total alphabets ",a)

print("total upper case alphabets ",ua)

print("total lower case alphabets ",la)

print("total digits ",d)


print("total spaces ",sp)

print("total special characters ",spl)

# function calling

count()

Output:

Welcome to cbsepython.

('total alphabets ', 19)

('total upper case alphabets ', 1)

('total lower case alphabets ', 21)

('total digits ', 0)

('total spaces ', 0)

('total special characters ', 22)

>>>

Program: 14

Write a python program to read a file named “article.txt”, count and print total

words starting with “a” or “A” in the file?

def count_words():

w=0

with open("article.txt") as f:

for line in f:

for word in line.split():


if(word[0]=="a" or word[0]=="A"):

print(word)

w=w+1

print("total words starting with 'a' are ",w)

# function calling

count_words()

Output:

Amit

Ankur

and

Ajay

("total words starting with 'a' are ", 4)

>>>>

computer science practical file for class 12 python 2021-22

Program: 15

Write a python program to read a file named “article.txt”, count and print total

lines starting with vowels in the file?

filepath = 'article.txt'

vowels="AEIOUaeiou"

with open(filepath) as fp:


line = fp.readline()

cnt = 1

while line:

if(line[0] in vowels):

#print(line)

print("Line {}: {}".format(cnt, line.strip()))

cnt=cnt+1

line = fp.readline()

Output:

Line 1: amitLine 2: owlLine 3: Eat apple a day and stay


healthyLine 4: Anmol

Program:16

Python interface with MySQL

Write a function to insert a record in table using python and MySQL


interface.

def insert_data():

#take input for the details and then save the record in
the databse

#to insert data into the existing table in an existing


database

import mysql.connector

db =
mysql.connector.connect(host="localhost",user="root",
password="admin")
c = db.cursor()

r=int(input("Enter roll no "))

n=input("Enter name ")

p=int(input("Enter per "))

try:

c.execute("insert into student (roll,name,per) values


(%s,%s,%s)",(r,n,p))

db.commit()

print("Record saved")

except:

db.rollback()

db.close()

# function calling

insert_data()

Output:

Enter roll no 101Enter name amitEnter per 97Record saved>>>

Program 17:

Python interface with MySQL

Write a function to display all the records stored in a table using


python and MySQL interface.

def display_all():

import mysql.connector
db =
mysql.connector.connect(host='localhost',user='root',
passwd='admin',database='test4')

try:

c = db.cursor()

sql='select * from student;'

c.execute(sql)

countrow=c.execute(sql)

print("number of rows : ",countrow)

data=c.fetchall()

print("=========================")

print("Roll No Name Per ")

print("=========================")

for eachrow in data:

r=eachrow[0]

n=eachrow[1]

p=eachrow[2]

print(r,' ',n,' ',p)

print("=========================")

except:

db.rollback()

db.close()

# function calling

display_all()
Output:

number of rows : 2=========================Roll No


Name Per=========================102 aaa 99101 amit
97=========================>>>

Program :18

Python interface with MySQL

Write a function to search a record stored in a table using


python and MySQL interface.

def search_roll():

import mysql.connector

db =
mysql.connector.connect(host="localhost",user="root",
passwd="admin",database="test")

try:

z=0

roll=int(input("Enter roll no to search "))

c = db.cursor()

sql='select * from student;'

c.execute(sql)

countrow=c.execute(sql)

print("number of rows : ",countrow)

data=c.fetchall()

for eachrow in data:

r=eachrow[0]

n=eachrow[1]
p=eachrow[2]

if(r==roll):

z=1

print(r,n,p)

if(z==0):

print("Record is not present")

except:

db.rollback()

db.close()

# function calling

search_roll()

Output:

Enter roll no to search 101number of rows : 2101 amit 97>>>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy