0% found this document useful (0 votes)
31 views31 pages

Computer Science Project by Amit

Uploaded by

srishti8851
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)
31 views31 pages

Computer Science Project by Amit

Uploaded by

srishti8851
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

ARYA VIDYA MANDIR SR. SEC.

SCHOOL

ACADEMIC YEAR: 2024-25

PROJECT FILE

SUBMITTED BY: SUBMITTED TO:

NAME : Amit Mrs. Monika Goyal


ROLL NO. : ____________ PGT(Computer)
CLASS/SEC : XII - D
SUBJECT : COMPUTER SCIENCE(083)
PROGRAM 1
AIM: WAP to create a calculator?
Code:
s=0
def add():
a=int(input("enter a no."))
b=int(input("enter a no."))
s=a+b
print(s)
def subtract():
a=int(input("enter a no."))
b=int(input("enter a no."))
s=a-b
print(s)
def multiply():
a=int(input("enter a no."))
b=int(input("enter a no."))
s=a*b
print(s)
def divide():
a=int(input("enter a no."))
b=int(input("enter a no."))
s=a/b
print(s)
def square():
n=int(input("enter a no. whose square you want"))
print(n**2)
print("what do u want 1 add 2 subtract 3 multiply 4 divide 5 square ")
c=int(input("what do u want"))
if c==1:
add()
elif c==2:
subtract()
elif c==3:
multiply()
elif c==4:
divide()
else:
square()
print("amit")
output
PROGRAM 2
AIM: WAP to search operation in file?
Code:
import pickle
f=open("[Link]","rb")
try:
while True:
s=[Link](f)
if s["salary"]>20000:
print(s["name"])
except:
[Link]()
print (“amit”)
PROGRAM 3
AIM: WAP to update a file.
Code:
import pickle
def update():
f=open("[Link]","rb+")
try:
while True:
pos=[Link]()
s=[Link](f)
if s["name"]=="mohit":
s["name"]="mohit ji"
[Link](pos)
[Link](s,f)
except:
[Link]()
PROGRAM 4
AIM: WAP to start with a vowels.
Code:
f=open("[Link]","r")
s=[Link]()
for i in s:
if i[0] not in "aeiouAEIOU":
print(i)
print("amit")

PROGRAM 5
AIM: WAP to operate a text file?
Code:
f=open("[Link]","w")
s="hlo python"
[Link](s)
[Link]()
print("amit")

PROGRAM 6
AIM: calculate size of file?
Code:
f=open("[Link]","r")
s=[Link]()
l=len(s)
print("total no. of bytes/size",l)
print("amit")

PROGRAM 7
AIM: WAP to display total no. of words in a file?
Code:
f=open("[Link]","r")
s=[Link]()
l=[Link]()
n=len(l)
print(n)
print("amit")

PROGRAM 8
AIM: WAP to display a total no. of line in a file?
Code:
f=open("[Link]","r")
s=[Link]()
l=len(s)
print("total no. of lines in a file",l)
print("amit")

PROGRAM 9
AIM: WAP to create operation on binary file?
Code:
import pickle
f=open("[Link]","wb")
d={"name":"amitji","empno.":432,"salary":39000}
d1={"name":"amrit ji","emp.":423,"salary":36000}
[Link](d,f)
[Link](d1,f)
[Link]()
print(“amit”)
PROGRAM 10
AIM: WAP to display a reading on binary file?
Code:
import pickle
f=open("[Link]","rb")
s=" "
try:
while True:
s=[Link](f)
print(s)
except:
print(";no more data to display")
[Link]()
print(“amit)
PROGRAM 11
AIM: WAP to operate on csv file?
Code:
import csv
f=open("[Link]","w")
a="y"
p=[Link](f)
while a=="y":
name=input("entert name")
rn=int(input("enter rollno."))
marks=int(input("enter ur marks"))
l=[name,rn,marks]
[Link](l)
a=input("do u want to enter new [Link]")
[Link]()
PROGRAM 12
AIM: WAP to read a binary file ?
Code:
import csv
f=open("[Link]","r")
r=[Link](f)
for i in r:
print(r)
prin(“amit”)
PROGRAM 13
AIM: WAP to implement different types of argument?
Code:
(i) positional
def check(x,y):
if x>10:
print("x is big")
elif y>10:
print("y is big")
check(2,23)
print(“amit”)
output:-
(ii) keyword:-
def check(x,y):
if x>10:
print("x is big")
elif y>10:
print("y is big")
check(y=2,x=23)
print(“amit”)
Output:-
(iii) default:-
def check(x,y=2):
if x>10:
print("x is big")
elif y>10:
print("y is big")
check(18)
print(“amit”)

