0% found this document useful (0 votes)
14 views70 pages

Ashish File 3.0

The document contains a series of Python programming tasks focused on various concepts such as functions, file handling, binary files, CSV files, and database operations using MySQL. Each task includes a brief description of the problem to be solved, along with input and output specifications. The tasks range from simple calculations and file manipulations to more complex database interactions with menu-driven programs.

Uploaded by

SEVAGE GAMING
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)
14 views70 pages

Ashish File 3.0

The document contains a series of Python programming tasks focused on various concepts such as functions, file handling, binary files, CSV files, and database operations using MySQL. Each task includes a brief description of the problem to be solved, along with input and output specifications. The tasks range from simple calculations and file manipulations to more complex database interactions with menu-driven programs.

Uploaded by

SEVAGE GAMING
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/ 70

1. WAP in Python to find the factorial of a numberusing function.

Input:

Output:
2. WAP in Python to implement default andpositional
parameters

Input:

Output:
3. Write a program in Python to input the value of x andn and print
the sum of the following series

1+x+x^2+x^3+.......X^n

Input:

Output:
4. WAP in Python to read a text file and printthe
number of vowels and consonants in the file.

Input:

Output:
5. WAP in Python to read a text file and print theline or
paragraph starting with the letter 'S'

Input:

Output:

6. WAP in Python to read a text fileand print thenumber of


uppercase and lowercase letters in the file.
Input:

Output:

7. WAP in Python to create a binary file with name and rollnumber of the
students. Search for given roll number and a display the name of
student

Input:
Output:

8. WAP in Python to create a binary file with rollno, name and marks of
the students and update the marks of specific student

Input:
Output:
9. WAP in Python to create a binary file with eid,ename and
salary of the employees and update the salary of specific
employee.

Input:

Output:
10. WAP in Python to create a text file and remove the lines whichcontains
the letter ‘K’.

Input:

Output:
11. Create a binary file with 10 randomnumbers from 1
to 40 and print those numbers. .

Input:

Output:
12. Write a program in Python to create a CSV file with thedetails of
5 students (Roll no, Name, Marks).
Input:

Output:
13. WAP in python to read acsv file.
Input:

Output:

14. Write a menu driven program which insert, delete and displaythe details
of an employee such as eid, ename and salary using Stack.

Input:
Output:
15. Write a menu driven program which insert, delete and display.The details
of a book such as book id, book name and price using stack

Input:
Output:
16. Write a menu driven program which insert, deleteand display the
details of a student such as roll no, sname and course using stack.

Input:
Output:

17. Write a menu driven program which insertdelete and


display the details of a movie as mid, mname and rating
using stack.

Input:
Output:

18. Write a menu driven program which insert, deleteand


display the details of a product such as pid, pname and
price using Stack
Input:
Output:

19. Write a menu driven program whichinsert delete


and display the details of a Club with cid, cname
and city using stack.
Input:
Output:

20. Write a menu driven program to demonstrate add, display,update, delete


and exit. Performed on a table Book containing (id, name, price) through
python-MYSQL connectivity.
Input:
import mysql.connector

# Establishing the connection con =


mysql.connector.connect(
host="localhost", username="root",
passwd="root"
)

# Creating a cursor object mycursor =


con.cursor()

# Creating and using the database


mycursor.execute("CREATE DATABASE IF NOT EXISTS library") mycursor.execute("USE library")

# Creating the `book` table if it doesn't exist


mycursor.execute("""
CREATE TABLE IF NOT EXISTS book (
bid INT PRIMARY KEY, bname
VARCHAR(100),
bprice FLOAT(10,2)
)
""")

# Menu-driven program while


