0% found this document useful (0 votes)
3 views15 pages

Python Component

The document provides various Python programming exercises involving tuples, sets, lists, and dictionaries. It includes code snippets for declaring and manipulating these data structures, such as finding minimum and maximum values in tuples, performing set operations, counting occurrences in lists, and converting lists to dictionaries. Each section contains specific tasks along with corresponding code examples to illustrate the concepts.

Uploaded by

ddsuresh2005
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)
3 views15 pages

Python Component

The document provides various Python programming exercises involving tuples, sets, lists, and dictionaries. It includes code snippets for declaring and manipulating these data structures, such as finding minimum and maximum values in tuples, performing set operations, counting occurrences in lists, and converting lists to dictionaries. Each section contains specific tasks along with corresponding code examples to illustrate the concepts.

Uploaded by

ddsuresh2005
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

PYTHON COMPONENT

Tuple:
1. Write a python program to declare a tuple and print all
the elements of a tuple

CODE
# Declaring a tuple
my_tuple = (10, 20, 30, 40, 50)
# Printing all elements of the tuple
for element in my_tuple:
print(element)

2. Write a python program to find the minimum,


maximum, length of a tuple and Sum of the elements in
a tuple.

CODE
# Declare a tuple
my_tuple = (10, 25, 5, 40, 15)
# Find minimum element
minimum = min(my_tuple)
# Find maximum element
maximum = max(my_tuple)
# Find length of tuple
length = len(my_tuple)
# Find sum of elements
total = sum(my_tuple)
# Print results
print("Tuple:", my_tuple)
print("Minimum:", minimum)
print("Maximum:", maximum)
print("Length:", length)
print("Sum:", total)
3. Write a python program to check whether the element
is present in the list, if present print the index.

CODE
my_list = [10, 20, 30, 40, 50]
element = int(input("Enter the element to search: "))
if element in my_list:
print("Element found at index:", my_list.index(element))
else:
print("Element not found in the list")

Set:
4. Declare two sets A and B and perform all the set
operations

# Declare two sets


A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

# Union
print("Union of A and B:", A | B)

# Intersection
print("Intersection of A and B:", A & B)
# Difference (A - B)
print("Difference of A and B:", A - B)

# Difference (B - A)
print("Difference of B and A:", B - A)

# Symmetric Difference
print("Symmetric Difference of A and B:", A ^ B)

5. Declare a set A, add few more elements in A and print


set A.

# Declare a set
A = {1, 2, 3}

# Add elements to the set


[Link](4)
[Link](5)
[Link](6)

# Print the set


print("Set A:", A)
6. Declare a set A, remove/discard specific elements in A
and print it.

CODE
# Declare a set A
A = {1, 2, 3, 4, 5}
print("Original Set A:", A)
# Elements to remove
elements_to_remove = {2, 4}
# Remove elements from A
[Link](2)
[Link](4)
# or A = A - elements_to_remove
print("Set A after removal:", A)
List:

7. Write a python program to count the occurrence of an


item in a list

CODE
def count_occurrences(lst):
counts = {}
for item in lst:
counts[item] = [Link](item, 0) + 1
return counts
my_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
print(count_occurrences(my_list))
8. Write a python program to sort the elements of a list in
ascending/descending order.

CODE
my_list = [64, 34, 25, 12, 22, 11, 90]
print("Original List:", my_list)
Sort in ascending order
my_list.sort()
print("Sorted List (Ascending):", my_list)
Sort in descending order
my_list.sort(reverse=True)
print("Sorted List (Descending):", my_list)
[Link] a python program to add, update and remove
elements in the list.

CODE
my_list = [1, 2, 3, 4, 5]
print("Original List:", my_list)
# Add elements
my_list.append(6) # Add at the end
my_list.insert(2, 7) # Add at specific index
print("List after adding elements:", my_list)
# Update element
my_list[3] = 10
print("List after updating element:", my_list)
# Remove elements
my_list.remove(2) # Remove by value
my_list.pop(0) # Remove by index
print("List after removing elements:", my_list)
10. Write a python program to print the specified range of
elements in a list (for ex: 2:5)

CODE
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("Original List:", my_list)
# Print elements from index 2 to 5 (exclusive)
print("Elements from index 2 to 5:", my_list[2:5])
# Print elements from index 1 to end
print("Elements from index 1 to end:", my_list[1:])
# Print elements from start to index 3 (exclusive)
print("Elements from start to index 3:", my_list[:3])
# Print elements from index 2 to -2 (exclusive)
print("Elements from index 2 to -2:", my_list[2:-2])
11. Write a python program to copy from one list to another list.

CODE
# Original list
list1 = [10, 20, 30, 40, 50]

# Copy list1 to list2


list2 = [Link]()

# Print both lists


print("Original list:", list1)
print("Copied list:", list2)
Dictionary:

13. Write a python program to convert two lists into a dictionary.

CODE
# Two lists
keys = ['a', 'b', 'c', 'd']
values = [1, 2, 3, 4]

# Convert lists into a dictionary


my_dict = dict(zip(keys, values))

# Print the dictionary


print("Dictionary:", my_dict)
14. Write a python program to create a dictionary and print the
keys and values of a dictionary.

CODE
# Create a dictionary
student = {
"Name": "Arun",
"Age": 20,
"Course": "[Link]"
}

# Print keys
print("Keys:")
for key in [Link]():
print(key)

# Print values
print("\nValues:")
for value in [Link]():
print(value)
15. Write a python program to remove the values of a dictionary

CODE
# Create a dictionary
student = {
"Name": "Arun",
"Age": 20,
"Course": "[Link]",
"City": "Chennai"
}

# Remove a value using pop()


[Link]("City")

# Print dictionary after removing value


print("Dictionary after removing value:", student)
16. Write a python program to sort the elements of a dictionary.

CODE
# Create a dictionary
student = {
"Name": "Arun",
"Age": 20,
"Course": "[Link]",
"City": "Chennai"
}

# Sort dictionary by keys


sorted_dict = dict(sorted([Link]()))

# Print sorted dictionary


print("Sorted Dictionary:", sorted_dict)

You might also like