0% found this document useful (0 votes)
11 views8 pages

Python Lab Activities-Combined

Python activities

Uploaded by

surendragos27
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)
11 views8 pages

Python Lab Activities-Combined

Python activities

Uploaded by

surendragos27
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/ 8

Question :-3

Python program to apply all method of list.


Solution:-
list =[5, 8, 12, 18, 90, 512, 88, 61, 88, 96, 100, 0, 68]
list.append(2)
print(list)

list.extend({7,52,4 1})
print(list)

list.insert(0,100)
print(list)

list.remove(8)
print(list)

list.pop(7)
print(list)
list = [5, 8, 12, 18, 90, 512, 88, 61, 88, 96, 100, 0, 68]

print(list.count(88))

print(list.index(5))

list.sort()
print(list)

list.sort(reverse=True)
print(list)

list.reverse()
print(list)
Question :-4
Write a python program that finds the factorial of a given number.
Solution:-

num = int(input("Enter any number: "))


fact = 1
if num < 0:
print("Factorial doesn't exist")
elif num == 0:
print("Factorial of 0 is 1")
else:
for i in range(1, num + 1):
fact = fact * i
print("Factorial of", num, "is", fact)
Question :-5
Write a python program to find maximum and minimum elements in
the list without using built-in max() and min() function.
Solution:-

list = [151, 182, 2, 5, 9, 55, 88, 512]


mini = list[0]
maxi = list[0]
for i in list:
if i > maxi:
maxi = i
for i in list:
if i < mini:
mini = i
print("The highest element is:", maxi)
print("The smallest element is:", mini)
Question:-6

Write a Python program that uses a lambda function to filter even numbers from a list of integers.

solution:-

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

print("Even numbers:", even_numbers)


Question:-7

Write a Python program that creates a class called Student with attributes like name, age, and grade.
Create objects of this class and display their details.

Solution:-

class Student:

def __init__(self, name, age, grade):

self.name = name

self.age = age

self.grade = grade

def display_details(self):

print(f"Name: {self.name}")

print(f"Age: {self.age}")

print(f"Grade: {self.grade}")

print('-' * 30)

student1 = Student("Alice", 20, "A")

student2 = Student("Bob", 22, "B+")

student3 = Student("Charlie", 21, "A-")

student1.display_details()

student2.display_details()

student3.display_details()
Question:- 8

Write a Python program that handles a division by zero exception and always executes a cleanup
message in the finally block.

Solution:-

try:

num1 = float(input("Enter the numerator: "))

num2 = float(input("Enter the denominator: "))

result = num1 / num2

print(f"The result of {num1} divided by {num2} is: {result}")

except ZeroDivisionError:

print("Error: Division by zero is not allowed!")

finally:

print("This is the cleanup message. Execution complete.")


Question:- 9

Write a Python program to read content from a text file, modify the content (convert to uppercase),
and write it back to a new file.

Solution:-

input_filename = 'input.txt'

output_filename = 'output.txt'

try:

with open(input_filename, 'r') as infile:

content = infile.read()

modified_content = content.upper()

with open(output_filename, 'w') as outfile:

outfile.write(modified_content)

print(f"Content successfully modified and written to {output_filename}")

except FileNotFoundError:

print(f"Error: The file '{input_filename}' was not found.")

except Exception as e:

print(f"An error occurred: {e}")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy