Python
Python
CODING:
Simple Calculator
Select operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter choice (1/2/3/4): 3
Enter first number: 4
Enter second number: 5
4.0 * 5.0 = 20.0
2. PROGRAM USING CONTROL FLOW TOOLS
(PROGRAM TO FIND THE FACTORIAL OF A
GIVEN NUMBER)
CODING:
Enter a number: -5
-5.0 is a negative number.
3. WRITE A PROGRAM TO USE FOR LOOP
CODING:
Stack.py
# Python code to demonstrate Implementing
# stack using list
stack = ["SRI", "VIDYA", "KAMACHI"]
print(stack)
stack.append("ARTS")
stack.append("SCIENCE")
print("After Inserting elements the Stack is:")
print(stack)
print("Stack POP operations:")
# Removes the last item
stack.pop()
print("After Removing last element the Stack is:")
print(stack)
# Removes the last item
stack.pop()
print("After Removing last element the Stack is:")
print(stack)
OUTPUT:
Coding:
Queue.py
# Python code to demonstrate Implementing
# Queue using list
queue = ["SRI", "VIDYA", "KAMACHI"]
queue.append("ARTS")
queue.append("SCIENCE")
print("Queue elements are:")
print(queue)
print("Deleted elements are:")
# Removes the first item
print(queue.pop(0))
print(queue)
# Removes the first item
print(queue.pop(0))
print("Queue elements are:")
print(queue)
OUTPUT:
CODING:
1
2
3
4
5
Element at index 2: 3
5. CREATE NEW MODULE FOR
MATHEMATICAL OPERATIONS AND
USE IN YOUR PROGRAM
CODING:
# math_operations.py
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y != 0:
return x / y
else:
return "Cannot divide by zero."
# Program using the mathematical operations module
# Import the mathematical operations module
import math_operations as math_ops 12
# Input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Perform mathematical operations
sum_result = math_ops.add(num1, num2)
difference_result = math_ops.subtract(num1, num2)
product_result = math_ops.multiply(num1, num2)
division_result = math_ops.divide(num1, num2)
# Output
print(f"Sum: {sum_result}")
print(f"Difference: {difference_result}")
print(f"Product: {product_result}")
print(f"Division: {division_result}")
SAMPLE INPUT / OUTPUT:
CODING:
Import os
def read_file():
filename = input("Enter the name of the file to read: ")
try:
with open(filename, 'r') as file: 14
content = file.read()
print("\nFile content:\n", content)
except FileNotFoundError:
print(f"File '{filename}' not found.")
def write_to_file():
filename = input("Enter the name of the file to write: ")
content = input("Enter the content to write to the file: ")
with open(filename, 'w') as file:
file.write(content)
print(f"Content successfully written to '{filename}'.")
def create_directory():
dirname = input("Enter the name of the directory to
create: ")
try:
os.mkdir(dirname)
print(f"Directory '{dirname}' created successfully.")
except FileExistsError:
print(f"Directory '{dirname}' already exists.")
def delete_directory():
dirname = input("Enter the name of the directory to
delete: ")
try:
os.rmdir(dirname)
print(f"Directory '{dirname}' deleted successfully.")
except FileNotFoundError:
print(f"Directory '{dirname}' not found.")
except OSError as e:
print(f Error deleting directories’{dirname}’:{e}”)
# Sample Input & Output
read_file()
write_to_file()
create_directory()
delete_directory()
SAMPLE INPUT / OUTPUT:
CODING:
def perform_division():
try:
# Input
numerator = float(input("Enter the numerator: "))
denominator = float(input("Enter the denominator: "))
# Division Operation
result = numerator / denominator
# Output
print(f"Result of {numerator} / {denominator} =
{result}")
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
RESTART: C:\Python27\Myaddr.py
1. Add Record
2. Modify Record
3. Delete Record
4. List Record
5. Exit
Enter the Option :1
Enter SSN number:001
Enter a Name:nithi
Enter the Address:mecheri
Enter the City: salem
Enter the State:tamilnadu
Enter the Postcode:636453
Enter the country:India
Add Another Record y/n
Enter the option:y
Enter SSN number:002
Enter a Name:rathi
Enter the Address:mettur
Enter the City: salem
Enter the State:tamilnadu
Enter the Postcode:636453
Enter the country:India
Add Another Record y/n
Enter the option:n
10. PROGRAM USING STRING HANDLING AND
REGULAR EXPRESSIONS
CODING:
import re
def validate_and_extract_email(input_email):
# Regular expression pattern for a simple email
validation
email_pattern = r'^\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-
]+\.[A-Z|a-z]{2,}\b'
# Check if the input email matches the pattern
match_result = re.match(email_pattern, input_email) 24
if match_result:
print(f"The email '{input_email}' is valid.")
# Extract information from the email using regular
expressions
extracted_info = re.findall(r'([A-Za-z0-9._%+-]+)@([A-
Za-z0-9.-]+)\.([A-Z|a-z]{2,})', input_email)
# Display extracted information
print("Username:", extracted_info[0][0])
print("Domain:", extracted_info[0][1])
print("Top-level Domain:", extracted_info[0][2])
else:
print(f"The email '{input_email}' is not valid.")
# Sample Input & Output
input_email = input("Enter an email address: ")
validate_and_extract_email(input_email)
SAMPLE INPUT/OUTPUT: