Krish 5-12 PR
Krish 5-12 PR
: 202303103510150
Practical 5
# Union
print("Union:", set_a.union(set_b))
# Intersection
print("Intersection:", set_a.intersection(set_b))
# Difference
print("Difference (A - B):", set_a.difference(set_b))
# Symmetric Difference
print("Symmetric Difference:", set_a.symmetric_difference(set_b))
# Adding an element
set_a.add(6)
print("Set A after adding 6:", set_a)
# Removing an element
set_a.remove(6)
print("Set A after removing 6:", set_a)
Code: b. Find the list of uncommon elements from 2 lists using a set.
Output:
Practical 6
# Accessing values
print("Accessing value for key 'name':", sample_dict.get('name'))
# Updating a value
sample_dict['age'] = 20
print("After updating the value of 'age':", sample_dict)
'Vraj': 8.5,
'Jeel': 9.0,
'Krish': 8.8,
'Kenil': 7.9,
'Meet': 9.2
}
print("Students dictionary:", students)
Code: c. Count frequency of each element of list and store in dictionary using zip().
Output:
Practical 7
Output:
Practical 8
Code: c. Get a single string from two given strings, separated by a space,
and swap the first two characters of each string.
Output:
Practical 9
a. Write a function that accepts a string and counts the number of upper
case and lower-case letters.
b. Write a python program to reverse a string.
c. Write a python program to find whether the string is palindrome or not.
Code: a. Write a function that accepts a string and counts the number of
upper case and lower-case letters.
def count_case(s):
upper_count = sum(1 for
char in s if char.isupper())
lower_count = sum(1 for
char in s if char.islower())
return upper_count,
lower_count
Code: c Write a python program to find whether the string is palindrome or not.
def is_palindrome(s):
s = s.lower().replace(" ", "") # Normalize the string
return s == s[::-1]
Output:
Practical 10
def fibonacci_sequence(n):
sequence = []
a, b = 0, 1
for _ in range(n):
sequence.append(a)
a, b = b, a + b
return sequence
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
Output:
Practical 11
a.Using regular expression, write program that finds all Date of Birth that is
in dd-mm-yyyy format from a given list.
b.Write a program that reads a text file and display all valid email-ID
and 10-digit contact number using regular expression.
Code: a.Using regular expression, write program that finds all Date of Birth
that is in dd-mm-yyyy format from a given list.
import re
# Part a: Find all Date of Birth in dd-mm-yyyy format from a given list
def find_dobs(data):
dob_pattern = r'\b\d{2}-\d{2}-\d{4}\b'
return re.findall(dob_pattern, data)
Code: b. Write a program that reads a text file and display all valid email-ID
and 10-digit contact number using regular expression.
# Part b: Read a text file and display all valid email-IDs and 10-digit contact numbers
def extract_emails_and_contacts(file_path):
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
contact_pattern = r'\b\d{10}\b'
Practical 12
Code:
import re
def is_valid_password(password):
# Regular expression for password validation
pattern = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,12}$'
# Check validity
if is_valid_password(password):
print("Password is valid.")
else:
print("Password is invalid.")
Output: