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

PYTHON 7 output,

The document contains practical Python programming exercises covering various topics such as finding common items in lists, reversing lists, working with tuples and sets, sorting dictionaries, and using variable-length arguments. Each exercise includes code snippets demonstrating the required functionality, such as finding minimum and maximum values, reversing lists, and merging dictionaries. The document serves as a guide for practicing fundamental Python programming concepts.

Uploaded by

victimnil12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

PYTHON 7 output,

The document contains practical Python programming exercises covering various topics such as finding common items in lists, reversing lists, working with tuples and sets, sorting dictionaries, and using variable-length arguments. Each exercise includes code snippets demonstrating the required functionality, such as finding minimum and maximum values, reversing lists, and merging dictionaries. The document serves as a guide for practicing fundamental Python programming concepts.

Uploaded by

victimnil12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

PRACTICAL NO 7

6) Write a Python program to find common items from two lists.


list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]

common_items = list(set(list1) & set(list2)) # Find the intersection of two sets


print(common_items)

7) Write a Python program to reverse a list.


my_list = [1, 2, 3, 4, 5]

# Using reverse() method


my_list.reverse()
print(my_list)

# Alternatively, using slicing


reversed_list = my_list[::-1]
print(reversed_list)
PRACTICAL NO 9
4) Create a tuple and find the minimum and maximum number from it.
my_tuple = (10, 5, 8, 15, 3)

# Finding the minimum and maximum numbers


min_value = min(my_tuple)
max_value = max(my_tuple)

print("Minimum:", min_value) # Output: Minimum: 3


print("Maximum:", max_value) # Output: Maximum: 15

5) Write a Python program to find the repeated items of a tuple.


my_tuple = (1, 2, 3, 4, 5, 2, 3, 6, 7, 8, 1)

# Using a dictionary to count occurrences


from collections import Counter

item_counts = Counter(my_tuple)
repeated_items = [item for item, count in item_counts.items() if count > 1]
print("Repeated items:", repeated_items) # Output: Repeated items: [1, 2, 3]
6) Print the number in words for Example: 1234 => One Two Three Four

def number_to_words(n):
num_dict = {
0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four",
5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"
}

result = " ".join(num_dict[int(digit)] for digit in str(n))


return result

number = 1234
print(number_to_words(number)) # Output: One Two Three Four
PRACTICAL NO 10
3) Write a Python program to create a set, add member(s) in a set and remove one item from set.
# Create a set
my_set = {1, 2, 3, 4}
# Add members to the set
my_set.add(5)
my_set.update([6, 7]) # Adding multiple elements
print("Set after adding elements:", my_set) # Output: {1, 2, 3, 4, 5, 6, 7}
# Remove an item from the set
my_set.remove(3) # Removes 3 from the set
print("Set after removing an element:", my_set) # Output: {1, 2, 4, 5, 6, 7}

4) Write a Python program to find maximum and the minimum value in a set.
my_set = {10, 20, 5, 40, 30}
# Find the maximum and minimum values
max_value = max(my_set)
min_value = min(my_set)
print("Maximum value:", max_value) # Output: Maximum value: 40
print("Minimum value:", min_value) # Output: Minimum value: 5
5) Write a Python program to find the length of a set.
my_set = {10, 20, 30, 40}

# Find the length of the set


length = len(my_set)

print("Length of the set:", length) # Output: Length of the set: 4


PRACTICAL NO 12
1.Write a Python script to sort (ascending and descending) a dictionary by value.
# Sample dictionary
d = {'a': 100, 'b': 200, 'c': 300, 'd': 50}
# Sort dictionary by value in ascending order
sorted_asc = dict(sorted(d.items(), key=lambda item: item[1]))
# Sort dictionary by value in descending order
sorted_desc = dict(sorted(d.items(), key=lambda item: item[1], reverse=True))
print("Ascending Order:", sorted_asc)
print("Descending Order:", sorted_desc)

2. Write a Python script to concatenate following dictionaries to create a new one. Sample Dictionary:
diel (1:10, 2:20) dic23:30, 4:40} dic35:50,6:60}
# Sample dictionaries
d1 = {1: 10, 2: 20}
d2 = {3: 30, 4: 40}
d3 = {5: 50, 6: 60}

# Concatenate dictionaries
merged_dict = {**d1, **d2, **d3}

print("Merged Dictionary:", merged_dict)


3.Write a Python program to perform following operations on set: intersection of sets, union of sets, set
difference, symmetric difference, clear a set.
# Define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# Intersection of sets
intersection = set1 & set2
print(f"Intersection: {intersection}")

# Union of sets
union = set1 | set2
print(f"Union: {union}")

# Set difference (elements in set1 but not in set2)


difference = set1 - set2
print(f"Difference (set1 - set2): {difference}")

# Symmetric difference (elements in set1 or set2 but not in both)


symmetric_difference = set1 ^ set2
print(f"Symmetric Difference: {symmetric_difference}")

# Clear a set
set1.clear()
print(f"Set1 after clearing: {set1}")
4) Write a Python program to combine two dictionary adding values for common keys. d1= {'a': 100, 'b':
200, 'c':300} d2= {'a': 300, 'b': 200, 'd':400}
d1 = {'a': 100, 'b': 200, 'c': 300}
d2 = {'a': 300, 'b': 200, 'd': 400}

# Combine dictionaries and add values for common keys


for key, value in d2.items():
if key in d1:
d1[key] += value
else:
d1[key] = value

print(f"Combined Dictionary: {d1}")

5) Write a Python program to find the highest 3 values in a dictionary .


d = {'a': 100, 'b': 200, 'c': 300, 'd': 50, 'e': 400}

# Sort the dictionary by value in descending order and get the top 3 highest values
highest_three = sorted(d.items(), key=lambda x: x[1], reverse=True)[:3]
print(f"Highest 3 Values: {highest_three}")
6) What is the output of the following program?

dictionary1 = {'Google': 1, 'Facebook': 2, 'Microsoft': 3}


dictionary2 = {'GFG': 1, 'Microsoft': 2, 'Youtube': 3}

# Update dictionary1 with dictionary2


dictionary1.update(dictionary2)

# Print the updated dictionary1


for key, value in dictionary1.items():
print(key, value)
PRACTICAL NO 14
2) Write program to demonstrate use of variable length arguments.
def demo_args(*args, **kwargs):
print("Non-keyword arguments (args):")
for arg in args:
print(arg)
print("\nKeyword arguments (kwargs):")
for key, value in kwargs.items():
print(f"{key}: {value}")
# Calling the function with variable length arguments
demo_args(1, 2, 3, name="Alice", age=25)

5) What will be the output of following code?


def print_animals(*animals):
for animal in animals:
print(animal)
print_animals("Lion", "Elephant", "Wolf", "Gorilla")
6) What will be the output of following code

def print_animals(*animals):
for animal in animals:
print(animal)

print_animals("Lion", "Elephant", "Wolf", "Gorilla")

You might also like