Output:-
PROGRAM 14
AIM: WAP to implement stack operation?
Code:
(1) push()
(2) pop
(1) push()
stk=[]
top=None
def push():
if len(stk)==0:
[Link](items)
top=0
else:
[Link](items)
top=top+1
print(top)
print(“amit”)
(2) pop()
stk=[12,34]
a=[Link]()
print(a)
print(stk)
print("amit")
Output:-
PROGRAM 15
AIM: WAP to display global and local scope?
Code:
def add(x,y):
global a
z=x+y
a=a+10
print(z)
print(a)
a=12
b=12
add(a,b)

Local():-
def add(x,y):
z= x+y
print(a,b)
print(z)
a=12
b=19
add(a,b)
print(z)
print(“amit”)

Output:-

Display() :-
stk=[10,20,30,40]
def display(stk):
for i in stk:
print(i)
print("amit")
Output:-
Program 16
Ques16. Write a program to connect Python with MySQL using
database connectivity and perform the following operations on
data in database: Fetch, Update and delete the data.
A. CREATE A TABLE
import [Link]
demodb = [Link](host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=[Link]()
democursor execute("CREATE TABLE STUDENT (admn_no int primary
key, sname varchar(30), gender char(1). DOB date, stream
varchar(15), marks float(4.2))")

[Link] THE DATA


import [Link]
demodb = [Link](host="localhost", user="root",
passwd="computer", database="EDUCATION") democursor
[Link]() [Link]("insert into student values
(%s, %s, %s, %s, %s, %s)". (1245, 'Mayank Mangla'. 'M', '2007-09-11',
'science', 67.34))
[Link]()

C. FETCH THE DATA


