0% found this document useful (0 votes)
2 views12 pages

Python Component

The document contains multiple Python code snippets demonstrating various operations such as finding minimum and maximum values in a tuple, performing set operations, counting occurrences in a list, sorting lists, and manipulating list elements. Each section includes a brief description followed by the corresponding code. The examples cover basic data structures like tuples, sets, and lists, showcasing fundamental programming techniques in Python.

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)
2 views12 pages

Python Component

The document contains multiple Python code snippets demonstrating various operations such as finding minimum and maximum values in a tuple, performing set operations, counting occurrences in a list, sorting lists, and manipulating list elements. Each section includes a brief description followed by the corresponding code. The examples cover basic data structures like tuples, sets, and lists, showcasing fundamental programming techniques in Python.

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

1. 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)
2. Declare two sets A and B and perform all the set operations.

CODE

A = {1, 2, 3, 4, 5}

B = {4, 5, 6, 7, 8}
3. 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)


4. 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))
5. 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))
6. 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)


7. 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)
8. Write 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)


9. Write a python program to print the specified range of elements in a list

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])


10. 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")

You might also like