0% found this document useful (0 votes)
4 views

pythonrecord (1)

Uploaded by

kalpanaapj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

pythonrecord (1)

Uploaded by

kalpanaapj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

EXP 12

tuple_list = [(6, 24, 12), (60, 12, 6, -300), (12, -18, 21)]

K=6

divisible_by_k = [tup for tup in tuple_list if all(x % K == 0 for x in tup)]

print("a. Tuples where all elements are divisible by 6:", divisible_by_k)

all_positive = [tup for tup in tuple_list if all(x > 0 for x in tup)]

print("b. Tuples where all elements are positive:", all_positive)

sorted_by_last_element = sorted(tuple_list, key = lambda x: x[-1])

print("c. Tuples sorted by the last element:", sorted_by_last_element)

sorted_by_digits = sorted(tuple_list, key = lambda x: sum(len(str(abs(e))) for e in x))

print("d. Tuples sorted by total digits:", sorted_by_digits)


EXP 13

# Function to get a tuple from the user

def get_tuple():

n = int(input("Enter the number of elements in the tuple: "))

t = ()

print(f"Enter {n} elements: ")

for i in range(n):

elem = input(f"Element {i+1}: ")

t += (elem,) # Adding elements manually to the tuple

return t

# Function to compare two tuples

def compare_tuples(t1, t2):

if len(t1) != len(t2):

return "Tuples are not equal."

for i in range(len(t1)):

if t1[i] != t2[i]:

return "Tuples are not equal."

return "Both tuples are equal."

# Function to swap two tuples

def swap_tuples(t1, t2):

print("Before Swap:")

print(f"Tuple 1: {t1}")

print(f"Tuple 2: {t2}")
# Swapping

temp = t1

t1 = t2

t2 = temp

print("After Swap:")

print(f"Tuple 1: {t1}")

print(f"Tuple 2: {t2}")

# Function to concatenate two tuples

def concatenate_tuples(t1, t2):

result = ()

for i in range(len(t1)):

result += (t1[i],)

for i in range(len(t2)):

result += (t2[i],)

return result

# Function to find the length of a tuple

def length_of_tuple(t):

count = 0

for _ in t:

count += 1

return count

# Function to find maximum and minimum element in a tuple

def find_max_min(t):

max_elem = t[0]

min_elem = t[0]

for i in range(1, length_of_tuple(t)):


if t[i] > max_elem:

max_elem = t[i]

if t[i] < min_elem:

min_elem = t[i]

return max_elem, min_elem

# Function to reverse a tuple

def reverse_tuple(t):

result = ()

for i in range(length_of_tuple(t)-1, -1, -1):

result += (t[i],)

return result

# Main logic

t1 = get_tuple()

t2 = get_tuple()

# a. Compare tuples

print("\nComparing tuples...")

print(compare_tuples(t1, t2))

# b. Swap tuples

print("\nSwapping tuples...")

swap_tuples(t1, t2)

# c. Concatenate tuples

print("\nConcatenating tuples...")

concatenated_tuple = concatenate_tuples(t1, t2)

print(f"Concatenated Tuple: {concatenated_tuple}")


# d. Length of a tuple

print("\nFinding the length of Tuple 1...")

print(f"Length of Tuple 1: {length_of_tuple(t1)}")

# e. Maximum and minimum element of a tuple

print("\nFinding maximum and minimum elements in Tuple 1...")

max_elem, min_elem = find_max_min(t1)

print(f"Maximum Element: {max_elem}")

print(f"Minimum Element: {min_elem}")

# f. Reverse a tuple

print("\nReversing Tuple 1...")

reversed_tuple = reverse_tuple(t1)

print(f"Reversed Tuple: {reversed_tuple}")


EXP 14

def create_record():

sid = input("Enter Student ID: ")

name = input("Enter Name: ")

age = int(input("Enter Age: "))

gpa = float(input("Enter GPA: "))

major = input("Enter Major: ")

return (sid, name, age, gpa, major)

def update_gpa(record, new_gpa):

sid, name, age, _, major = record

return (sid, name, age, new_gpa, major)

def change_major(record, new_major):

sid, name, age, gpa, _ = record

return (sid, name, age, gpa, new_major)

def add_grad_year(record, grad_year):

return record + (grad_year,)

student = create_record()

print("\nInitial Student Record:", student)

new_gpa = float(input("\nEnter new GPA to update: "))

updated_gpa = update_gpa(student, new_gpa)

print("Updated GPA Student Record:", updated_gpa)

new_major = input("\nEnter new Major to update: ")

updated_major = change_major(student, new_major)


print("Updated Major Student Record:", updated_major)

grad_year = int(input("\nEnter Graduation Year to add: "))

updated_record = add_grad_year(student, grad_year)

print("Student Record with Graduation Year:", updated_record)


EXP 15

sales_data = [

("Product 1", [("Q1", 200), ("Q2", 300), ("Q3", 400)]),

("Product 2", [("Q1", 150), ("Q2", 250), ("Q3", 350)]),

("Product 3", [("Q1", 100), ("Q2", 200), ("Q3", 300)]),

total_sales = []

for product in sales_data:

total = sum(s for q, s in product[1])

total_sales.append(total)

print("Total Sales for Each Product:", total_sales)

highest_avg_product = None

highest_avg = 0

for product in sales_data:

avg_sales = sum(s for q, s in product[1]) / len(product[1])

if avg_sales > highest_avg:

highest_avg = avg_sales

highest_avg_product = product[0]

print(f"{highest_avg_product} has the highest average sales per quarter: {highest_avg:.2f}")

sorted_products = sorted(zip(sales_data, total_sales), key=lambda x: x[1], reverse=True)

print("Products sorted by total sales:")

for product, total in sorted_products:

print(f"{product[0]}: {total}")

normalized_sales = []

for product in sales_data:

sales_values = [s for q, s in product[1]]

if sales_values:

min_s, max_s = min(sales_values), max(sales_values)


normalized = [(q, (s - min_s) / (max_s - min_s) if max_s != min_s else 0) for q, s in product[1]]

else:

normalized = []

normalized_sales.append((product[0], normalized))

print("Normalized Sales Data:")

for product in normalized_sales:

print(f"{product[0]}: {product[1]}")
EXP 16

dict = {'b':20, 'a':35}

for key in dict:

dict[key]= -dict[key]

print(dict)

dict['c'] = 40

print(dict)

sorted_dict = {key : dict[key] for key in sorted(dict)}

print(sorted_dict)

del dict['b']

print(dict)
EXP 17

n = int(input("Enter no of products: "))

dict = {}

for i in range (0, n):

key = input("Enter product: ")

value = int(input("Enter it's price: "))

dict[key] = value

print(dict)

i = int(input("Enter the no of products to display the price: "))

for j in range (0,i):

product = input("Enter the product: ")

if(product in dict):

print(dict[product])

else:

print("Product is not in the dictionary")

dollar_amt = float(input("Enter the amount in dollar: "))

for key in dict:

if(dict[key] < dollar_amt):

print(key)

You might also like