Welcome to Foundation of Data Science Laboratory
Welcome to Foundation of Data Science Laboratory
Python Data Structures
1. Lists and Tuples:
o Write a Python program to find the second largest number in a list
def find_second_largest(numbers):
if len(numbers) < 2:
raise ValueError("List must contain at least two elements")
# Initialize the largest and second largest numbers
largest = second_largest = float('-inf')
for number in numbers:
if number > largest:
# Update second largest before updating largest
second_largest = largest
largest = number
elif largest > number > second_largest:
second_largest = number
if second_largest == float('-inf'):
raise ValueError("There is no second largest number")
return second_largest
# Example usage:
numbers = [10, 5, 20, 20, 30, 40, 40]
try:
result = find_second_largest(numbers)
print(f"The second largest number is: {result}")
except ValueError as e:
print(e)
Another Method
lst = [2,3,45,6,2,23,66,3,2,65,7,2,2,657]
lst2 = [0]
for i in lst:
if lst2[-1]<i:
lst2.append(i)
lst2[-2]
o Write a Python program to merge two tuples into a dictionary.
def merge_tuples_to_dict(keys, values):
if len(keys) != len(values):
raise ValueError("The number of keys must be equal to the number of values")
# Zip the keys and values together and create a dictionary
merged_dict = dict(zip(keys, values))
return merged_dict
# Example usage:
keys_tuple = ('a', 'b', 'c')
values_tuple = (1, 2, 3)
try:
result = merge_tuples_to_dict(keys_tuple, values_tuple)
print(f"Merged dictionary: {result}")
except ValueError as e:
print(e)
2. Dictionaries and Sets:
o Write a Python program to count the frequency of each word in a given string using a dictionary.
def count_word_frequencies(text):
# Convert the string to lowercase to make the count case-insensitive
text = text.lower()
# Split the text into words
words = text.split()
# Initialize an empty dictionary to store word frequencies
word_count = {}
# Count the frequency of each word
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
return word_count
# Example usage:
input_text = "Hello world! Welcome to the world of Python. Python is amazing."
# Remove punctuation for cleaner word counting
import string
translator = str.maketrans('', '', string.punctuation)
cleaned_text = input_text.translate(translator)
result = count_word_frequencies(cleaned_text)
print(f"Word frequencies: {result}")
Another Method:
s = "Hellow this is a string and i'll use thsi for this snippet of code"
hash = {}
for i in s.lower():
if i not in hash:
hash[i] = 1
else:
hash[i]+=1
o Write a Python program to find the union and intersection of two sets.
def set_operations(set1, set2):
# Calculate the union of two sets
union_set = set1 | set2 # or set1.union(set2)
# Calculate the intersection of two sets
intersection_set = set1 & set2 # or set1.intersection(set2)
return union_set, intersection_set
# Example usage:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
union_result, intersection_result = set_operations(set1, set2)
print(f"Union of the sets: {union_result}")
print(f"Intersection of the sets: {intersection_result}")