True:
print("\nMenu:") print("1. Add
New Book") print("2. Display Books")
print("3. Update Book Details")
print("4. Delete Book")
print("5. Exit")

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

if choice == 1: #
Add a new book
bid = int(input("Enter Book ID: ")) bname =
input("Enter Book Name: ") bprice =
float(input("Enter Book Price: "))
mycursor.execute("INSERT INTO book VALUES (%s, %s, %s)", (bid, bname, bprice))
con.commit()
print("Book added successfully.")

elif choice == 2: #
Display all books
mycursor.execute("SELECT * FROM book")
books = mycursor.fetchall() if
books:
print("\nBooks:")
for book in books:
print(f"ID: ,book*0+-, Name: ,book*1+-, Price: ,book*2+-") else:
print("No books found.")
elif choice == 3: #
Update book details
bid = int(input("Enter Book ID to update: ")) bname
= input("Enter new Book Name: ") bprice =
float(input("Enter new Book Price: "))
mycursor.execute(
"UPDATE book SET bname = %s, bprice = %s WHERE bid = %s",
(bname, bprice, bid)
)
con.commit() if
mycursor.rowcount > 0:
print("Book updated successfully.") else:
print("Book ID not found.")

elif choice == 4:
# Delete a book
bid = int(input("Enter Book ID to delete: "))
mycursor.execute("DELETE FROM book WHERE bid = %s", (bid,))
con.commit() if mycursor.rowcount > 0:
print("Book deleted successfully.") else:
print("Book ID not found.")

elif choice == 5: #
Exit the program
print("Exiting...")
break

else: print("Invalid choice! Please try


again.")

# Closing the connection


mycursor.close() con.close()

Output:
21. Write a menu driven program to demonstrate add,
display, update, delete and exit. Performed
on a table product containing (id, name, price) through
python-MYSQL connectivity.

Input :
import mysql.connector

def connect_to_database(): try:


connection = mysql.connector.connect(
host="localhost", user="your_username",
password="your_password",
database="your_database"
)
if connection.is_connected():
print("Connected to MySQL database.")
return connection except mysql.connector.Error as
e:
print(f"Error connecting to MySQL: ,e-")
exit()

def add_product(connection): try:


cursor = connection.cursor() id =
int(input("Enter Product ID: ")) name =
input("Enter Product Name: ") price =
float(input("Enter Product Price: "))
query = "INSERT INTO product (id, name, price) VALUES (%s, %s, %s)" values
= (id, name, price) cursor.execute(query, values) connection.commit()
print("Product added successfully.")
except Exception as e: print(f"Error: ,e-")

def display_products(connection): try:

cursor = connection.cursor() id =
int(input("Enter Produ cursor = connection.cursor()
id = int(input("Enter Product ID to update: ")) name
= input("Enter new Product Name: ")
price = float(input("Enter new Product Price: ")) query = "UPDATE
product SET name=%s, price=%s WHERE id=%s" values = (name, price, id)
cursor.execute(query, values) connection.commit() if cursor.rowcount >
0:
print("Product updated successfully.") else:
print("Product not found.") except
Exception as e: print(f"Error: ,e-")

def delete_product(connection): try:


cursor = connection.cursor() id =
int(input("Enter Product ID to delete: "))
query = "DELETE FROM product WHERE id=%s"
cursor.execute(query, (id,))
connection.commit() if
cursor.rowcount > 0:
print("Product deleted successfully.") else:
print("Product not found.") except
Exception as e: print(f"Error: ,e-")

def main():
connection = connect_to_database() while
True:
print("\nMenu:") print("1.
Add Product") print("2. Display
Products") print("3. Update
Product") print("4. Delete
Product")
print("5. Exit")
choice = input("Enter your choice: ") if
choice == '1': add_product(connection)
elif choice == '2':
display_products(connection) elif choice
== '3': update_product(connection)
elif choice == '4':
delete_product(connection) elif choice ==
'5':
print("Exiting the program. Goodbye!")
connection.close()
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

output :
22. Write a menu driven program to demonstrate add,
display, update, delete and exit, performed on a table
club containing (id, name, city) through python-MYSQL
connectivity.

Input :
import mysql.connector

def connect_to_database(): try:


connection = mysql.connector.connect(
host="localhost", user="your_username",
password="your_password",
database="your_database"
)
if connection.is_connected():
print("Connected to MySQL database.")
return connection except mysql.connector.Error as
e:
print(f"Error connecting to MySQL: ,e-")
exit()

def add_club(connection): try:


cursor = connection.cursor() id =
int(input("Enter Club ID: ")) name =
input("Enter Club Name: ") city =
input("Enter Club City: ")
query = "INSERT INTO club (id, name, city) VALUES (%s, %s, %s)"
values = (id, name, city) cursor.execute(query, values)
connection.commit() print("Club added successfully.") except Exception
as e: print(f"Error: ,e-")

def display_clubs(connection): try:


cursor = connection.cursor()
query = "SELECT * FROM club"
cursor.execute(query) results =
cursor.fetchall() if results:
print("\nClub Table:")
print(f",'ID':<10-,'Name':<20-,'City':<20-")
print("-" * 50)
for row in results:
print(f",row*0+:<10-,row*1+:<20-,row*2+:<20-") else:
print("No clubs found.") except
Exception as e: print(f"Error: ,e-")
def update_club(connection): try:
cursor = connection.cursor() id =
int(input("Enter Club ID to update: ")) name =
input("Enter new Club Name: ")
city = input("Enter new Club City: ")
query = "UPDATE club SET name=%s, city=%s WHERE id=%s"
values = (name, city, id) cursor.execute(query, values)
connection.commit() if cursor.rowcount > 0:
print("Club updated successfully.") else:
print("Club not found.") except
Exception as e: print(f"Error: ,e-")

def delete_club(connection): try:


cursor = connection.cursor() id =
int(input("Enter Club ID to delete: ")) query =
"DELETE FROM club WHERE id=%s"
cursor.execute(query, (id,))
connection.commit() if
cursor.rowcount > 0:
print("Club deleted successfully.") else:
print("Club not found.") except
Exception as e: print(f"Error: ,e-")

def main():
connection = connect_to_database() while
True:
print("\nMenu:")
print("1. Add Club")
print("2. Display Clubs")
print("3. Update Club")
print("4. Delete Club")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == '1': add_club(connection)
elif choice == '2':
display_clubs(connection)
elif choice == '3':
update_club(connection) elif
choice == '4':
delete_club(connection) elif
choice == '5':
print("Exiting the program. Goodbye!")
connection.close()
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

Output :
23. Write a menu driven program to demonstrate add, display,
update, delete and exit,
performed on a Student table containing (id,
name, course) through python-MYSQL connectivity
Input :
import mysql.connector

# Connect to MySQL def connect_db(): return mysql.connector.connect(host="localhost", user="root", password="",


database="school")

# Add a student def


add_student(conn):
id, name, course = input("Enter ID, Name, Course: ").split(", ")
conn.cursor().execute("INSERT INTO Student (id, name, course) VALUES (%s, %s, %s)", (id, name, course)) conn.commit()
print("Student added.")

# Display students def


display_students(conn): cursor
= conn.cursor()
cursor.execute("SELECT * FROM Student") for
row in cursor.fetchall(): print(row)

# Update a student def


update_student(conn):
id = input("Enter ID to update: ")
name, course = input("Enter new Name, Course: ").split(", ")
conn.cursor().execute("UPDATE Student SET name=%s, course=%s WHERE id=%s", (name, course, id)) conn.commit()
print("Student updated.")

# Delete a student def


delete_student(conn):
id = input("Enter ID to delete: ")
conn.cursor().execute("DELETE FROM Student WHERE id=%s", (id,))
conn.commit() print("Student deleted.")

# Menu
def menu():
conn = connect_db() while
True:
choice = input("1. Add 2. Display 3. Update 4. Delete 5. Exit: ") if
choice == "1":
add_student(conn) elif
choice == "2":
display_students(conn) elif
choice == "3":
update_student(conn) elif
choice == "4":
delete_student(conn) elif
choice == "5": conn.close()
print("Goodbye!")
break
else:
print("Invalid choice!")

menu()

Output :
24. create a menu driven program to demonstrate add, display,
update, delete and exit.
Performed on a table movie containing (id, name, rating)
through python-MYSQL connectivity.
Input :
import mysql.connector

# Connect to MySQL def


connect_db():
return mysql.connector.connect(host="localhost", user="root", password="", database="movies_db")

def add_movie(conn):
id, name, rating = input("Enter ID, Name, Rating: ").split(", ")
conn.cursor().execute("INSERT INTO movie (id, name, rating) VALUES (%s, %s, %s)",
(id, name, rating)) conn.commit()

def display_movies(conn): cursor =


conn.cursor()
cursor.execute("SELECT * FROM movie") for
row in cursor.fetchall(): print(row)

def update_movie(conn):
id, name, rating = input("Enter ID, New Name, New Rating: ").split(", ")
conn.cursor().execute("UPDATE movie SET name=%s, rating=%s WHERE id=%s",
(name, rating, id)) conn.commit()

def delete_movie(conn):
id = input("Enter ID to delete: ")
conn.cursor().execute("DELETE FROM movie WHERE id=%s", (id,)) conn.commit()

def menu():
conn = connect_db() while
True:
choice = input("\n1.Add 2.Display 3.Update 4.Delete 5.Exit: ") if
choice == "1": add_movie(conn) elif choice == "2":
display_movies(conn) elif choice == "3": update_movie(conn) elif
choice == "4": delete_movie(conn) elif choice == "5": conn.close();
break

menu()

Output :
25. Write the SQL Query for full outer join of two
tables Emp
(id, name, salary, dept_id) and
Dept(dept_id, name)

Input :

SELECT e.id, e.name AS emp_name, e.salary, e.dept_id, d.name


AS dept_name
FROM Emp e
LEFT JOIN Dept d ON e.dept_id = d.dept_id
UNION
SELECT e.id, e.name AS emp_name, e.salary, e.dept_id, d.name
AS dept_name
FROM Emp e
RIGHT JOIN Dept d ON e.dept_id = d.dept_id;
26. Write the SQL query to find out thesquare root of 26

27. Shiva is using a table with the following details:Students(Name, Class, Stream_id,
Stream_Name)

Write the SQL query to display the names of students who have not been assigned any stream or have been
assigned Stream_Name that end with "computers"
30. Shiva, a student of class XII, created a table "CLASS". Grade is one of the columnsof
this table. Write the SQL query to find the details of students whose grade have not
been entered.

31. Shiva is using a table employee. It hasfollowing details: Employee(Code, Name, Salary,
DeptCode). Write the SQL queryto display maximum salary department wise.
32. Write the SQL Query to display the differenceofhighest and

lowest salary of each department having maximum salary

greater than4000
33. Write the SQL Query to increase 20% salary ofthe employee
whoseexperience is more than 5 year of the table Emp(id, name,
salary, exp)
34. Write the SQL Query for inner join of two tables Emp(eid,ename, salary,
dept_id) and Dept(dept_id, dname)

35. Write the SQL Query for full outer join of two tables Emp(eid,ename,
salary,dept_id) and Dept(dept_id, dname)

36. Write the SQL Query for full outer join of two tables Emp(eid, ename,salary,dept_id)
and Dept(dept_id, dname)
37. Display 4 characters extracted from 5th right characteronwards
from string 'ABCDEFG'.

38. Write a query to create a string fromthe


ASCII values 65, 67.3, '68.3'
39. Display names 'MR. MODI' and 'MR.Sharma' into
lowercase

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