[go: up one dir, main page]

0% found this document useful (0 votes)
2 views10 pages

Ujwal Ass2

Antivirus and rogue antivirus are quite different in purpose and functionality: 1. Antivirus: This is legitimate software designed to detect, prevent, and remove malware from computers and devices. It provides real-time protection, regularly updates its virus definitions, and scans for threats to keep systems secure. 2. Rogue Antivirus: This is a type of malicious software that pretends to be legitimate antivirus software. It often tricks users into downloading it by displaying fake alerts abo

Uploaded by

520 Rakesh
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)
2 views10 pages

Ujwal Ass2

Antivirus and rogue antivirus are quite different in purpose and functionality: 1. Antivirus: This is legitimate software designed to detect, prevent, and remove malware from computers and devices. It provides real-time protection, regularly updates its virus definitions, and scans for threats to keep systems secure. 2. Rogue Antivirus: This is a type of malicious software that pretends to be legitimate antivirus software. It often tricks users into downloading it by displaying fake alerts abo

Uploaded by

520 Rakesh
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/ 10

PYTHON

ASSIGNMENT 2
Ujwal.C
1602-23-744-009
ES-VLSID

1. Write a function that demonstrates the use of multiple except blocks by handling
both ValueError and TypeError.
Prog:
def handle_errors(x, y):
try:
result = x / y
print(result)
except ValueError:
print("ValueError: Invalid input")
except TypeError:
print("TypeError: Input must be a number")
except Exception as e:
print(f"Unexpected error: {e}")

# Test the function


handle_errors(10, 2) # Normal case
handle_errors("a", 2) # ValueError
handle_errors(10, "b") # TypeError
handle_errors(10, 0) # ZeroDivisionError (caught by the general Exception block)

2. User-Defined Create a user-defined exception InvalidAgeError and write a


program that raises this exception if the age is not within a specified range.
Prog:
class InvalidAgeError(Exception):
pass

def check_age(age):
if not (18 <= age <= 65):
raise InvalidAgeError("Age must be between 18 and 65")
else:
print("Age is valid")

# Test the function


try:
age = int(input("Enter your age: "))
check_age(age)
except InvalidAgeError as e:
print(e)
3. Write a program that reads a file and handles any file not found errors using try- except blocks.
Prog:
try:
# Attempt to open and read the file
with open('example.txt', 'r') as file:
content = file.read()
print(content)
except FileNotFoundError:
# Handle the file not found error
print("Error: File not found")
except Exception as e:
# Handle any other exceptions
print(f"An error occurred: {e}")

4. Write a program that demonstrates the use of try-except blocks by handling


division by zero errors.
Prog:
def divide_numbers(num1, num2):
try:
result = num1 / num2
print(f"The result is: {result}")
except ZeroDivisionError:
print("Error: Division by zero is not allowed")
except TypeError:
print("Error: Both inputs must be numbers")
except Exception as e:
print(f"An unexpected error occurred: {e}")

# Test the function


divide_numbers(10, 2) # Normal case
divide_numbers(10, 0) # Division by zero
divide_numbers("a", 2) # TypeError

5. Create a program that writes a list of dictionaries (each representing a student


with name, age, grade) to a file in JSON format.
Prog:
import json

# List of student dictionaries


students = [
{"name": "John", "age": 18, "grade": 90},
{"name": "Jane", "age": 19, "grade": 85},
{"name": "Bob", "age": 20, "grade": 92}
]

# Open the file in write mode


with open("students.json", "w") as file:
# Dump the list of dictionaries to the file in JSON format
json.dump(students, file, indent=4)

print("Data written to students.json")

6. Write a program that takes a list of tuples (name, score) and returns a dictionary
with names as keys and scores as values.
Prog:
def tuple_list_to_dict(tuple_list):
return dict(tuple_list)

tuples = [("John", 90), ("Jane", 85), ("Bob", 92)]


result_dict = tuple_list_to_dict(tuples)
print(result_dict)

7. Create a nested dictionary to store student information (name, age, grade) and
write a function to display the information.
Prog:
# Create a nested dictionary to store student information
students = {
"John": {"age": 18, "grade": 90},
"Jane": {"age": 19, "grade": 85},
"Bob": {"age": 20, "grade": 92}
}

# Function to display student information


def display_student_info(name):
student = students.get(name)
if student:
print(f"Name: {name}")
print(f"Age: {student['age']}")
print(f"Grade: {student['grade']}")
else:
print("Student not found")

# Test the function


display_student_info("John")
display_student_info("Jane")
display_student_info("Bob")
display_student_info("Alice") # Student not found
8. Create a function to perform union and difference operations on two sets and print
the results.
Prog:

def set_operations(set1, set2):


# Union
union = set1.union(set2)
print(f"Union: {union}")

# Difference
difference1 = set1.difference(set2)
difference2 = set2.difference(set1)
print(f"Difference (set1 - set2): {difference1}")
print(f"Difference (set2 - set1): {difference2}")

# Test the function


set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set_operations(set1, set2)

9. Write a program to demonstrate tuple assignment and swap two variables using
tuple assignment.
Prog:

# Tuple assignment
name, age = ("John", 25)
print(name) # Output: John
print(age) # Output: 25

# Swap two variables using tuple assignment


a = 10
b = 20
print("Before swap: a =", a, "b =", b)

# Swap a and b
a, b = b, a
print("After swap: a =", a, "b =", b)
# Output:
# Before swap: a = 10 b = 20
# After swap: a = 20 b = 10

10. Write a program that takes a list of numbers as input and returns a tuple
containing the minimum, maximum, and average of the number
prog:

def calculate_stats(numbers):
minimum = min(numbers)
maximum = max(numbers)
average = sum(numbers) / len(numbers)
return (minimum, maximum, average)

# Test the function


numbers = [1, 2, 3, 4, 5]
stats = calculate_stats(numbers)
print(stats)

# Output:
# (1, 5, 3.0)

You might also like