Raghavcspractical
Raghavcspractical
output:
Number of words: 1
output:
Enter text to write to file: abcdefg
output:
Longest line: abcdedg
binary files programs:
filename = "project.dat"
numbers = [1, 2, 3, 4, 5]
file = open(filename, 'wb')
pickle.dump(numbers, file)
print("successfully added data.")
file.close()
output:
successfully added data.
filename = "project.dat"
file = open(filename, 'rb')
numbers = pickle.load(file)
print("Sum of numbers:", sum(numbers))
file.close()
output:
Sum of numbers: 15
import pickle
filename = "project.dat"
file = open(filename, 'rb')
numbers = pickle.load(file)
file.close()
new_numbers = [6, 7, 8]
numbers.extend(new_numbers)
file = open(filename, 'wb')
pickle.dump(numbers, file)
print("successfully appended data.")
file.close()
output:
successfully appended data.
CSV files programs:
filename = "project.csv"
file = open(filename, 'r')
reader = csv.reader(file)
for row in reader:
print(row)
file.close()
output:
['Name\tClass']
['Raghav\tXII-A']
['Shikhar\tXII-A']
['Aarab\tXII-A']
import csv
filename = "project.csv"
data = [{"Name": "Raghav", "Age": 16}, {"Name": "Vinay", "Age": 15}]
file = open(filename, 'w', newline='')
writer = csv.DictWriter(file, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
print("successfully added data.")
file.close()
output:
Sum of numbers: 15
import csv
output:
successfully updated.
stack programs:
Q1 Stack implementation using a list.
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
print("Popped item:", stack.pop())
output:
Popped item: 3
string = "hello"
stack = []
for char in string:
stack.append(char)
reversed_string = ""
while stack:
reversed_string += stack.pop()
print("Reversed string:", reversed_string)
output:
Reversed string: olleh
stack = []
def push(item):
stack.append(item)
print(f"Pushed {item} onto the stack.")
push(10)
push(20)
push(30)
print("Stack:", stack)
output:
Pushed 10 onto the stack.
Pushed 20 onto the stack.
Pushed 30 onto the stack.
Stack: [10, 20, 30]
list programs:
Q1 find largest number in list.
numbers = [3, 1, 7, 5, 9, 2]
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
print("The largest number is:", largest)
output:
The largest number is: 9
numbers = [5, 8, 2, 9, 1, 6]
smallest = numbers[0]
for num in numbers:
if num < smallest:
smallest = num
print("The smallest number is:", smallest)
output:
The smallest number is: 1
numbers = [1, 2, 3, 2, 4, 1, 5]
unique_numbers = []
for num in numbers:
if num not in unique_numbers:
unique_numbers.append(num)
print("List without duplicates:", unique_numbers)
output:
List without duplicates: [1, 2, 3, 4, 5]
tuple programs:
output:
Index of 30 is: 2
my_tuple = (1, 2, 3, 1, 4, 2, 5)
count_dict = {}
for item in my_tuple:
if item in count_dict:
count_dict[item] += 1
else:
count_dict[item] = 1
print("Element counts:", count_dict)
output:
Element counts: {1: 2, 2: 2, 3: 1, 4: 1, 5: 1}
output:
30 is in the tuple.
dictionary programs:
output:
Union of dict1 and dict2: {'a': 1, 'b': 3, 'c': 4}
output:
Enter the dictionary: {'c':10, 'f':87, 'r':23, 'a':5}
Sorted keys: ['a', 'c', 'f', 'r']
output:
Word frequency: {'hello': 2, 'world': 1}
python-SQL connectivity programs.
import mysql.connector
output:
table successfully created.
import mysql.connector
output:
records successfully added.
import mysql.connector
output:
(1, 'Alice')
(2, 'Bob')
SQL queries:
single table:
STUDENT TABLE
+--------+--------------+----------------+------------------+----------------+-----------+
| rollno | stname | clas | section | sub1 | sub2 | sub3 |
+--------+--------------+----------------+------------------+----------------+-----------+
| 104 | Aarav Sharma | 12 | A | Maths | Physics | Chemistry |
| 105 | Isha Kapoor | 11 | B | English | Biology | History |
| 106 | Vihaan Gupta | 10 | A | Computer Science | Maths | English |
| 107 | Ananya Reddy | 9 | C | Hindi | Geography | Maths |
| 108 | Madhav Joshi | 8 | D | Science | Social Studies | Maths |
+--------+--------------+------+---------+-----------------------------------+-----------+
output:
+--------+--------------+------+---------+-------+-----------+-----------+
| rollno | stname | clas | section | sub1 | sub2 | sub3 |
+--------+--------------+------+---------+-------+-----------+-----------+
| 104 | Aarav Sharma | 12 | A | Maths | Physics | Chemistry |
| 107 | Ananya Reddy | 9 | C | Hindi | Geography | Maths |
+--------+--------------+------+---------+-------+-----------+-----------+
output:
+--------+--------------+------+---------+------------------+----------------+-----------+
| rollno | stname | clas | section | sub1 | sub2 | sub3 |
+--------+--------------+------+---------+------------------+----------------+-----------+
| 104 | Aarav Sharma | 12 | A | Maths | Physics | Chemistry |
| 105 | Isha Kapoor | 11 | B | English | Biology | History |
| 106 | Vihaan Gupta | 10 | A | Computer Science | Maths | English |
| 107 | Ananya Reddy | 9 | C | Hindi | Geography | Maths |
| 108 | Madhav Joshi | 8 | D | Science | Social Studies | Maths |
| 109 | Raghav Singh | 12 | A | Maths | Physics | Python |
+--------+--------------+------+---------+------------------+----------------+-----------+
two tables:
academic_info table:
+------------+-------+---------+------------+----------+------------------+
| student_id | class | section | subject1 | subject2 | subject3 |
+------------+-------+---------+------------+----------+------------------+
| 1 | 10 | A | Math | Science | History |
| 2 | 12 | B | English | Physics | Chemistry |
| 3 | 11 | C | Biology | Math | Geography |
| 4 | 9 | A | History | Math | Computer Science |
| 5 | 10 | B | Literature | Biology | Math |
+------------+-------+---------+------------+----------+------------------+
student_details table
+------------+---------------+--------------------------+----------------+
| student_id | name | address | contact_number |
+------------+---------------+--------------------------+----------------+
| 1 | John Doe | 123 Main St, New York | 555-1234 |
| 2 | Jane Smith | 456 Oak Ave, Los Angeles | 555-5678 |
| 3 | Alice Brown | 789 Pine Rd, Chicago | 555-8765 |
| 4 | Bob Johnson | 321 Elm St, Miami | 555-4321 |
| 5 | Charlie Davis | 654 Cedar Blvd, Houston | 555-1111 |
+------------+---------------+--------------------------+----------------+
output:
+------------+----------+---------+------------+------------+-----------+----------+
| booking_id | guest_id | room_id | status | first_name | last_name | phone |
+------------+----------+---------+------------+------------+-----------+----------+
| 3 | 1 | 1 | checked_in | John | Doe | 555-1234 |
+------------+----------+---------+------------+------------+-----------+----------+
output:
+---------------+-------+---------+----------------+
| name | class | section | contact_number |
+---------------+-------+---------+----------------+
| John Doe | 10 | A | 555-1234 |
| Alice Brown | 11 | C | 555-8765 |
| Bob Johnson | 9 | A | 555-4321 |
| Charlie Davis | 10 | B | 555-1111 |
+---------------+-------+---------+----------------+
output:
+-------------+-------+---------+----------------+
| name | class | section | contact_number |
+-------------+-------+---------+----------------+
| Bob Johnson | 9 | A | 555-4321 |
+-------------+-------+---------+----------------+