program file
program file
CH-12 Tuples
Program1-
bonacci = ()
a, b = 0, 1
for _ in range(9):
bonacci += (a,)
a, b = b, a + b
print( bonacci)
Program2-
a)
values = [10, 20, 30, 40, 50]
index = input("Enter the index: ")
if index.isdigit():
index = int(index)
if 0 <= index < len(values):
fi
fi
fi
print(f"The value at index {index} is
{values[index]}")
else:
print("Index out of range")
else:
print("Invalid input. Please enter a valid
integer index.”)
b)
term = int(input("Enter the Fibonacci term:
"))
a, b = 0, 1
index = 1
while a < term:
a, b = b, a + b
index += 1
if a == term:
print(f"The term {term} is at index
{index}")
else:
print(f"The term {term} is not in the
Fibonacci series")
Program3-
n = int(input("Enter the number of
elements: "))
numbers = tuple( oat(input(f"Enter number
{i+1}: ")) for i in range(n))
maximum = max(numbers)
minimum = min(numbers)
print(f"Maximum number: {maximum}")
print(f"Minimum number: {minimum}")
Program4-
n = int(input("Enter the number of
students: "))
student_info = []
for i in range(n):
roll_number = int(input(f"Enter roll
number for student {i+1}: "))
name = input(f"Enter name for student
{i+1}: ")
marks = oat(input(f"Enter marks for
student {i+1}: "))
student_tuple = (roll_number, name,
marks)
student_info.append(student_tuple)
fl
fl
student_info_tuple = tuple(student_info)
print("Student information:")
for student in student_info_tuple:
print(student)
Program5-
student_marks = []
for i in range(5):
marks_str = input(f"Enter marks for
student {i+1} (separated by spaces): ")
marks_list = tuple(map(int,
marks_str.split()))
student_marks.append(marks_list)
marks_tuple = tuple(student_marks)
print("Student marks:")
print(marks_tuple)
Program6-
student_marks = []
for i in range(5):
marks_str = input(f"Enter marks for
student {i+1} (separated by spaces): ")
marks_list = tuple(map(int,
marks_str.split()))
total_marks = sum(marks_list)
average_marks = total_marks /
len(marks_list)
student_marks.append((marks_list,
total_marks, average_marks))
marks_tuple = tuple(student_marks)
print("Student marks, total marks, and
average marks:")
for student in marks_tuple:
print(f"Marks: {student[0]}, Total Marks:
{student[1]}, Average Marks: {student[2]}")
Program7-
tuple1 = tuple(input("Enter elements of the
rst tuple (separated by spaces): ").split())
tuple2 = tuple(input("Enter elements of the
second tuple (separated by spaces):
").split())
tuple3 = tuple1 + tuple2
print("Merged tuple:", tuple3)
Program8-
str_tuple = tuple(input("Enter strings
separated by spaces: ").split())
fi
shortest_length = min(len(s) for s in
str_tuple)
print("Length of the shortest string:",
shortest_length)
Program9-
A)
squares = tuple(i**2 for i in range(1, 51))
print(squares)
B)
letters = tuple(chr(ord('a') + i) * (i + 1) for i in
range(26))
print(letters)
Program10-
pairs = ((2, 5), (4, 2), (9, 8), (12, 10))
count = sum(1 for a, b in pairs if a % 2 ==
0 and b % 2 == 0)
print("Number of pairs where both a and b
are even:", count)
Program11-
seq_a = tuple(input("Enter elements of
seq_a (separated by spaces): ").split())
seq_b = tuple(input("Enter elements of
seq_b (separated by spaces): ").split())
result = all(elem in seq_b for elem in seq_a)
print(result)
Program12-
num_tuple = tuple( oat(x) for x in
input("Enter numeric elements separated
by spaces: ").split())
sum_num = sum(num_tuple)
mean = sum_num / len(num_tuple)
print("Mean:", mean)
Program 13-
num_tuple = tuple(int(x) for x in
input("Enter elements separated by
spaces: ").split())
mode_element = None
max_count = 0
for num in num_tuple:
fl
count = sum(1 for x in num_tuple if x ==
num)
if count > max_count:
max_count = count
mode_element = num
print("Mode:", mode_element)
Program14-
import statistics
num_tuple = tuple( oat(x) for x in
input("Enter numeric elements separated
by spaces: ").split())
sum_num = sum(num_tuple)
average_manual = sum_num /
len(num_tuple)
average_mean =
statistics.mean(num_tuple)
print("Average (calculated manually):",
average_manual)
print("Average (using mean() function):",
average_mean)
if average_manual == average_mean:
print("Both averages are equal")
else:
fl
print("Averages are not equal")
Program15-
tup1 = tuple( oat(x) for x in input("Enter
elements separated by spaces: ").split())
means = [x for x in tup1]
mean_means = sum(means) / len(means)
print("Means of individual elements:")
for mean in means:
print(mean)
print("Mean of computed means:",
mean_means)
fl
CH-13 Dictionaries
Program1-
employee_salaries = {}
num_employees = int(input("Enter the
number of employees: "))
for i in range(num_employees):
name = input(f"Enter the name of
employee {i+1}: ")
salary = oat(input(f"Enter the salary of
employee {i+1}: "))
employee_salaries[name] = salary
print("Employee names and salaries:")
for name, salary in
employee_salaries.items():
print(f"{name}: {salary}”)
Program2-
string = input("Enter a string: ")
char_counts = {}
for char in string:
if char in char_counts:
char_counts[char] += 1
fl
else:
char_counts[char] = 1
print("Character counts:")
for char, count in char_counts.items():
print(f"{char}: {count}")
Program3-
num_to_word = {
'0': 'Zero', '1': 'One', '2': 'Two', '3':
'Three', '4': 'Four', '5': 'Five',
'6': 'Six', '7': 'Seven', '8': 'Eight', '9':
'Nine'
}
print(' '.join(words))
Program4-
teams = {}
while True:
team_name = input("Enter team name
(or type 'done' to nish): ")
if team_name.lower() == 'done':
break
wins = int(input("Enter number of wins:
"))
losses = int(input("Enter number of
losses: "))
teams[team_name] = [wins, losses]
team_name = input("Enter team name to
get winning percentage: ")
if team_name in teams:
wins, losses = teams[team_name]
winning_percentage = (wins / (wins +
losses)) * 100
print(f"{team_name} has a winning
percentage of {winning_percentage:.2f}%")
else:
print(f"Team '{team_name}' not found.")
fi
wins_list = [team[0] for team in
teams.values()]
winning_teams = [team for team, record in
teams.items() if record[0] > record[1]]
Program5-
products = {}
while True:
product_name = input("Enter product
name (or type 'done' to nish): ")
if product_name.lower() == 'done':
break
price = oat(input("Enter price: "))
products[product_name] = price
while True:
product_name = input("Enter product
name to get price (or type 'done' to nish):
")
fl
fi
fi
if product_name.lower() == 'done':
break
if product_name in products:
print(f"The price of {product_name} is
${products[product_name]:.2f}")
else:
print(f"Product '{product_name}' not
found.")
Program6-
month_days = {
'January': 31, 'February': 28, 'March':
31, 'April': 30, 'May': 31, 'June': 30,
'July': 31, 'August': 31, 'September': 30,
'October': 31, 'November': 30,
'December': 31
}
Program7-
D1 = {'A': 1, 'B': 2, 'C': 3, 'D': 4}
D2 = {'B': 20, 'C': 30, 'E': 50, 'F': 60}
overlapping_keys = [key for key in
D1.keys() if key in D2]
print("Overlapping keys:",
overlapping_keys)
Program8-
D1 = {'a': 10, 'b': 20, 'c': 10}
Program10-
d1 = {1: 11, 2: 12}
d2 = {1: 11, 2: 12, 3: 13, 4: 15}
if all(item in d2.items() for item in
d1.items()):
print("d1 is contained in d2")
else:
print("d1 is not contained in d2")
Program11-
D1 = {'A': [1, 2, 3], 'B': [4, 5, 6]}
D2 = {key: sum(value) for key, value in
D1.items()}
print(D2)