Cs Practical File
Cs Practical File
# Main Program
try:
# Taking input from the user
number = int(input("Enter a positive integer: "))
except ValueError:
print("Please enter a valid positive integer!")
Example Output (Palindrome):
print("\nStudent Data:")
for row in reader:
print(', '.join(row)) # Print each row in a readable format
except FileNotFoundError:
print("The file does not exist. Please create the CSV first!")
# Main Program
print("Choose an option:")
print("1. Create a CSV File")
print("2. Read the CSV File")
if choice == '1':
create_csv()
elif choice == '2':
read_csv()
else:
print("Invalid choice. Please enter 1 or 2.")
# Main Program
stack = Stack()
while True:
print("\nChoose an operation:")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display Stack")
print("5. Exit")
if choice == '1':
element = input("Enter the element to push: ")
stack.push(element)
elif choice == '2':
stack.pop()
elif choice == '3':
stack.peek()
elif choice == '4':
stack.display()
elif choice == '5':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice! Please enter a number between 1 and 5.")
Output:
Python Program: File Handling (Write, Read, Append)
# Function to write data to a file
def write_to_file():
with open("sample.txt", "w") as file:
file.write("Hello, this is the first line of the file.\n")
file.write("Python file handling example.\n")
print("Data written to 'sample.txt' successfully!")
if choice == '1':
write_to_file()
elif choice == '2':
read_file()
elif choice == '3':
append_to_file()
elif choice == '4':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice! Please enter a number between 1 and 4.")
Output Writing to a File:
def count_words_in_file():
try:
# Open the file in read mode
with open("sample.txt", "r") as file:
content = file.read()
words = content.split() # Splitting content into words using space as a separator
word_count = len(words)
print(f"\nThe file contains {word_count} words.")
except FileNotFoundError:
print("The file 'sample.txt' does not exist. Please create the file first!")
if choice == '1':
create_sample_file()
elif choice == '2':
count_words_in_file()
elif choice == '3':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice! Please enter a number between 1 and 3.")
Output: Create a File
# Main Program
while True:
print("\nString Operations Menu:")
print("1. Reverse a string")
print("2. Count vowels in a string")
print("3. Check if a string is a palindrome")
print("4. Exit")
if choice == '1':
reversed_str = reverse_string(user_string)
print(f"Reversed String: {reversed_str}")
Output:
Python Program: Dictionary Operations
# Function to display the dictionary
def display_dict(my_dict):
if my_dict:
print("Dictionary content:", my_dict)
else:
print("The dictionary is empty!")
while True:
print("\nDictionary Operations Menu:")
print("1. Add a key-value pair")
print("2. Remove a key")
print("3. Display dictionary")
print("4. Exit")
if choice == '1':
key = input("Enter the key: ")
value = input("Enter the value: ")
add_to_dict(my_dict, key, value)
elif choice == '2':
key = input("Enter the key to remove: ")
remove_from_dict(my_dict, key)
elif choice == '3':
display_dict(my_dict)
elif choice == '4':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice! Please enter a number between 1 and 4.")
Output
Python Program: Tuple Operations
# Tuple creation
my_tuple = (10, 20, 30, 40, 50)
Output
my_list.append(60)
my_list.remove(30)
count_20 = my_list.count(20)
index_40 = my_list.index(40)
Output:
import csv
Output
# Main Program
push(10)
push(20)
push(30)
display()
pop()
display()
pop()
display()
Output Example:
Python Program: Stack with Push Operation
# Stack implementation using a list
stack = [] # Empty stack
# Main Program
push(10)
push(20)
push(30)
display() # Displaying the stack after pushing elements
Output
def divide_numbers():
try:
# Taking user input
num1 = int(input("Enter the numerator: "))
num2 = int(input("Enter the denominator: "))
# Attempting division
result = num1 / num2
print(f"Result: {result}")
if num == target:
print(f"Target {target} found!")
break # Exits the loop when the target is found
else:
print(f"Target {target} not found in the list.")
# Main Program
numbers_list = [10, 20, 30, 40, 50]
target_number = int(input("Enter a number to search: "))
find_number(numbers_list, target_number)
Output
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
conn.close()
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
conn = sqlite3.connect('school.db')
cursor = conn.cursor()