Labmanual
Labmanual
student_dict = {}
for i in range(len(names)):
student_dict[names[i]] = roll_nos[i]
print(student_dict)
Output:
Q.2. Write a program to print the Fibonacci series using function.
def fibonacci_series(n):
a, b = 0, 1
print("Fibonacci Series:")
for _ in range(n):
a, b = b, a + b
fibonacci_series(num_terms)
Output:
Q.3. Create a module name ‘Calculate’ and add 4 methods in it
add(),substract(),multiply(), and divide(), create another module named as
‘operations’ and import ‘Calculate’ module in it and use its function.
calculate.py
return a + b
return a - b
return a * b
if b == 0:
return a / b
calculations.py
import calculate
def perform_operations():
a = 10
b=5
if __name__ == "__main__":
perform_operations()
Output:
Q.4. Write a program to demonstrate the use of Multilevel inheritance?
class Animal:
self.name = name
def speak(self):
class Dog(Animal):
def speak(self):
print(f"{self.name} barks")
class Labrador(Dog):
def color(self):
print(f"{self.name} is brown")
dog = Dog("Buddy")
labrador = Labrador("Max")
animal.speak()
dog.speak()
labrador.speak()
labrador.color()
Output:
Q.5 Write a program to validate strong password using regular expression ?
import re
def is_strong_password(password):
pattern = re.compile(r'''
(?=.*[a-z])
(?=.*[A-Z])
(?=.*\d)
(?=.*[@$!%*?&])
.{8,}
''', re.VERBOSE)
return bool(pattern.match(password))
if is_strong_password(password):
else:
def divide_numbers():
try:
except ZeroDivisionError:
except ValueError:
else:
print(f"Result: {result}")
finally:
print("Execution complete.")
divide_numbers()
Output:
Q.7. Write a program and create 2 threads in python , one should print square of
a numbers (1-10) while other should print the cube of the numbers. ( 1-10)
import threading
def print_squares():
def print_cubes():
thread1 = threading.Thread(target=print_squares)
thread2 = threading.Thread(target=print_cubes)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print("Execution complete.")
Output:
Q.8. Write Programs to perform searching, adding, updating the content from
the file.
file.write(content + '\n')
lines = file.readlines()
found = False
if search_term in line:
print(f"Found: {line.strip()}")
found = True
if not found:
lines = file.readlines()
if old_content in line:
file.write(line.replace(old_content, new_content))
print("Content updated successfully.")
else:
file.write(line)
def main():
file_name = 'sample_file.txt'
search_content(file_name, "test")
print(file.read())
main()
Output:
After Adding Content:
Content added
successfully. Content
added successfully.
Hello, this is a
sample. This is
another line.
Q.9. Write a pythan program to perform following operations. on MongoDB
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/") db
= client["CompanyDB"]
emp_collection = db["EMP"]
employees = [
emp_collection.insert_many(employees) print("5
documents inserted.")
emp_collection.find(query)
employees_ordered:
print(emp)
client.close()
Output:
5 documents inserted.
Employees with salary between 5000 and 10000:
{'_id': ObjectId('...'), 'Emp-name': 'John', 'Emp-mobile': '9876543210', 'Emp-sal': 8000, 'Age': 30}
{'_id': ObjectId('...'), 'Emp-name': 'John', 'Emp-mobile': '9876543210', 'Emp-sal': 8000, 'Age': 30}
INSTALLED_APPS = [
'userlogin',
urlpatterns = [
urlpatterns = [
path('admin/', admin.site.urls),
path('userlogin/', include('userlogin.urls')),
if request.method == "POST":
request.POST.get('password')
return redirect('dashboard')
else:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="POST">
{% csrf_token %}
<label for="username">Username:</label>
<label for="password">Password:</label>
<br><br>
<button type="submit">Login</button>
</form>
</body>
</html>