import [Link]
demodb [Link](host="localhost", user="root".
passwd="computer", database="EDUCATION") democursor
[Link]() [Link]("select from student")
for i in democursor:
print(i)
[Link] THE RECORD
import [Link] demodb =
[Link](host="localhost", user="root".
passwd="computer", database="EDUCATION") democursor-
[Link]() [Link]("update student set
marks=55.68 where admn_no=4342")
[Link]()
E. DELETE THE DATA
import [Link]
demodb = [Link](host="localhost", user="root".
passwd="computer", database="EDUCATION")
[Link]()
[Link]("delete from student where admn_no=4342")
[Link]()
PROGRAM 17
SQL Command

Q1 create DATABASE
Syntax :

use name:

create table:
Syntax:

Q2 show database
Syntax:
show tables
Syntax:

Desc

Insert table

Q3 Alter command
Delete
Add

Update

Rename

Add primary key

Select command

To display selected coloum:


Todisplayselectedrow:

To select district row:

To select table
Select scalar expression

Logical relation

Sorting output: assending order

Descending order
Patter match

Select those student whose name letter 5:

Count of employer in a table :

PROGRAM 17
Aggregate function:
count(): select count(dept) from employee;
average(): select avg(salary) from employee;
sum(): select sum(salary) from employee;
max(): select max(salary) from employee;
min(): select min(salary) from employee;

Common questions

Powered by AI

Efficient data management in stack operations requires using an appropriate data structure like a list. In Python, standard stack operations include `push`, to add an element to the top of the stack, and `pop`, to remove the top element . Implementation involves checking conditions like whether the stack is empty before popping and handling the top index for push operations . These operations inherently follow the Last In, First Out (LIFO) principle, ensuring that the last added element is the one removed first, allowing efficient temporary data storage and management .

Interacting with a MySQL database using Python involves using the `mysql.connector` module, which provides methods for connecting to and executing SQL commands on a database . Operations include creating a connection object using `mysql.connector.connect` by specifying host, user, password, and database . Through a cursor object `democursor`, the essential CRUD operations can be performed such as creating tables, inserting data, fetching data using select queries, updating records, and deleting entries . Committing changes is necessary after insert, update, or delete operations to ensure that they are saved in the database .

CSV file operations in Python are best supported by the `csv` module, which allows for reading from and writing to CSV files conveniently . To write, open the CSV file in write mode using `open`, and utilize the `csv.writer` class to create a writer object . With this writer, use `writerow()` to write rows of data to the file . Reading involves opening the file in read mode and using `csv.reader` to iterate through the file content, extracting each row's data into lists for processing . The `csv` module provides a straightforward interface to handle CSV operations consistently and efficiently.

Binary files can efficiently store data as they save space by encoding data in binary form rather than as plain text, allowing for more compact file sizes . They are also faster to read from or write to because they do not require parsing as text does, which can be beneficial for large datasets or complex data structures . However, binary files are less human-readable, making them harder to debug or modify manually without specialized tools . They can be manipulated in Python using the `pickle` module to load and dump data, requiring careful handling of file modes and potential exceptions during operations like reading or updating .

Updating and deleting records in a MySQL database using Python involves first establishing a connection with the `mysql.connector.connect` method by providing necessary credentials . To update a record, create a cursor object and use `execute` with an appropriate SQL `UPDATE` query, specifying the table, the new values, and conditions for which records to update . After executing, `commit` changes to save updates . For deleting records, similarly, use an `EXECUTE` command with a `DELETE` query specifying the condition(s) to remove targeted records . Again, use `commit` to finalize the transaction .

To calculate a file's size in Python, first open the file in read mode with `open()`, ensuring binary mode for accuracy in bytes count . Read the entire file content as a string with `f.read()` and apply `len()` to determine the size in bytes . This method is applied in practice when you need to determine storage usage, especially for performance optimization or when managing disk space in applications that handle numerous or large files. Ensuring file closure after the operation is crucial for proper resource management .

Positional arguments are used in functions where the order of arguments is fixed; they are specified in the same order when the function is called . A practical scenario is when function parameters are heavily dependent on order, such as a function that calculates a mathematical formula with strict positional inputs. Keyword arguments allow calling the function with parameter names, not relying on order, making the code more readable by specifying which parameter is being set . This is practical when dealing with functions with many optional arguments, enhancing clarity. Default arguments provide a default value if none is given, allowing the function to be called with fewer arguments . This is especially useful in scenarios where you have optional parameters that do not always need to be specified.

The number of lines in a text file can be determined by reading the file with `f.readlines()` and using `len()` to get the count of lines . To count the number of words, read the file content using `f.read()`, split the content into words with `split()` function, and then use `len()` on the resulting list . For counting bytes, read the file into a string and directly apply the `len()` function to the string to get the total number of bytes (characters). These operations require opening the file in the appropriate mode before reading its content.

The `pickle` module is essential in Python for serializing and deserializing Python objects into binary files, allowing complex objects to be easily saved and loaded for later use, which text files do not support . This is vital for preserving object state, such as dictionaries or class instances, across program runs . However, risks include security vulnerabilities since `pickle` can execute arbitrary code if untrusted sources provide the data, leading to potential code injection attacks . Without proper trust verification on the source of the pickle data and adhering to secure coding practices, use of `pickle` can inadvertently expose applications to significant risks.

A basic calculator program in Python should focus on simplicity, input validation, and modularity . Simplicity ensures that the operations are direct and user input prompts are clear, as demonstrated by the program’s simplistic input prompts and direct conditional structures to choose operations . Input validation is crucial to prevent errors, reflected in the program's use of `int(input())` for number operations despite its vulnerability to runtime errors with non-integer input . Modularity is maintained by defining separate functions for each operation like add, subtract, multiply, divide, and square, which simplifies the main control structure .

You might also like