[go: up one dir, main page]

0% found this document useful (0 votes)
3 views6 pages

Computer_Science_Practical_File (2) (1)

Class 12 Computer science project

Uploaded by

hzz124421
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)
3 views6 pages

Computer_Science_Practical_File (2) (1)

Class 12 Computer science project

Uploaded by

hzz124421
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
You are on page 1/ 6

Computer Science Practical File

EXCEPTION HANDLING

1. Division by Zero
Program:

try:
a = int(input("Enter numerator: "))
b = int(input("Enter denominator: "))
print("Result:", a / b)
except ZeroDivisionError:
print("Error: Division by zero.")
Output:

Enter numerator: 10
Enter denominator: 0
Error: Division by zero.

2. Index Error
Program:

try:
lst = [1, 2, 3]
print(lst[5])
except IndexError:
print("Index out of range!")
Output:

Index out of range!

3. Value Error
Program:

try:
x = int(input("Enter an integer: "))
except ValueError:
print("Invalid input! Not an integer.")
Output:

Enter an integer: abc


Invalid input! Not an integer.

4. Multiple Exceptions
Program:

try:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print("Result:", a / b)
except ZeroDivisionError:
print("Cannot divide by zero.")
except ValueError:
print("Invalid input.")
Output:

Enter a: ten
Invalid input.

5. Finally Block
Program:

try:
num = int(input("Enter a number: "))
print("Square:", num * num)
finally:
print("This block always runs.")
Output:

Enter a number: 5
Square: 25
This block always runs.

DATA FILE HANDLING

1. Write to a Text File


Program:

with open("sample.txt", "w") as f:


f.write("Hello, World!")
Output:

File Output (sample.txt):


Hello, World!

2. Read from a Text File


Program:

with open("sample.txt", "r") as f:


content = f.read()
print(content)
Output:

Hello, World!

3. Append to a File
Program:

with open("sample.txt", "a") as f:


f.write("\nWelcome to Python.")
Output:

File Output (sample.txt):


Hello, World!
Welcome to Python.

4. Count Lines in File


Program:

with open("sample.txt", "r") as f:


lines = f.readlines()
print("Total lines:", len(lines))
Output:

Total lines: 2

5. Search a Word in File


Program:

word = "Python"
with open("sample.txt", "r") as f:
found = any(word in line for line in f)
print("Found" if found else "Not Found")
Output:

Found

STACK

1. Stack using List


Program:

stack = []
stack.append(10)
stack.append(20)
print("Stack:", stack)
print("Popped:", stack.pop())
Output:

Stack: [10, 20]


Popped: 20

2. Stack with Functions


Program:

def push(stack, item):


stack.append(item)

def pop(stack):
return stack.pop()
stack = []
push(stack, 1)
push(stack, 2)
print("Stack:", stack)
print("Popped:", pop(stack))
Output:

Stack: [1, 2]
Popped: 2

3. Stack Overflow (limit 3)


Program:

stack = []
limit = 3
for i in range(5):
if len(stack) < limit:
stack.append(i)
else:
print("Stack Overflow")
Output:

Stack Overflow
Stack Overflow

SORTING

1. Bubble Sort
Program:

arr = [5, 2, 9, 1]
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print("Sorted array:", arr)
Output:

Sorted array: [1, 2, 5, 9]

2. Insertion Sort
Program:

arr = [4, 3, 2, 10]


for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
print("Sorted:", arr)
Output:

Sorted: [2, 3, 4, 10]

3. Selection Sort
Program:

arr = [64, 25, 12, 22, 11]


for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
print("Sorted:", arr)
Output:

Sorted: [11, 12, 22, 25, 64]

4. Sort Strings Alphabetically


Program:

names = ["Zara", "Liam", "Emma", "Olivia"]


names.sort()
print("Sorted:", names)
Output:

Sorted: ['Emma', 'Liam', 'Olivia', 'Zara']

5. Merge Two Sorted Lists


Program:

a = [1, 3, 5]
b = [2, 4, 6]
merged = sorted(a + b)
print("Merged:", merged)
Output:

Merged: [1, 2, 3, 4, 5, 6]

6. Count Sort (Simple Version)


Program:

arr = [1, 4, 1, 2, 7, 5, 2]
max_val = max(arr)
count = [0] * (max_val + 1)
for num in arr:
count[num] += 1
sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])
print("Sorted:", sorted_arr)
Output:

Sorted: [1, 1, 2, 2, 4, 5, 7]

7. Sort Dictionary by Value


Program:

d = {'a': 3, 'b': 1, 'c': 2}


sorted_d = dict(sorted(d.items(), key=lambda item: item[1]))
print("Sorted Dict:", sorted_d)
Output:

Sorted Dict: {'b': 1, 'c': 2, 'a': 3}

You might also like